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) :
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 ??...)
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];
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With