Whoa
Whoa

Reputation: 1334

How to obtain a non-auto updating result set in Realm?

Realm's RLMResults is an auto-updating container. Typically, auto-updating is a great thing, but I'm struggling with it for a specific application:

I'm sending up arrays of model objects to the server, and then deleting them from Realm if the send was successful. Since this can happen concurrently, each model has a currentlyProcessing attribute. I use objectsWhere() with a predicate querying for unprocessed objects, set them to processing, and then aim to delete those same objects. Since the RLMResults container is auto-updating, the original unprocessedObjects RLMResults is empty, deleting that specific set isn't straightforward.

Is there any way to turn off the auto-update? Or make an immutable copy of an RLMResults?

Upvotes: 1

Views: 1828

Answers (2)

livingtech
livingtech

Reputation: 3670

In my case, it didn't work for me to use an array (RLMArray or NSArray), because I needed to make further sub-selects later, so I ended up with the following solution (which relies on my identifier property):

RLMResult *result = ...
NSMutableArray *ids = [NSMutableArray arrayWithCapacity:results.count];
for (MyObj *obj in results)
{
    [ids addObject:obj.identifier];
}
results = [MyObj objectsWhere:@"identifier IN %@", ids];

Upvotes: 1

segiddins
segiddins

Reputation: 4120

Right now, there's no way to get a 'frozen' RLMResults, but support is forthcoming. Your best bet will be to copy your objects out of that results into an array.

Upvotes: 3

Related Questions