I am writing an app for MacOS and main feature will be syncing data to iCloud. All that I've already done is - project with Core-Data and CloudKit options turned on, - created a model and some basic routine to create model by importing file to app.
Since this is a large file, I am doing all actions in separate thread/dispatching a new queue. When I am debugging app, saving data to local db works fine, then data is syncing to cloud when app loose its focus and I have to say that it works, what I can say by querying the data in CloudKit dashboard. But there is a problem with syncing the data when I start import of the file and when during import I will switch to other app. After import finishes, I see the data is stored locally but it cannot sync to cloud. Instead I see the following error in console:
CoreData: debug: CoreData+CloudKit: -PFCloudKitThrottledNotificationObserver noteRecievedNotification:: : Got: NSApplicationWillBecomeActiveNotification - 0 CoreData: debug: CoreData+CloudKit: -PFCloudKitThrottledNotificationObserver noteRecievedNotification:: : Got: NSApplicationDidBecomeActiveNotification - 1 CoreData: CloudKit: CoreData+CloudKit: -PFCloudKitThrottledNotificationObserver noteRecievedNotification:: - Already scheduled a block to respond to 'NSApplicationDidBecomeActiveNotification', 2 notifications since. CoreData: debug: CoreData+CloudKit: -[PFCloudKitThrottledNotificationObserver noteRecievedNotification:]_block_invoke(49): : Executing 'AppActivateObserver' block for 'NSApplicationWillBecomeActiveNotification' clearing 2 iterations. CoreData: CloudKit: CoreData+CloudKit: -NSCloudKitMirroringDelegate _scheduleAutomatedExportWithLabel:activity:completionHandler:: - Beginning automated export - AppActivationExport: (null) CoreData: CloudKit: CoreData+CloudKit: -NSCloudKitMirroringDelegate executeMirroringRequest:error:: : Asked to execute request: 41AA55D5-F8A9-4EA9-A12A-9CA05775EC03 CoreData: CloudKit: CoreData+CloudKit: -NSCloudKitMirroringDelegate _scheduleAutomatedImportWithLabel:activity:completionHandler:: - Beginning automated import - AppActivationImport - in response to activity: (null) CoreData: CloudKit: CoreData+CloudKit: -NSCloudKitMirroringDelegate executeMirroringRequest:error:: : Asked to execute request: 3D212164-ABDF-4A44-A31D-1D5A9B19A9D9 CoreData: CloudKit: CoreData+CloudKit: -[NSCloudKitMirroringDelegate _enqueueRequest:]_block_invoke(714): : enqueuing request: 41AA55D5-F8A9-4EA9-A12A-9CA05775EC03 CoreData: warning: CoreData+CloudKit: -[NSCloudKitMirroringDelegate _enqueueRequest:]_block_invoke_2(724): Failed to enqueue request: 41AA55D5-F8A9-4EA9-A12A-9CA05775EC03 Error Domain=NSCocoaErrorDomain Code=134417 "Request ' 41AA55D5-F8A9-4EA9-A12A-9CA05775EC03' was cancelled because there is already a pending request of type 'NSCloudKitMirroringExportRequest'." UserInfo={NSLocalizedFailureReason=Request ' 41AA55D5-F8A9-4EA9-A12A-9CA05775EC03' was cancelled because there is already a pending request of type 'NSCloudKitMirroringExportRequest'.} CoreData: CloudKit: CoreData+CloudKit: -[NSCloudKitMirroringDelegate _scheduleAutomatedExportWithLabel:activity:completionHandler:]_block_invoke(2174): - Finished automatic export - AppActivationExport - with result: success: 0 madeChanges: 0 error: Error Domain=NSCocoaErrorDomain Code=134417 "Request ' 41AA55D5-F8A9-4EA9-A12A-9CA05775EC03' was cancelled because there is already a pending request of type 'NSCloudKitMirroringExportRequest'." UserInfo={NSLocalizedFailureReason=Request ' 41AA55D5-F8A9-4EA9-A12A-9CA05775EC03' was cancelled because there is already a pending request of type 'NSCloudKitMirroringExportRequest'.} CoreData: warning: CoreData+CloudKit: -NSCloudKitMirroringDelegate finishedAutomatedRequestWithResult:: Finished request ' 41AA55D5-F8A9-4EA9-A12A-9CA05775EC03' with result: success: 0 madeChanges: 0 error: Error Domain=NSCocoaErrorDomain Code=134417 "Request ' 41AA55D5-F8A9-4EA9-A12A-9CA05775EC03' was cancelled because there is already a pending request of type 'NSCloudKitMirroringExportRequest'." UserInfo={NSLocalizedFailureReason=Request ' 41AA55D5-F8A9-4EA9-A12A-9CA05775EC03' was cancelled because there is already a pending request of type 'NSCloudKitMirroringExportRequest'.} CoreData: CloudKit: CoreData+CloudKit: -[NSCloudKitMirroringDelegate _enqueueRequest:]_block_invoke(714): : enqueuing request: 3D212164-ABDF-4A44-A31D-1D5A9B19A9D9 CoreData: warning: CoreData+CloudKit: -[NSCloudKitMirroringDelegate _enqueueRequest:]_block_invoke_2(724): Failed to enqueue request: 3D212164-ABDF-4A44-A31D-1D5A9B19A9D9 Error Domain=NSCocoaErrorDomain Code=134417 "Request ' 3D212164-ABDF-4A44-A31D-1D5A9B19A9D9' was cancelled because there is already a pending request of type 'NSCloudKitMirroringImportRequest'." UserInfo={NSLocalizedFailureReason=Request ' 3D212164-ABDF-4A44-A31D-1D5A9B19A9D9' was cancelled because there is already a pending request of type 'NSCloudKitMirroringImportRequest'.} CoreData: CloudKit: CoreData+CloudKit: -[NSCloudKitMirroringDelegate _scheduleAutomatedImportWithLabel:activity:completionHandler:]_block_invoke(2150): - Finished automatic import - AppActivationImport - with result: success: 0 madeChanges: 0 error: Error Domain=NSCocoaErrorDomain Code=134417 "Request ' 3D212164-ABDF-4A44-A31D-1D5A9B19A9D9' was cancelled because there is already a pending request of type 'NSCloudKitMirroringImportRequest'." UserInfo={NSLocalizedFailureReason=Request ' 3D212164-ABDF-4A44-A31D-1D5A9B19A9D9' was cancelled because there is already a pending request of type 'NSCloudKitMirroringImportRequest'.} CoreData: warning: CoreData+CloudKit: -NSCloudKitMirroringDelegate finishedAutomatedRequestWithResult:: Finished request ' 3D212164-ABDF-4A44-A31D-1D5A9B19A9D9' with result: success: 0 madeChanges: 0 error: Error Domain=NSCocoaErrorDomain Code=134417 "Request ' 3D212164-ABDF-4A44-A31D-1D5A9B19A9D9' was cancelled because there is already a pending request of type 'NSCloudKitMirroringImportRequest'." UserInfo={NSLocalizedFailureReason=Request ' 3D212164-ABDF-4A44-A31D-1D5A9B19A9D9' was cancelled because there is already a pending request of type 'NSCloudKitMirroringImportRequest'.} CoreData: debug: CoreData+CloudKit: -PFCloudKitThrottledNotificationObserver noteRecievedNotification:: : Got: NSApplicationWillResignActiveNotification - 0 CoreData: debug: CoreData+CloudKit: -PFCloudKitThrottledNotificationObserver noteRecievedNotification:: : Got: NSApplicationDidResignActiveNotification - 1 CoreData: CloudKit: CoreData+CloudKit: -PFCloudKitThrottledNotificationObserver noteRecievedNotification:: - Already scheduled a block to respond to 'NSApplicationDidResignActiveNotification', 2 notifications since.
So what is said above - Error Domain=NSCocoaErrorDomain Code=134417 - cannot execute request because there is one pending... Looks like sync action is triggered twice...
Also, what I want to mention is - the app will sync correctly when I will restart app...
So now, all I do is:
- AppDelegate:
lazy var persistentContainer: NSPersistentCloudKitContainer = {
let container = NSPersistentCloudKitContainer(name: "TestContainer")
// container.viewContext.automaticallyMergesChangesFromParent = true
container.loadPersistentStores(completionHandler: { (storeDescription, error) in
if let error = error {
fatalError("Unresolved error \(error)")
}
container.viewContext.mergePolicy = NSMergePolicy(merge: .mergeByPropertyObjectTrumpMergePolicyType)
})
return container
}()
and I've found some good practice to set merge policy as I did: container.viewContext.mergePolicy = NSMergePolicy(merge: .mergeByPropertyObjectTrumpMergePolicyType)
- After import is finished in ViewController
do {
try managedContext!.save()
managedContext?.commitEditing()
} catch {
print(error)
return
}
And then is this problem with error in sync data as I mentioned above. For development I am using macOS 10.15.4 with Swift 5.2... Any hints? Probably there is some problem that I do not see at the moment. Any help is appreciated :)