itsniper / FTASync

Allows you to sync CoreData entities with a Parse backend.
MIT License
311 stars 59 forks source link

Duplicate objects synching core data to parse #9

Open laucel opened 11 years ago

laucel commented 11 years ago

I've setted my project and added this fix https://github.com/itsniper/FTASync/issues/6. My app is very similar to FTASyncDemo, even simpler. I have a table view showing core data objects and two buttons, one for synching and one for adding an object. I've tried to use NSFetchedResultsController and now I'm handling by myself core data notification but in both cases I'm having the same issue. This is what I do when I add a new Person object:

NSManagedObjectContext moc = [NSManagedObjectContext MR_contextWithParent: [NSManagedObjectContext MR_defaultContext]];                Person newPerson = [Person MR_createInContext: moc];            //Person newPerson = [NSEntityDescription insertNewObjectForEntityForName:@"Person" inManagedObjectContext: moc];                newPerson.name = nameField.text;        newPerson.age = [NSNumber numberWithInteger: [ageTextField.text integerValue]];        newPerson.isMale = ([isMaleField.text isEqualToString: @"YES"]?[NSNumber numberWithBool: YES]:[NSNumber numberWithBool: NO]);          [moc MR_saveNestedContexts];                  [[FTASyncHandler sharedInstance] syncWithCompletionBlock:^         {             DCLog(@"Completion Block INSERT Called");             [self dismissModalViewControllerAnimated: YES];         }                                                   progressBlock:^(float progress, NSString message)         {             DLog(@"PROGRESS UPDATE: %f - %@", progress, message);         }];            moc = nil;

I receive three NSManagedObjectContextObjectsDidChangeNotifications, the first one for an inserted object and the other two for updating an object. In the insert notification, in the managed object context I have one object. In the next two update notifications, I have two objects: One object is the one I have just created and the other is the object returned from the server after the sync operation. This is the log after last notification:

***\ DATASOURCE AFTER UPDATE(    "<Person: 0x7b835e0> (entity: Person; id: 0x6df8220 x-coredata:///Person/t3E85364B-445F-4DDA-BC49-FC80042B753D2 ; data: {\n    age = 0;\n    createdHere = 1;\n    deleted = 0;\n    isMale = 0;\n    name = Carlotta;\n    objectId = nil;\n    syncStatus = 2;\n    updatedAt = nil;\n})",    "<Person: 0x6d690c0> (entity: Person; id: 0xcff9290 x-coredata://3BB06E3A-2674-4908-B744-BC6C0208129F/Person/p8 ; data: {\n    age = 0;\n    createdHere = 1;\n    deleted = 0;\n    isMale = 0;\n    name = Carlotta;\n    objectId = mm5E0JBaqv;\n    syncStatus = 1;\n    updatedAt = \"2012-12-18 09:38:14 +0000\";\n})"

The result is that I have a duplicate Person in my table. On Parse side, the sync hasn't produced any duplicate. If I run again the app, the duplicate disappears and so everything is ok.

Can you help me figure out what's going on? Thanks, L.

UPDATE

I have this problem only on iOS 5.0 and 5.1, not on iOS 6

alessiocasti commented 11 years ago

I have the same problem, any update?

lucabartoletti commented 11 years ago

Can someone help with this?

itsniper commented 11 years ago

Sorry guys, I'm working a day job plus 2 startups, so my free-time for this is very limited.

Based on the error you are seeing I would guess your MagicalRecord is out of date. The first ObjectId above is a temporary ID, which has been fixed in the latest MagicalRecord.

See: https://github.com/magicalpanda/MagicalRecord/commit/0b00e39b2692f34e1d9c2f00e96a801892e53f14

alessiocasti commented 11 years ago

Justin, thanks for the time spent! The previous added lines are commented in the latest version of NSManagedObjectContext+MagicalRecord.m

laucel commented 11 years ago

Hi Justin, what Alessio said is true, but decommenting that lines in the class seems to fix the problem. Sorry to bother you, but do you think that you're going to update FTASync accordingly to MagicalRecord updates? Thank you for the help.