NSPredicate using RLMResults as an argument

▼魔方 西西 提交于 2019-12-13 15:08:08

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!