PFUbiquity error when using Core Data and iCloud

2019-06-24 06:12发布

问题:

I am using iCloud to sync my Core Data store. This works fine. I just noticed that when I launch the app I get the following error message. Is this normal? Is there something I can do to prevent it?

Thanks

2014-12-06 10:06:37.337 -[PFUbiquitySwitchboardEntryMetadata setUseLocalStorage:](808): CoreData: Ubiquity:  nobody~sim352B6DF3-0975-5C4B-B561-086A3032D6EA:ContentKeyName
Using local storage: 1
2014-12-06 10:06:37.382 __60-[PFUbiquitySetupAssistant canReadFromUbiquityRootLocation:]_block_invoke682(1476): CoreData: Ubiquity:  Attempting to download Peers hit a serious error for peers to download Error Domain=BRCloudDocsErrorDomain Code=5 "The operation couldn’t be completed. (BRCloudDocsErrorDomain error 5 - No document at URL)" UserInfo=0x7ff77ac9b110 {NSDescription=No document at URL, NSFilePath=/Users/dev/Library/Developer/CoreSimulator/Devices/0AC7A779-9C6C-4A6C-91C5-D1973CE278D9/data/Library/Mobile Documents/iCloud~app~container/Data/.DS_Store, NSUnderlyingError=0x7ff77ac990d0 "The operation couldn’t be completed. No such file or directory"} with userInfo {
    NSDescription = "No document at URL";
    NSFilePath = "/Users/dev/Library/Developer/CoreSimulator/Devices/0AC7A779-9C6C-4A6C-91C5-D1973CE278D9/data/Library/Mobile Documents/iCloud~app~container/Data/.DS_Store";
    NSUnderlyingError = "Error Domain=NSPOSIXErrorDomain Code=2 \"The operation couldn\U2019t be completed. No such file or directory\" UserInfo=0x7ff77ac9bee0 {NSDescription=No such file or directory}";
}
2014-12-06 10:06:37.409 -[PFUbiquitySwitchboardEntryMetadata setUseLocalStorage:](808): CoreData: Ubiquity:  nobody~sim352B6DF3-0975-5C4B-B561-086A3032D6EA:ContentKeyName
Using local storage: 0

回答1:

Based on my testing, here's what I believe is the problem.

When your local Core Data resources sync with iCloud, any rows deleted on another device are deleted. More rows can be deleted through the de-dupe process.

In memory you may be holding ManagedObjects which represent deleted rows directly, or may be related to those rows.

So, when you access the properties of those rows, the fault process expects to find Core Data rows, but it doesn't. The inconsistency causes the error.

My solution: Refresh all ManagedObject subclassed objects whenever you receive a NSPersistentStoreCoordinatorStoresDidChangeNotification or NSPersistentStoreDidImportUbiquitousContentChangesNotification

This worked for me. Hopefully, someone else can profit from this too.