Pregunta

I have an app that uses core data and integrated iCloud. 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 get updated on the other one.

Here is my code for deleting in core data:

- (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 my code with the 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]; 
}]; 
}

So, anyone could help me with this?

Thank you.

¿Fue útil?

Solución

Try using just this line

[moc mergeChangesFromContextDidSaveNotification:notification];

Not sure why you are modifying the notification.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top