iPhone coredata删除通过关系连接的两个实体之间的记录

发布于 2024-09-01 06:41:29 字数 311 浏览 12 评论 0原文

我正在我的 iPhone 应用程序中实现核心数据。它有两个实体。 实体1:最新数据 Entity2:LatestDetailedData

LastData 有 URL、发布日期、标题 LatestDetailedData 具有 URL、NewsDescription、PublishedDate、Author

两个实体都具有相同的记录 URL。 这两个实体都以逆关系连接。关系是“删除->级联”

我想要的:如果我删除LatestData中的一条记录,我希望LatestDetailedData中具有相同URL的记录也必须被删除。

如何?

I'm implementing core data in my iphone app. It has two entities.
Entity1: LatestData
Entity2: LatestDetailedData

LatestData has URL, publishedDate, heading
LatestDetailedData has URL, NewsDescription, PublishedDate, Author

Both entities have same URL for a record.
Both the entities are connected with inverse relation ship. And the relation ship is "delete->Cascaded"

What I want: If I remove a record in LatestData, I want the record with same URL in LatestDetailedData must also be deleted.

How?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

最舍不得你 2024-09-08 06:41:29

如果我理解正确的话,你正在使用一种关系,并且它有一个逆关系。如果是这种情况,那么当您删除一个时,Core Data 将自动删除另一个,您无需执行任何额外操作。

您看到什么表明这种情况没有发生?

更新

由于您使用多个线程,因此每个线程是否使用一个 NSManagedObjectContext ?如果是这样,您是否在保存时更新所有线程?我怀疑这两者之一没有发生,因此导致了您的问题。

If I am understanding you correctly, you are using a relationship and it has an inverse. If that is the case then when you delete one then Core Data will automatically delete the other and you don't need to do anything extra.

What are you seeing that suggests that is not happening?

Update

Since you are using multiple threads, are you using one NSManagedObjectContext per thread? If so, are you updating all of the threads when a save occurs? I suspect one of those two is not occurring and therefore causing your issue.

扛起拖把扫天下 2024-09-08 06:41:29

当您创建实体时,您也需要创建关系

LatestDetailedData * entity2 = [[NSEntityDescription insertNewObjectForEntityForName:@"LatestDetailedData" inManagedObjectContext:context]; 
entity1.lastestdetail=entity2;

如果您只是依赖 URL 字段,那么这是不好的做法。在 coredata 中设置关系,级联删除将自行处理。

when you create the entities you need to create the relationship too

LatestDetailedData * entity2 = [[NSEntityDescription insertNewObjectForEntityForName:@"LatestDetailedData" inManagedObjectContext:context]; 
entity1.lastestdetail=entity2;

if you are just relying on a URL field, then thats bad practice. Set up the relationship in coredata and the cascaded deletes will take care of themselves.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文