I have been working on this for a while and haven't found a solution. I have a populated public core data database (used readonly in the app) that I need to pull from on launch. This ONLY works with SwiftUI via @FetchRequest or FetchRequest -- NSFetchResultsController, attempting to do it with persistentCloudKitContainer.viewContext.perform {} doesn't fetch (all do work after relaunching the app).
The only thing that works is FetchRequest, but I need to do things in my model as soon as I get that data from CloudKit (create local data in response). How can I set, in my model, allItems
to the wrappedValue
property of the FetchRequest
or @FetchRequest ? Since it is from cloudkit just setting the variable won't work, I need to use combine, but I can't wrap my head around how. If anyone has any suggestions -- I'd much appreciate the assistance.
class MyViewModel: ObservableObject {
typealias PublisherType = PassthroughSubject<MyViewModel, Never>
let objectWillChange = ObservableObjectPublisher()
let objectDidChange = ObservableObjectPublisher()
@Published var allItems: [Item] = []
struct MyView: View {
@FetchRequest var fetchRequest = FetchRequest<Item>(entity: Item.entity(), sortDescriptors: [NSSortDescriptor(key: "name", ascending: true)])
var body: some View {
List(fetchRequest.wrappedValue) { (item:Item) in
Text(item.name)
}
}