gpt4 book ai didi

iOS Realm 级联删除

转载 作者:行者123 更新时间:2023-12-01 16:09:12 24 4
gpt4 key购买 nike

我的模型是:
第一个模型 - Job: idx, title, description
第二个模型 - Specialization: idx, title, RLMArray<Job> jobs
如果我想删除作业并且我想删除与作业相关的所有规范,我应该怎么做。提前谢谢你。
我的代码是:

[self.storage beginWriteTransaction];
RLMResults *specs = [MBSpecialization objectsInRealm:self.storage where:@"%@ IN jobs", job];
for (MBSpecialization *spec in specs) {
[self.storage deleteObjects:spec];
}
[self.storage deleteObject:job];
[self.storage commitWriteTransaction];

最佳答案

如果您想删除所有包含被删除作业的特化,即使它们还有其他作业:

[self.storage beginWriteTransaction];
[self.storage deleteObjects:[job linkingObjectsOfClass:MBSpecialization.className forProperty:@"jobs"]];
[self.storage deleteObject:job];
[self.storage commitWriteTransaction];

或者,您可能只想清理所有在删除后不再有任何工作的专业:
[self.storage beginWriteTransaction];
[self.storage deleteObject:job];
[self.storage deleteObjects:[MBSpecialization objectsInRealm:self.storage where:@"jobs.@count = 0"]];
[self.storage commitWriteTransaction];

关于iOS Realm 级联删除,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35227805/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com