Accessing deleted objects in iCloud notification

大兔子大兔子 提交于 2020-01-02 10:32:57

问题


I have an app set up much like the iCloudCoreDataRecipes sample (ie, using Core Data in conjunction with iCloud). In the app delegate, I observe the

NSPersistentStoreDidImportUbiquitousContentChangesNotification

When a notification arrives, I call

[context mergeChangesFromContextDidSaveNotification:note];

I have some additional processing I'd like to do when this notification is received but am having trouble using the objects identified by the NSManagedObjectID's present in the NSDeletedObjectsKey set.

NSSet *deletedObjects = [info objectForKey:NSDeletedObjectsKey];
for (NSManagedObjectID *oid in deletedObjects) {
    NSManagedObject *obj = [context objectWithID:oid];
}

If I access any property on the obj, it is nil.

I then tried running the above code prior to calling mergeChangesFromContextDidSaveNotification:

When I did that, I was able, most of the time, to access the properties on the object. In some cases, I'd get an exception for unable to fulfill fault; the record was already deleted from the Core Data store.

I realized that accessing the deleted object's properties would work if the object had been loaded into the context some time prior to the notification arriving (ie, if the object was viewed/accessed within the app).

My problem is that I'd like to do some clean-up related to the deleted objects; my NSManagedObject's have a property that I'd like to read and then use to perform some work outside of Core Data related to that value.

What am I missing? Is it possible to do this?


回答1:


You should probably look at

- (void)prepareForDeletion;

and override that in your NSManagedObject subclass.



来源:https://stackoverflow.com/questions/11317455/accessing-deleted-objects-in-icloud-notification

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!