Skip to content

Commit

Permalink
The previous change made it clear that only objects that once existed…
Browse files Browse the repository at this point in the history
… on the

server but no longer do should be deleted (i.e. objects with a remoteId).

Objects without a remoteId only exist locally and should not be removed (duh).
  • Loading branch information
lukeredpath committed Mar 19, 2010
1 parent 687a56a commit 628c40d
Showing 1 changed file with 4 additions and 3 deletions.
7 changes: 4 additions & 3 deletions Classes/PTSyncManager.m
Expand Up @@ -50,13 +50,14 @@ - (void)remoteModel:(id)modelKlass didFinishLoading:(NSArray *)results;
NSEntityDescription *entity = [managedObjectContext entityDescriptionForName:entityName];

// TODO it seems wrong that remoteId is hardcoded here, what if I want to use UUID instead?
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"remoteId in %@", [results valueForKeyPath:@"remoteId"]];
NSPredicate *matchingIdPredicate = [NSPredicate predicateWithFormat:@"remoteId in %@", [results valueForKeyPath:@"remoteId"]];
NSSet *managedObjectsForResultsSet = [[NSSet alloc] initWithArray:
[managedObjectContext fetchAllOfEntity:entity predicate:predicate error:nil]];
[managedObjectContext fetchAllOfEntity:entity predicate:matchingIdPredicate error:nil]];

// delete all objects that no longer exist on the server
NSPredicate *hasRemoteIdPredicate = [NSPredicate predicateWithFormat:@"remoteId <> NIL"];
NSMutableSet *allObjectSet = [[NSMutableSet alloc] initWithArray:
[managedObjectContext fetchAllOfEntity:entity error:nil]];
[managedObjectContext fetchAllOfEntity:entity predicate:hasRemoteIdPredicate error:nil]];
[allObjectSet minusSet:managedObjectsForResultsSet];

for (NSManagedObject *object in allObjectSet) {
Expand Down

0 comments on commit 628c40d

Please sign in to comment.