文档冲突iOS网络编程-解决iCloud文档存储过程中文档冲突问题
查了好多资料,发现还是不全,干脆自己整理吧,至少保证在我的做法正确的,以免误导读者,也是给自己做个记录吧!
iCloud文档在保存的过程当中难免会生发冲突,我们必须要有一套处理冲突的略策。略策的采取要根据用户的需求而定,有的单简有的杂复,最单简的是直接用应当前版本盖覆冲突版本。杂复的略策,例如:如果是两个文本文件冲突,可以将两个冲突点列出来,让用户来判断再进行保存。
我们采取的略策是用应当前版本盖覆之前的版本。处理冲突首先需要在updateUbiquitousDocuments:方法中注册UIDocumentStateChangedNotification通知:
//当iCloud中的文件化变时候调用
- (void)updateUbiquitousDocuments:(NSNotification *)notification {
… …
if (_myCloudDocument) {
//注册CloudDocument象对到文档协调者,文档状态化变才能收到通知
[NSFileCoordinator addFilePresenter:_myCloudDocument]; ①
//注册文档状态化变通知
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(resolveConflict:)
name:UIDocumentStateChangedNotification object:nil]; ②
}
}
//文档冲突处理
- (void)resolveConflict:(NSNotification *)notification {
if (_myCloudDocument
&& _myCloudDocument.documentState == UIDocumentStateInConflict) { ③
NSLog(@”冲突生发”);
//文档冲突处理略策
NSError *error;
if (![NSFileVersion removeOtherVersionsOfItemAtURL: _
myCloudDocument.fileURL error:&error]) { ④
NSLog(@”移除其它的文档: %@”, [error localizedFailureReason]);
return;
}
_myCloudDocument.contents = _txtContent.text; ⑤
[_myCloudDocument updateChangeCount:UIDocumentChangeDone]; ⑥
}
[[NSNotificationCenter defaultCenter] removeObserver:self
name:UIDocumentStateChangedNotification object:nil]; ⑦
//从文档协调者中除解CloudDocument象对
[NSFileCoordinator removeFilePresenter:_myCloudDocument]; ⑧
}
出自《iOS络网程编与云端用应最佳实践》作者:关东升 @tony_关东升
文章结束给大家分享下程序员的一些笑话语录:
爱情观
爱情就是死循环,一旦执行就陷进去了。
爱上一个人,就是内存泄露--你永远释放不了。
真正爱上一个人的时候,那就是常量限定,永远不会改变。
女朋友就是私有变量,只有我这个类才能调用。
情人就是指针用的时候一定要注意,要不然就带来巨大的灾难。

浙公网安备 33010602011771号