background image

iOS 网络编程-解决 iCloud 文档存储过程中文档冲突问题

    iCloud 文档在保存的过程中难免会发生冲突,我们必须要有一套解决冲突的策略。策略

 

的采用要根据用户的需求而定,有的简单有的复杂,最简单的是 直接使用当前版本覆盖
冲突版本。复杂的策略,例如:如果是两个文本文件冲突,可以将两个冲突点列出来,让
用户来判断再进行保存。

        我 们 采 用 的 策 略 是 使 用 当 前 版 本 覆 盖 以 前 的 版 本 。 解 决 冲 突 首 先 需 要 在
updateUbiquitousDocuments:方法中注册 UIDocumentStateChangedNotification 通知:

//当 iCloud 中的文件变化时候调用

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

… …

if (_myCloudDocument) {

//注册 CloudDocument 对象到文档协调者,文档状态变化才能收到通知

[NSFileCoordinator addFilePresenter:_myCloudDocument];       ①

//注册文档状态变化通知

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(resolveConflict:)