Search results for

NSPersistentCloudKitContainer

589 results found

Post

Replies

Boosts

Views

Activity

Core Data: Using local and iCloud
Hi everyone, I'm trying to use in my app core data as persistent storage. In this app it is supposed to have some entities of core data local but also some other synced through iCloud. In the xcdatamodeld, I create two configs that belongs to each type offline(local) and the online(iCloud) but I'm getting a strange error where it says that it can't use the same container name. I don't understand if for each entity I want to be synced on iCloud I need a different container. Also, I do not understand because if I use only iCloud syncing without using any configuration, I can store different entities. Also, if each entity has a 1:1 relationship with the container, why is impossible to delete it, it doesn't make sense. Here is the code I'm using persistentContainer = NSPersistentCloudKitContainer(name: name, managedObjectModel: model) var defaultDirectoryURL: URL var descriptors = [NSPersistentStoreDescription]() for capability in self.dbCapabilities { if capability.cloudEnable { defaultDirectoryURL = NS
0
0
478
Oct ’20
Detect and handle string conflicts in Core Data with iCloud Sync
I'm trying to create a note-taking like app that uses NSPersistentCloudKitContainer and core data. The store uses the NSMergeByPropertyObjectTrumpMergePolicy, which is fine for almost every property. For example, if the name of a file is changed on two different devices, then it's fine to use the latest value. The problem is that the note text cannot be overridden by the latest value if it's changed on two devices at once. It needs to be detected as a conflict so the user can choose which version they want to keep. I can replicate the behavior by turning off wifi on one device and writing content, then writing content on a different device at the same time. When I turn the wifi back on, whichever device saved the changes last completely overrides the other device's text. What I'd like to accomplish is detect when there is a conflict of text, then create a duplicate file called Conflicted Copy. Bonus points if someone can tell me how Apple Notes magically merges text without ever creating a conflict.
0
0
1k
Sep ’22
How to get notified on CKError.quotaExceeded
Hi all, I have an iOS app which uses CloudKit and the standard NSPersistentCloudKitContainer, which I rely on for syncing app data between the user's devices. If the user's iCloud account is full I can see a log message while debugging in Xcode shortly after startup which looks something like this: error: CoreData+CloudKit: -[NSCloudKitMirroringDelegate _requestAbortedNotInitialized:](2183): - Never successfully initialized and cannot execute request ' 51383346-87BA-44D8-B527-A0B1EE35A0EF' due to error: 2FC9A487-D630-444D-B7F4-27A0F3A6B46E:(com.apple.coredata.cloudkit.zone:__defaultOwner__) = 903DD6A0-0BD8-46C0-84FB-E89797514D9F:(com.apple.coredata.cloudkit.zone:__defaultOwner__) = }> I would like to know how I can get a callback of some sort so I can run code if this CloudKit/CoreData error happens. In particular I'd like to put up some sort of warning to the user letting them know their data isn't going to sync. Please note that I'm not looking for how to do error handling as a result of a u
5
0
1.3k
Jan ’24
CD4CK: Can't find Default configuration in M.O.M.
There are a few things about how Core Data 4 Cloud Kit initializes that I'm stuggling to understand. I've spent most of the day finally getting a schema initialized on the server, and learnt a few things along the way.Firstly, even though I have a Default configuration in my managed object model, and it has been there from day one with the option Used with CloudKit checked for true, if I create an `NSPersistentStoreDescription` and set the configuration property to Default, when I try to load the stores, it will fail, saying Unable to find a configuration named 'Default' in the specified managed object model.. If I inpsect the debug description for my ManagedObjectModel instance, it does indeed not have any reference to the string Default in it (I'm not sure if debugDescription is actually printing out the configuration names however). Why does this fail? I can only get my store loaded and the schema initialized if I remove the code that sets the configuration name to Default.The next thing that tripped me up
2
0
1.7k
Aug ’19
Understanding Syncing between Core Data and CloudKit Public Database using NSPersistantCloudKitContainer
Can someone please give me an overview of how sync works between Core Data and the public CloudKit database when using the NSPersistentCloudKitContainer and please point out my misunderstandings based on what I describe below? In the following code, I'm successfully connecting to the public database in CloudKit using the NSPersistentCloudKitContainer. Below is how I have Core Data and CloudKit set up for your reference. In CloudKit I have a set of PublicIconImage that I created manually via the CloudKit Console. I intend to be able to download all images from the public database at the app launch to the local device and manage them via Core Data to minimize server requests, which works but only if the user is logged in. This is the behavior I see: When the app launches, all the CloudKit images get mirrored to Core Data and displayed on the screen but only if the user is logged in with the Apple ID, otherwise nothing gets mirrored. What I was expecting: I was under the impression that when co
0
0
576
Jun ’24
CD4CK wont initialize CloudKit schema
By the time this debug output is printed, I've loaded 2 store descriptions, into a NSPersistentCloudKitContainer, that both have the NSPersistentCloudKitContainerOptionsKey set, both with differing containerIdentifiers that exist in iCloud. Yet, attempting to initialize the schema fails saying none of my store descriptions in the coordinator are configured to use CloudKit. They definitely are configured in the MOM. Other observations: I'm beginning to think it's not advisable to have 2 configurations in one MOM that both point to different iCloud containers, because Xcode tells me that I have to add entities from one CloudKit configuration into another CloudKit configuration in order to get it to compile. Yet the one configuration contains just a single entity with no relationships at all. So that's really unexpected. Will Core Data attempt to generate CKRecords in both containers for the entities that appear in both CloudKit configurations? Perhaps it's best to use seperate MOMs (and an associated NSPersistentCloudKitContainer
1
0
1.8k
Aug ’19
How to add Persistent History Tracking in already existing NSPersistentContainer
As I said in the title I have a core data container created in a custom class static var context: NSManagedObjectContext { let context = persistentContainer.viewContext context.automaticallyMergesChangesFromParent = true return context } // MARK: - Core Data stack static var persistentContainer: NSPersistentCloudKitContainer = { /* The persistent container for the application. This implementation creates and returns a container, having loaded the store for the application to it. This property is optional since there are legitimate error conditions that could cause the creation of the store to fail. */ let container = PersistentContainer(name: App) container.loadPersistentStores(completionHandler: { (storeDescription, error) in if let error = error as NSError? { print(Unresolved error (error), (error.userInfo)) } }) let description = container.persistentStoreDescriptions.first description?.setOption(true as NSNumber, forKey: NSPersistentHistoryTrackingKey) container.persistentStoreDescriptions = [desc
0
0
665
Jan ’20
Filter store transactions Core Data
Hello I have this Core Data stack and I have an observer to observe NSPersistentStoreRemoteChange, I would like to filter changes that the user has made to call the mergePersistentHistoryChanges() just when needed, I think it has to be done in the fetchPersistentHistoryTransactionsAndChanges() function but I don't know how to do it. Can you help me. Thank You Here is my Core Data Stack: class PersistenceController { static let shared = PersistenceController() private var notificationToken: NSObjectProtocol? init() { notificationToken = NotificationCenter.default.addObserver(forName: .NSPersistentStoreRemoteChange, object: nil, queue: nil) { note in Task { await self.fetchPersistentHistory() } } } deinit { if let observer = notificationToken { NotificationCenter.default.removeObserver(observer) } } private var lastToken: NSPersistentHistoryToken? /// A persistent container to set up the Core Data stack. lazy var container: NSPersistentCloudKitContainer = { let fileContainer = URL.storeURL(for: group n
0
0
968
Jan ’22
Many-to-one relationship can't be unlinked
I am using NSPersistentCloudKitContainer, and have a many-to-one optional relationship, let say many item to one project.This relationship is optional, so I can do this ->item.project = nilwhen the user decided that this item does not belongs to any project.This code is fine in Core Data, and the inverse relationships are updated accordingly.However, once this gets synced to CloudKit, and come back down, the project relationship will be restored. Setting the project to other entities are fine, it just cannot be set to nil.Playing with the CloudKit dashboard, I realized that the schema that is created have a CD_Project as String. If I create a new item without any project, there's no such field, however, once I assign a project to an item, the field is set, and there is no way to unset this field, even from the CloudKit Dashboard. I naively tried to clear out the string in cloudkit dashboard but it crashes my app when the sync happens.Is this a bug that will be fixed? The issue is also posted by an
3
0
1.4k
Jan ’20
CloudKit Dashboard logs show no events
I'm using NSPersistentCloudKitContainer in an app and was just studying up on how to do some debugging using the CloudKit Dashboard (watching Build Better Apps with CloudKit Dashboard from WWDC17) - in particular, how to look at live and historical server logs. When I go to CloudKit Dashboard > my.app.bundle.id > Development > Logs and select the Live tab, I see the Logs screen with its header, but instead of showing any log entries, it shows a message saying Logged events will automatically appear here as they happen on the server. If I run my app in development mode (e.g. from Xcode) and update entries, I can see the updated records in CloudKit Dashboard, but the Logs screen stays empty. I can't get CloudKit Dashboard to show any logs at all - live or historical, development or production (yes I know we can't see live production logs, but I get the same blank log area when viewing historical logs for time periods when I know there have been many events). The app is in active development an
6
0
1.8k
Sep ’20
CloudKit, 2 quick saves, error: "Could not merge changes"
I'm experimenting with Core Data and CloudKit using the recommended `NSPersistentCloudKitContainer`. I'm kind of new to Core Data so maybe I'm doing something simple/stupid. I get an error when changing the object twice, quickly.I have a SwiftUI view of one of my managed objects, with button toggle the isActive property. If I tap it twice in a row, I get an error from `NSManagedObjectContext.save`. How do you prevent or recover from this kind of error?It seems to be a timing thing, because I'm having trouble duplicating it. private func toggleActiveState() { exercise.isActive = !exercise.isActive try! moc.save() // error }RobFatal error: 'try!' expression unexpectedly raised an error: Error Domain=NSCocoaErrorDomain Code=133020 Could not merge changes. UserInfo={conflictList=( NSMergeConflict (0x282fb15c0) for NSManagedObject (0x2819ca8a0) with objectID '0xb61bc172bb432131 <x-coredata://6C618B32-5866-4E35-93C9-3B7F3D221FE6/MyObject/p7>' with oldVersion = 4 and newVersion = 5 and old object snap
1
0
940
Jun ’20
How to save to specific stores/configurations in Core Data
I'm currently syncing core data with the CloudKit private and public databases, as you can see in the code below, I'm saving the private database in the default configuration in Core Data and the public in a configuration called Public everything works fine when NSPersistentCloudKitContainer syncs, what I'm having an issue with is trying to save to the public data store PublicStore, for instance when I try to save with func createIconImage(imageName: String) it saves the image to the default store, not the PublicStore(Public configuration). What could I do to make the createIconImage() function save to the PublicStore sqlite database? class CoreDataManager: ObservableObject{ static let instance = CoreDataManager() private let queue = DispatchQueue(label: CoreDataManagerQueue) @AppStorage(UserDefaults.Keys.iCloudSyncKey) private var iCloudSync = false lazy var context: NSManagedObjectContext = { return container.viewContext }() lazy var container: NSPersistentContainer = { return setupContainer() }()
1
0
721
Jul ’24
CloudKit Multi-Device Syncing
Let's suppose I want to provide a new user with some sample data which they can use to familiarize themselves with my app. In the NSPersistentContainer (i.e., local-only) case, I use one-time initialization logic to manually populate my app with this sample data. Then, over time, the user can keep that data, modify it, or delete it, as they choose.Now if I want to do the same thing with NSPersistentCloudKitContainer, this should be fine with my user's first device. But let's further suppose that (after using my app [and changing the CoreData database contents), my user load my app on a second device and runs it. On the second device, it will ALSO build an initial database containing sample data.What happens now? Does the content of the second device superceed the contents of the first? Suppose the user deleted a same record on device 1, but that sample record is loaded onto device 2 (as part of it's one-time initialization). Does that record reappear on device 1? And if a sample record is modified on
1
0
1k
Mar ’20
A suggestion for another way to implement public database syncing
In session wwdc20-10650 - https://developer.apple.com/videos/play/wwdc2020/10650/ Sync a Core Data store with the CloudKit public database at 14:22, Nick says NSPersistentCloudKitContainer can't use CKFetchRecordZoneChangesOperation with the public database, it has to use CKQueryOperation. I was wondering why didn't you use CKSubscription? Create a subscription for each record type and the CKQueryNotification can contain CKQueryNotificationReasonRecordDeleted. It's been a while since I worked on my own public database sync but I think that is how I did it. This is the second time I've heard of the sync design being affected by limitations of CloudKit and it worries me. The first time it was when you decided not to use CKReference and instead create your own relation records because you said CloudKit had a limit on the number of references. Personally I didn't think the number was too low and I didn't understand why it couldn't just be raised, and if you had paired that with the non-public CKReference
1
0
915
Jun ’20
Cloudkit Share Updating Core Data NSManaged Objects
I have a simple shopping list app that is using Core Data with Cloudkit. A user creates a list then adds items to the list. The list can be shared to other users. Core Data is first used to save the Lists and Items as NSManaged Objects. A list is then shared to another user who can add to items to the list, and both users working on the same list stay in sync with each other. This is all mostly working but I am having some trouble getting Core Data to update the locally stored NSManaged objects with changes from the Cloud Kit Share. Currently the app is dealing with a mix of NSManaged Objects and CKRecords to display in the table View that shows the items on a list. Which is a bit messy. I really want to just work with NSManaged Objects with Cloudkit doing the work of keeping things in sync between the 2 users. When User B adds an Item it successfully saves to the Shared Database and sets the parent as the list that was shared. This is where I get a bit stuck as I want User A to see the Item that User B added
0
0
701
Apr ’21