ios – 删除一个领域对象及其所有RLMArray

前端之家收集整理的这篇文章主要介绍了ios – 删除一个领域对象及其所有RLMArray前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在努力删除领域对象及其RLMArray子(和子子)对象!

下图显示了当前的域结构(Realm-Browser的屏幕截图):

如您所见,目前创建了三个RLMTopoResult对象,每个对象都有86个RLMCriteria作为children-Array. (不可见的是,每个RLMCriteria都有自己的RLMStatistics-Array – 这就是RLMCtatistics-对象与RLMCriteria-objects一样多的原因).

现在,我们的想法是删除一个RLMTopoResult(带有根据TopoNrRLM过滤的预测器)!

我应用以下代码

  1. - (void) removeObjects_at_TopoNr_from_LocationRLM :(NSUInteger)TopoNr :(NSString *)folderName :(NSString *)fileName {
  2.  
  3. RLMRealm *realm = [RLMRealm realmWithPath:[self get_TopoResultRLM_FilePath :folderName :fileName]];
  4. RLMResults *resultTopoResult = [RLMTopoResult allObjectsInRealm:realm];
  5.  
  6. NSPredicate *predicate = [NSPredicate predicateWithFormat:@"TopoNrRLM == %d",TopoNr];
  7. RLMResults *resultsTopoNr = [resultTopoResult objectsWithPredicate:predicate];
  8.  
  9. if ([resultsTopoNr count] > 0) {
  10. if (TopoNr <= (int)[resultsTopoNr count]) {
  11. [realm beginWriteTransaction];
  12. [realm deleteObject:[resultsTopoNr firstObject]];
  13. [realm commitWriteTransaction];
  14. }
  15. else {
  16. NSLog(@"Fail...trying to remove TopoResult-object with TopoNr bigger to object-count");
  17. }
  18. }
  19. else {
  20. NSLog(@"Fail...trying to remove TopoResult-object in empty Realm");
  21. }
  22. }

使用TopoNr = 2运行上述方法删除(如预期)RLMTopoResult Nr2(参见下面的结果图) – >但是,不幸的是,它没有删除它的Array-Children(和子孙)!在删除RLMTopoResult-Nr2后,仍然存在3×86 = 258 RLMCriteria(以及258 RLMStatistics).但预计会是2×86 = 172 !!!!

我该怎么做才能自动删除附加到相应RLMTopoResult的86个RLMCriteria(及其86个RLMStatistics)子节点???

任何帮助赞赏!

下图显示删除TopoResult Nr2后的结果(带上面的代码):
(预计会有172个RLMCriteria而不是258个!……还有什么问题?……)

解决方法

级联删除规则将在未来的版本中出现,但与此同时,您可以相当轻松地自己完成此操作.这是删除topo孩子的方法的更新版本:
  1. - (void) removeObjects_at_TopoNr_from_LocationRLM :(NSUInteger)TopoNr :(NSString *)folderName :(NSString *)fileName {
  2. RLMRealm *realm = [RLMRealm realmWithPath:[self get_TopoResultRLM_FilePath :folderName :fileName]];
  3. [realm beginWriteTransaction];
  4. RLMResults *topos = [RLMTopoResult objectsInRealm:realm where:@"TopoNrRLM == %d",TopoNr];
  5. for (RLMTopoResult *topo in topos) {
  6. [realm deleteObjects:topo.CriteriaRLM];
  7. }
  8. [realm deleteObjects:topos];
  9. [realm commitWriteTransaction];
  10. }

猜你在找的iOS相关文章