I am writing a method to synchronize the local Core Data entities with a remote web service (in this case, Parse.com).
To update changed or created objects, I fetch all where the updatedAt date property is larger than the last local sync date. So far, so good.
But I am struggling with the deletion of local objects that have been removed on the server. My current approach is the following. This however seems quite expensive to me (\$O(r*l)\$ in the worst case, if I recall correctly). Is there any way to reduce the two loops?
PFQuery *idQuery = [PFQuery queryWithClassName:@"Product"];
[idQuery selectKeys:@[]];
NSArray *allRemote = [idQuery findObjects];
NSArray *allRemoteIds = [allRemote valueForKey:@"objectId"];
NSArray *allLocal = [Product all];
for(Product *p in allLocal) {
BOOL shouldDelete = YES;
for(NSString *remoteId in allRemoteIds) {
if([remoteId isEqualToString:p.productId]) {
shouldDelete = NO;
break;
}
}
if(shouldDelete) {
[p delete];
}
}
for-loops are not the end of the world, are they? \$\endgroup\$