简体   繁体   English

实体更新后,Coredata关系中断

[英]Coredata relationship breaks once the entity is updated

I am using coredata to save the server data through web services in my application and I am storing relationships as an object to the entity. 我正在使用coredata通过应用程序中的Web服务保存服务器数据,并将关系作为对象存储到实体中。

I have many entities eg "Inspirations" and "Products" and both are related to each other. 我有很多实体,例如“灵感”和“产品”,它们彼此相关。 I have a problem whenever the records are updated in the third entity which is "Filters" then the relations of the entities broke and I cannot apply filters on the entities. 每当第三个实体为“过滤器”中的记录更新时,我都会遇到问题,然后实体之间的关系中断,并且无法在实体上应用过滤器。

[object addRelatedInspirationsObject:related];

This is how I save relationships. 这就是我保存人际关系的方式。 I am not able to figure out why the relations are being broken once the entity is updated which has no direct link with the entity. 我无法弄清楚一旦实体更新后与实体没有直接链接的关系为什么会中断。

One thing more if I fetch and save the data of any one of the entities like "Inspirations" then all the relations start to work again. 如果我获取并保存诸如“灵感”之类的任何实体的数据,则还有一件事,那么所有关系都会重新开始工作。

From your question it seems that entities have 1 to many relationship between them. 从您的问题看来,实体之间存在1对多的关系。 And by the code you supplied, every things should work fine. 根据您提供的代码,一切都应该正常工作。 Just make sure, you are using the Filter object from the relationship like object.filter (or obj1.obj2.filter), not accessing it via a direct NSPredicate on Filter entity and updating it. 只需确保您正在使用来自object.filter(或obj1.obj2.filter)之类关系的Filter对象,而不是通过Filter实体上的直接NSPredicate访问它并对其进行更新。 And if you are using FRC, you might also need to generate a fault against the parent entities, to get your UI updates. 而且,如果您使用的是FRC,则可能还需要针对父实体生成故障,以获取UI更新。

Your code should work. 您的代码应该可以工作。 Here are 2 things you need to check: 您需要检查以下两件事:

  • Make sure related is not nil when you call your method. 调用方法时,请确保relatednil
  • Make sure you call save on a valid managed object context. 确保在有效的受管对象上下文上调用save

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

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