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

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

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

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

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

我应用以下代码

- (void) removeObjects_at_TopoNr_from_LocationRLM :(NSUInteger)TopoNr :(NSString *)folderName :(NSString *)fileName {

    RLMRealm *realm = [RLMRealm realmWithPath:[self get_TopoResultRLM_FilePath :folderName :fileName]];
    RLMResults *resultTopoResult = [RLMTopoResult allObjectsInRealm:realm];

    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"TopoNrRLM == %d",TopoNr];
    RLMResults *resultsTopoNr = [resultTopoResult objectsWithPredicate:predicate];

    if ([resultsTopoNr count] > 0) {
        if (TopoNr <= (int)[resultsTopoNr count]) {
            [realm beginWriteTransaction];
            [realm deleteObject:[resultsTopoNr firstObject]];
            [realm commitWriteTransaction];
        }
        else {
            NSLog(@"Fail...trying to remove TopoResult-object with TopoNr bigger to object-count");
        }
    }
    else {
        NSLog(@"Fail...trying to remove TopoResult-object in empty Realm");
    }
}

使用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孩子的方法的更新版本:
- (void) removeObjects_at_TopoNr_from_LocationRLM :(NSUInteger)TopoNr :(NSString *)folderName :(NSString *)fileName {
    RLMRealm *realm = [RLMRealm realmWithPath:[self get_TopoResultRLM_FilePath :folderName :fileName]];
    [realm beginWriteTransaction];
    RLMResults *topos = [RLMTopoResult objectsInRealm:realm where:@"TopoNrRLM == %d",TopoNr];
    for (RLMTopoResult *topo in topos) {
        [realm deleteObjects:topo.CriteriaRLM];
    }
    [realm deleteObjects:topos];
    [realm commitWriteTransaction];
}
原文链接:https://www.f2er.com/iOS/328000.html

猜你在找的iOS相关文章