How do I explicitly define the NSPersistentStore and CKRecordZone.ID for @FetchRequest in SwiftUI?

29 Views Asked by At

I am updating my App to support sharing of Cloudkit data with other iCloud users. Is there a way to specify the NSPersistentStore and the CKRecordZone.ID for my fetch? I do not see an option in the predicate.

struct MyView: View {

    @FetchRequest<CD_MyManagedObject> private var fetchRequest: FetchedResults< CD_MyManagedObject >

    init() {
        _fetchRequest = FetchRequest(entity: CD_MyManagedObject.entity()
                                    , sortDescriptors: []
                                    , predicate: myfetchPredicate())
    }
}
1

There are 1 best solutions below

0
malhal On

You can't specify the zone because that is set automatically but you can choose the store with:

https://developer.apple.com/documentation/coredata/nsfetchrequest/1506518-affectedstores

You need to retain the NSFetchRequest object somewhere so you can reuse it in the FetchRequest struct init every time the View struct is init otherwise it's a memory leak.