UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

Day 61 where to run MainActor in Swift UI

Forums > 100 Days of SwiftUI

In the clues for Day 61, there is a suggestion to use MainActor.run to update your CoreData with a fetch to data. But I do not understand where to put that MainActor.run. The errors I get are about how Main Actor cannot run async functions, and I don't understand that.

Currently, I am running the function in a .task on my View. But when the view loads the app crashes. I feel like the reason is because I am updating CoreData outside the Main Actor?

struct SectionView: View {
    @Environment(\.managedObjectContext) private var viewContext
    @FetchRequest(entity: Book.entity(), sortDescriptors: []) var subjects: FetchedResults<BookSubject>

    var body: some View {
        VStack {
            List {
                ForEach(subjects, id: \.self) { subject in 
                    Section(header: Text(subject.name_)) {
                        ForEach(subject.books, id: \.id) { book in 
                            Text(book.title_)
                        }
                    }
                }
            }
        }
        .task {
            let _ = await PopulateData().withSubject("Biography", context: viewContext)
            // This PopulateDatawithSubject function will query Core Data, and then if it doesnt exist fetch 
            // from the API and populate Core Data with that data
            // If I use `await MainActor.run()` here I get a error that main actor cannot run async functions
        }
    }
}

2      

I ended up not using MainActor. Figured it out by copying from another question in this forum about "day 61". They were using main dispatch queue instead of async/await

2      

I thought it would belong around the code that loads the database, so after the JSON has been processed. The following worked, as in: No problems reported. Not sure how to test it for correctness.

import SwiftUI

struct ContentView: View {
    @Environment(\.managedObjectContext) var moc
    @FetchRequest(sortDescriptors: [SortDescriptor(\.name)]) var cachedUsers: FetchedResults<CachedUser>

    @State private var users = [User]()

    var body: some View {
        NavigationView {

            List {
                ForEach(cachedUsers) { cachedUser in
                    NavigationLink {
                        CachedUserView(cachedUser: cachedUser)
                    } label: {
                        Text(cachedUser.wrappedName)
                    }

                }
            }
            .task {
                if users == [] {
                    do {
                        try await loadData()
                    } catch {
                        fatalError("\(error)")
                    }
                }
            }
            .navigationTitle("All Users")
        }
    }

    func loadData() async throws {
        let url = URL(string: "https://www.hackingwithswift.com/samples/friendface.json")!

        do {
            let (data, _) = try await URLSession.shared.data(for: URLRequest(url: url))

            let decoder = JSONDecoder()
            decoder.dateDecodingStrategy = .iso8601

            users = try decoder.decode([User].self, from: data)
        } catch {
            print("Failed to load data from JSON, continuing with stored version")
            return
        }

        await MainActor.run {
            for user in users {
                let cachedUser = CachedUser(context: moc)
                cachedUser.id = user.id
                cachedUser.isActive = user.isActive
                cachedUser.name = user.name
                cachedUser.age = Int16(user.age)
                cachedUser.company = user.company
                cachedUser.email = user.email
                cachedUser.address = user.address
                cachedUser.about = user.about
                cachedUser.registered = user.registered
                cachedUser.tags = user.tags.joined(separator: ",")
                for friend in user.friends {
                    let cachedFriend = CachedFriend(context: moc)
                    cachedFriend.id = friend.id
                    cachedFriend.name = friend.name
                    cachedUser.addToHasFriend(cachedFriend)
                }

                try? moc.save()
            }
        }
    }
}

2      

Hacking with Swift is sponsored by RevenueCat

SPONSORED Take the pain out of configuring and testing your paywalls. RevenueCat's Paywalls allow you to remotely configure your entire paywall view without any code changes or app updates.

Learn more here

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.