Permalink
Browse files

Added a @try block to catch exceptions thrown when applying sync chan…

…ge sets. This is possible, for example, if an object object is deleted, and a 'could not fulfill fault' exception arises.
  • Loading branch information...
1 parent c11b30d commit 710907233849045eeee11290f86981f26e1ea887 @drewmccormack committed Mar 2, 2013
Showing with 35 additions and 32 deletions.
  1. +35 −32 TICoreDataSync/02 Primary Classes/Generic Operations/TICDSSynchronizationOperation.m
@@ -528,43 +528,46 @@ - (BOOL)beginApplyingSyncChangesInChangeSet:(TICDSSyncChangeSet *)aChangeSet
syncChanges = [syncChanges sortedArrayUsingDescriptors:[NSArray arrayWithObject:sequenceSort]];
[sequenceSort release], sequenceSort = nil;
- NSInteger changeCount = 1;
- // Apply each object's changes in turn
- for( TICDSSyncChange *eachChange in syncChanges ) {
- NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
-
- switch( [[eachChange changeType] unsignedIntegerValue] ) {
- case TICDSSyncChangeTypeObjectInserted:
- [self applyObjectInsertedSyncChange:eachChange];
- [[self backgroundApplicationContext] processPendingChanges];
- break;
+ @try {
+ NSInteger changeCount = 1;
+ // Apply each object's changes in turn
+ for( TICDSSyncChange *eachChange in syncChanges ) {
+ @autoreleasepool {
+ switch( [[eachChange changeType] unsignedIntegerValue] ) {
+ case TICDSSyncChangeTypeObjectInserted:
+ [self applyObjectInsertedSyncChange:eachChange];
+ [[self backgroundApplicationContext] processPendingChanges];
+ break;
+
+ case TICDSSyncChangeTypeAttributeChanged:
+ [self applyAttributeChangeSyncChange:eachChange];
+ break;
+
+ case TICDSSyncChangeTypeToOneRelationshipChanged:
+ [self applyToOneRelationshipSyncChange:eachChange];
+ break;
+
+ case TICDSSyncChangeTypeToManyRelationshipChangedByAddingObject:
+ case TICDSSyncChangeTypeToManyRelationshipChangedByRemovingObject:
+ [self applyToManyRelationshipSyncChange:eachChange];
+ break;
+
+ case TICDSSyncChangeTypeObjectDeleted:
+ [self applyObjectDeletedSyncChange:eachChange];
+ break;
+ }
- case TICDSSyncChangeTypeAttributeChanged:
- [self applyAttributeChangeSyncChange:eachChange];
- break;
+ [[eachChange managedObjectContext] refreshObject:eachChange mergeChanges:NO]; // Keep memory low
- case TICDSSyncChangeTypeToOneRelationshipChanged:
- [self applyToOneRelationshipSyncChange:eachChange];
- break;
-
- case TICDSSyncChangeTypeToManyRelationshipChangedByAddingObject:
- case TICDSSyncChangeTypeToManyRelationshipChangedByRemovingObject:
- [self applyToManyRelationshipSyncChange:eachChange];
- break;
-
- case TICDSSyncChangeTypeObjectDeleted:
- [self applyObjectDeletedSyncChange:eachChange];
- break;
+ [self ti_alertDelegateOnMainThreadWithSelector:@selector(synchronizationOperation:processedChangeNumber:outOfTotalChangeCount:fromClientNamed:) waitUntilDone:NO, [NSNumber numberWithInteger:changeCount++], [NSNumber numberWithInteger:[syncChanges count]], self.changeSetProgressString];
+ }
}
- [[eachChange managedObjectContext] refreshObject:eachChange mergeChanges:NO]; // Keep memory low
-
- [self ti_alertDelegateOnMainThreadWithSelector:@selector(synchronizationOperation:processedChangeNumber:outOfTotalChangeCount:fromClientNamed:) waitUntilDone:NO, [NSNumber numberWithInteger:changeCount++], [NSNumber numberWithInteger:[syncChanges count]], self.changeSetProgressString];
-
- [pool drain];
+ [[self backgroundApplicationContext] processPendingChanges];
+ }
+ @catch ( NSException *exception ) {
+ return NO;
}
-
- [[self backgroundApplicationContext] processPendingChanges];
return YES;
}

0 comments on commit 7109072

Please sign in to comment.