问题
I am trying to get the difference between two sets of Realm data (and are different objects) by filtering using an NSPredicate, but there is an error that I'm having trouble understanding. My code:
RLMResults *topStories = [KFXTopStory allObjects];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"NONE threadId = %@.topStoryId", topStories];
RLMResults *objectsToDelete = [KFXThread objectsWithPredicate:predicate];
Error:
*** Terminating app due to uncaught exception 'Invalid predicate', reason: 'Predicate with ANY modifier must compare a KeyPath with RLMArray with a value'
The query seems to state that a keypath must be compared with a value, not another keypath. Is this type of query even possible with Realm? It looks like it should be, so where am I going wrong? Any help of this or a better solution would be awesome - thanks!
EDIT:
Of course, right after I post this, I get something that works. But I'd still like to know if there is a better way. Working code:
RLMResults *topStories = [KFXTopStory allObjects];
NSMutableArray *topStoryIds = [[NSMutableArray alloc] initWithCapacity:100];
for (KFXTopStory *story in topStories) {
[topStoryIds addObject:story.topStoryId];
}
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"NOT (threadId IN %@)", topStoryIds];
RLMResults *objectsToDelete = [KFXThread objectsWithPredicate:predicate];
回答1:
Using an NSSet rather than an Array (to de-duplicate the ordered products) would probably be faster, but otherwise your solution is the best on at the moment. (Similar to realm cocoa: predicate to select items not related).
来源:https://stackoverflow.com/questions/27759737/nspredicate-using-rlmresults-as-an-argument