Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Delete one realm-object and all its RLMArrays

I am struggling with the deletion of realm objects and its RLMArray children (and sub-children) Objects !

The following picture shows the current realm-structure (screenshot of Realm-Browser) :

Current realm structure

As you can see, there are currently three RLMTopoResult objects created, each having 86 RLMCriteria as a children-Array. (What is not visible is that each of those RLMCriteria as well has its own RLMStatistics-Array - thats why there are as many RLMStatistics-objects as RLMCriteria-objects).

Now, the idea is to delete one RLMTopoResult (with a predictor that filters according to TopoNrRLM) !

I apply the following code :

- (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");
    }
}

Running the above method with TopoNr = 2 deletes (as expected) the RLMTopoResult Nr2 (see result-picture below) --> But, unfortunately, it does not delete its Array-Children (and subchildren) !!! After deletion of RLMTopoResult-Nr2, there are still 3x86=258 RLMCriteria (and also 258 RLMStatistics). But expected would be 2x86=172 !!!!

What can I do to automatically also delete the 86 RLMCriteria (and its 86 RLMStatistics) children attached to the corresponding RLMTopoResult ???

Any help appreciated !

The following image shows the result after deletion of TopoResult Nr2 (with above code) : (expected to have 172 RLMCriteria instead of 258 !...what is still wrong ??...)

enter image description here

like image 969
iKK Avatar asked Nov 24 '14 11:11

iKK


1 Answers

Cascading delete rules are coming to realm in a future release, but in the meantime you can do this yourself fairly easily. Here's an updated version of your method that deletes topo children:

- (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];
}
like image 135
jpsim Avatar answered Nov 12 '22 19:11

jpsim