【发布时间】:2013-12-28 13:11:50
【问题描述】:
我有一个使用核心数据和集成 iCloud 的应用程序。该应用程序适用于使用核心数据添加和删除数据。但是对于 iCloud,只有添加新对象才有效。当我在一台设备中删除一个对象时,它不会在另一台设备上得到更新。
这是我在核心数据中删除的代码:
- (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);
}
}
}
这是我的 iCloud 合并策略代码:
- (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];
}];
}
那么,有人可以帮我解决这个问题吗?
谢谢。
【问题讨论】:
-
为什么要使用合并策略?您是否尝试过不使用合并策略?
-
事实上,我之所以使用它是因为它在书上,而且由于我正在尝试它的示例,所以它也在我的项目中。但是为了测试,我把方法注释掉了,但问题仍然存在,删除条目后没有同步。
-
好的,它确实有效,您是否在合并后更新 UI?查看此链接以了解有关核心数据和 iCloud 集成的更多详细信息。看看我在收到导入通知后发布通知以更新 UI 的方式。 ossh.com.au/design-and-technology/software-development/…
-
嗯,邓肯,奇怪的是,当我添加一个新条目时,它确实会正常同步,而问题只是在我删除时。
-
在这个代码模块中寻找storesDidImport方法。ossh.com.au/design-and-technology/software-development/…
标签: objective-c ios7 sync icloud