iCloud文档在保存的过程中难免会发生冲突,我们必须要有一套解决冲突的策略。策略的采用要根据用户的需求而定,有的简单有的复杂,最简单的是 直接使用当前版本覆盖冲突版本。复杂的策略,例如:如果是两个文本文件冲突,可以将两个冲突点列出来,让用户来判断再进行保存。
我们采用的策略是使用当前版本覆盖以前的版本。解决冲突首先需要在updateUbiquitousDocuments:方法中注册UIDocumentStateChangedNotification通知:
-
-
- - (void)updateUbiquitousDocuments:(NSNotification *)notification {
-
- … …
-
- if (_myCloudDocument) {
-
-
-
- [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]; ⑦
-
-
-
- [NSFileCoordinator removeFilePresenter:_myCloudDocument]; ⑧
-
- 本文转自 tony关东升 51CTO博客,原文链接:http://blog.51cto.com/tonyguan/1193879,如需转载请自行联系原作者