Core Data + CloudKit - I need to get FetchRequest's data into my Model

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)
}
}

Here is what I did to get it working, although it isn't what I wanted it does work, in case anyone else is attempting to pull from cloudkit+core data public db and has run into issues with the initial, on-launch, fetch -- This is how I got FetchRequest in SwiftUI working with my need to do additional things with those items as they are pulled into the app.

In body of the main view, I call my model when the NavigationItem is being created, and ask it to finish setup for the current item.

                        NavigationLink(destination: ItemDetailView(detailViewModel: ItemDetailViewModel(item: item, pairedFavorite: self.itemModel.pairedFavoriteForItem(cloudItem: Item), manager: self.itemModel))) { Text(item.name)

And in the model, I have this method




var fetchedAlreadyFavoritesReturnedEntities: Bool?

    func pairedFavoriteForItem(cloudItem: Item) -> FavoriteItem {

        

        if allItems.contains(s) == false {

            self.allItems.append(cloudItem)

        }

        if self.fetchedAlreadyFavoritesReturnedEntities == nil && self.allFavoriteItemObjects.isEmpty == true {

            let request = NSFetchRequest<FavoriteItem>(entityName: "FavoriteItem")

            

            

            request.sortDescriptors  = [NSSortDescriptor(key: "favoritedState", ascending: true)]

            let favoriteRequestFRC =  NSFetchedResultsController(fetchRequest: request,

                                                                 managedObjectContext: self.localManagedObjectContext,

                                                                 sectionNameKeyPath: nil,

                                                                 cacheName: nil)

            favoriteRequestFRC.delegate = self

            try? favoriteRequestFRC.performFetch()

            let fetched: [FavoriteItem]? = favoriteRequestFRC.fetchedObjects

            

            if let f = fetched {

                self.allFavoriteItemObjects = Set<FavoriteItem>(f)

            }

            self.fetchedAlreadyFavoritesReturnedEntities = true

        }

        let results = self.allFavoriteItemObjects.filter {

            return ($0.matchingItemUUID!.uuidString == cloudItem.uuid!.uuidString)

        }

        if results.isEmpty == false {

            if let fave: FavoriteItem = results.first {

                s.isFavorite = fave.favoritedState

            }

            return results.first!

        }

        else {

                        let newFavorite = self.makeFavoriteWithItem(cloudItem)

                        self.allFavoriteItemObjects.insert(newFavorite)

                        

                        return newFavorite

        }

    }
Core Data + CloudKit - I need to get FetchRequest's data into my Model
 
 
Q