简体   繁体   English

自动生成的核心数据访问器返回已删除的对象

[英]Auto-generated Core Data accessor returns deleted objects

I have two simple Core Data models: Patient and Treatment. 我有两个简单的核心数据模型:患者和治疗。 There is a one-to-many relationship from Patient to Treatment, so a Patient can have multiple Treatments, but each Treatment always has one Patient. 病人与治疗之间存在一对多的关系,因此一个病人可以进行多种治疗,但是每个治疗始终只有一个病人。

There is an auto-generated accessor property of Patient called treatments, defined in Patient+CoreDataProperties.h: 有一个自动生成的“患者”访问器属性,称为“治疗”,在Patient + CoreDataProperties.h中定义:

@property (nullable, nonatomic, retain) NSSet<Treatment *> *treatments;

I have a patient view that lists all his/her treatments. 我有一个病人视图,列出了他/她的所有治疗方法。 The user can swipe delete treatments from the list. 用户可以从列表中滑动删除治疗。

When I delete a treatment that is related to a patient (they all are) like this: 当我删除与患者相关的治疗(全部都是)时,如下所示:

[managedObjectContext deleteObject:treatment];
/* I even tried this: */
NSMutableSet *treatments = [patient mutableSetValueForKey:@"treatments"];
if ([treatments containsObject:treatment]) {
  /* this actually is the case */
  [treatments removeObject:treatment];
  patient.treatments = treatments;
}

[managedObjectContext save];

the patient.treatments property still contains the deleted treatment. patient.treatments属性仍然包含已删除的治疗。 I also tried to delay the table reload using dispatch_async - did not help. 我还尝试使用dispatch_async延迟表重新加载-没有帮助。

The deleted treatment is in a faulted state; 删除的处理处于故障状态; does this mean I'm required to kick out faulty objects from the treatments NSSet by hand? 这是否意味着我需要手动从NSSet处理中踢出有缺陷的物体? What can I do to force Core Data to update the treatments NSSet? 如何强制核心数据更新NSSet处理?

If I terminate the app and restart, the deleted treatment is gone. 如果我终止该应用程序并重新启动,则删除的处理将消失。

Answering my own question... 回答我自己的问题...

After half a night of try and error, I found out that calling 经过半天的尝试和错误,我发现

[moc refreshObject:patient],

as a response to a change notification resulted in deleted treatments to hang around until app restart. 作为对更改通知的响应,导致删除的处理方法一直徘徊,直到应用重新启动。 Deleting a treatment also triggered the notification, so maybe there was some kind of ... side effect. 删除治疗方法也会触发通知,因此可能会有某种...副作用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM