【问题标题】:Not being able to do iCloud sync between devices无法在设备之间进行 iCloud 同步
【发布时间】: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


【解决方案1】:

尝试只使用这一行

[moc mergeChangesFromContextDidSaveNotification:notification];

不确定您为什么要修改通知。

【讨论】:

  • 成功了!!!我注释掉了我在 mergeChangesFromiCloud: 方法中的通知并使用了你的代码行。谢谢!!!
  • 是的,不确定您的代码试图通过修改原始通知来实现什么。
  • 我也不是,正如我所说,我使用的是书中的代码:“iCloud for Developers”,第 351 页。
猜你喜欢
  • 1970-01-01
  • 2019-10-26
  • 2017-02-21
  • 2015-11-08
  • 2011-05-08
  • 2015-11-07
  • 2014-11-16
  • 1970-01-01
相关资源
最近更新 更多