在iCloud通知中访问已删除的对象 [英] Accessing deleted objects in iCloud notification

查看:144
本文介绍了在iCloud通知中访问已删除的对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个应用程序设置很像iCloudCoreDataRecipes示例(即,使用Core Data与iCloud一起使用)。在应用程式委托中,我观察

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

当通知到达时,我调用

[context mergeChangesFromContextDidSaveNotification:note];

我收到此通知时遇到问题,由NSManagedObjectID在NSDeletedObjectsKey集合中存在的对象。

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];
}



如果我访问 obj 它是nil。

然后我尝试在调用mergeChangesFromContextDidSaveNotification之前运行上面的代码:

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

,我可以在大多数时间 访问对象上的属性。在某些情况下,我会收到无法完成错误的例外;该记录已从Core Data存储中删除。

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).

我的问题是,我想做一些清理与删除对象;我的NSManagedObject有一个属性,我想读,然后使用来执行一些工作外的Core Data相关的值。

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.

我缺少什么?

推荐答案

您应该看看

- (void)prepareForDeletion;

并覆盖您的 NSManagedObject 子类。

这篇关于在iCloud通知中访问已删除的对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆