27 Dec 2013, 18:03
Generic-user-small

Marco Almeida (5 posts)

I have an app that uses core data and integrated icloud using the example avaiable in the book “iCloud for Developers” by Cesare.

The app works fine for adding and deleting data with core data. But when it comes to icloud only adding new objects is working. When I delete one object in one device, it does not gets updated on the other device.

I used the book’s default confif for merging policy and also tried other that did not work, but once again note that when I add a new entry on one device it syncs fine with the other.

So, anyone could help me with this?

Thank you.

27 Dec 2013, 19:02
Mypic_pragsmall

Cesare Rocchi (15 posts)

Can you post the code you execute when you delete an object?

27 Dec 2013, 20:21
Generic-user-small

Marco Almeida (5 posts)

I use the template of a TableViewController:

    - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
    NSManagedObjectContext *context = [self managedObjectContext];
    Watchlist *stockToDelete = [self.fetchedResultsController objectAtIndexPath:indexPath];
    [context deleteObject:stockToDelete];
    
    NSError *error = nil;
    if (![context save:&error]) {
        NSLog(@"Error! %@", error);
    }
    }
    }

And here is the code I used for iCloud merge policy:

    - (void)mergeChangesFromiCloud:(NSNotification *)notification {
NSManagedObjectContext* moc = [self managedObjectContext];
NSDictionary *noteInfo = [notification userInfo];

    [moc performBlock:^{
    NSMutableDictionary *mergingPolicyResult = [NSMutableDictionary dictionary];
    [mergingPolicyResult setObject:noteInfo[NSInsertedObjectsKey]
                            forKey:NSInsertedObjectsKey];
    [mergingPolicyResult setObject:noteInfo[NSUpdatedObjectsKey]
                            forKey:NSUpdatedObjectsKey];
    [mergingPolicyResult setObject:[NSSet set] // Exclude deletions
                            forKey:NSOverwriteMergePolicy];
    
    NSNotification *saveNotification =
    [NSNotification notificationWithName:notification.name
                                  object:self
                                userInfo:mergingPolicyResult];
    
    [moc mergeChangesFromContextDidSaveNotification:saveNotification];
    [moc processPendingChanges]; 
}]; }
28 Dec 2013, 14:06
Mypic_pragsmall

Cesare Rocchi (15 posts)

Before thinking it’s an iCloud issue … Does the same happen without iCloud? I mean, let’s pretend iCloud does not exist and you are just building a simple Core Data app, using exactly the same data model. It should be pretty easy to build it. Can you reproduce the bug in that situation?

I ask it because I’d like to make sure it has nothing to do with the delete rules of the relationships …

29 Dec 2013, 01:01
Generic-user-small

Marco Almeida (5 posts)

No Cesare, the app works fine with just Core Data, adding and delete gives me no errors. Also, my Model has no relationships since it has only one Entity.

The problem is only on deleting with iCloud enabled. But if I quit the app, when I load it again the table is updated correctly. So the problem happens when I have two devices ate the same time turned on looking at the table and I delete one entry on a advice, when I look at the other it does not get update, only if I quit the app and load again.

Also it doesn’t work if I just put the app on the background.

29 Dec 2013, 05:04
Generic-user-small

Marco Almeida (5 posts)

Cesare, I am just dropping by to say that I managed to get delete syncing working. but with a bit different code from the one you have in your book and that I used.

What I did was just exchange the notification “- (void)mergeChangesFromiCloud:(NSNotification *)notification” method to this simple one:

[moc mergeChangesFromContextDidSaveNotification:notification];

So my final code for the merge method is:

   - (void)mergeChangesFromiCloud:(NSNotification *)notification {

    NSManagedObjectContext* moc = [self managedObjectContext];
   
    [moc mergeChangesFromContextDidSaveNotification:notification];
    [moc processPendingChanges]; 
    }

But now I am having another issue, that didn’t before doing this modification: the first time I run the app and when I try to delete an entry, it just quits all of a sudden. But the second time and on I can add and delete thousands of entries and all is working fine!!!!!

Wow, coredata + iCloud is driving me crazy!!!!!

  You must be logged in to comment