removeObjects from RLMResults

973 Views Asked by At

How can I remove objects from RLMResults. I mean in NSMutableArray there is a function like

[self.dogs removeAllObjects]

is there any funcation like that for RLMResults or RLMArray?

3

There are 3 best solutions below

0
marius On BEST ANSWER

Such a method exists for RLMArray with removeAllObjects. This means that you disassociate the object from all other linked objects within the array.

There is no such method for RLMResults, because instances of this class represent always the latest state of your query, which you can only mutate if you modify your underlying data. So if you query on a list, this would be implicitly possible by removing objects from the list. If you query on all objects (/ a table), then you would need either delete the objects from the Realm or modify them in such a way that they don't match your query anymore.

2
user3182143 On

You can delete

RLMResults *tableDataArray;    
tableDataArray=[Dog allObjects];
[[RLMRealm defaultRealm] beginWriteTransaction];
//Deleting All Objects 
[[RLMRealm defaultRealm]deleteAllObjects];
//Remove particular object
[[RLMRealm defaultRealm]deleteObject:[tableDataArray objectAtIndex:indexPath.row]];  // I use this in didSelectRowAtIndexPath
[[RLMRealm defaultRealm] commitWriteTransaction];

For adding

RLMRealm *realm = [RLMRealm defaultRealm];
[realm beginWriteTransaction];
Dog *dog = [[Dog alloc] init];
dog.name=@"Puppy";
dog.city=@"New York";
[realm addObject:dog];
[realm commitWriteTransaction];

For Updating

RLMRealm *realm = [RLMRealm defaultRealm];
[realm beginWriteTransaction];
dog.name=@"Rosie";
dog.city=@"Washington";
[realm commitWriteTransaction];
0
olidem On

Copy all objects from your RLMResults to a NSMutableArray and operate on that then.