Skip to content

Duplicate objects synching core data to parse #9

@laucel

Description

@laucel

I've setted my project and added this fix #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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions