0

我有 2 个带有 NSManagedObjects 的 NSSet,每个集合的对象都在不同的线程中获取,这意味着有些对象具有匹配的 objectID,但对象本身是不同的。现在我想从另一组中删除 managedObjects。

NSSet* oldObjects;
NSMutableSet* currentObjects;

// I want to remove the managedObjects in oldObjects from currentObjects, all objects in oldObjects are also in currentObjects

// This doesn't work, since the objects don't match
[currentObjects removeObjectsInArray:[oldObjects allObjects]]; 

// But strangely enough, this doesn't add any objects to currentObjects, but if the objects don't match, shouldn't it?
//[currentObjects addObjectsFromArray:[oldObjects allObjects]];


// This does work for me but this code is running on the main thread and I can see this becoming rather slow for large data sets
NSArray* oldObjectIDs = [[oldObjects allObjects] valueForKey:@"objectID"];
[currentObjects filterUsingPredicate:[NSPredicate predicateWithFormat:@"NOT (objectID IN %@)", oldObjectIDs]];

有没有更快的方法可以过滤掉这些?即使在这种情况下,快速枚举也会更快吗?

4

1 回答 1

0

抱歉这么晚才回来。

我重新阅读了您的问题,现在,我已经完全理解了设置,我可能会为您提供解决方案。

这未经测试,但尝试这样的事情:

//Since the current objects set has registered its objects in the current context
//lets use that registration to see which of them is contained in the old object set
NSMutableSet* oldRegisteredSet = [NSMutableSet new];
for (NSManagedObject* o in oldObjects) {
    NSManagedObject* regObject = [context objectRegisteredForID:[o objectID]];
    if (regObject) {
        //You could do here instead: [currentObjects removeObject:regObject];
        //You should optimize here after testing performance
        [oldRegisteredSet addObject:regObject];
    }
}

[currentObjects minusSet:oldRegisteredSet];
于 2013-06-21T14:31:27.557 回答