zl程序教程

您现在的位置是:首页 >  移动开发

当前栏目

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

ios文档存储网络编程 解决 过程 冲突
2023-09-14 08:59:50 时间
p iCloud文档在保存的过程中难免会发生冲突,我们必须要有一套解决冲突的策略。策略的采用要根据用户的需求而定,有的简单有的复杂,最简单的是直接使用当前版本覆盖冲突版本。复杂的策略,例如:如果是两个文本文件冲突,可以将两个冲突点列出来,让用户来判断再进行保存。 /p p span id= more-131 /span /p p 我们采用的策略是使用当前版本覆盖以前的版本。解

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_关东升


There are so many comparations between Android phone and iPhone. We cannot make the conclusion about which one is better, bu...
div "feed-item-img" target="_blank" href="https://developer.aliyun.com/article/258001">
转载请保留此句:太阳火神的美丽人生 -  本博客专注于 敏捷开发及移动和物联设备研究:iOS、Android、Html5、Arduino、pcDuino,否则,出自本博客的文章拒绝转载或再转载,谢谢合作。
关东升 国内著名iOS/Cocos技术作家,iOS技术顾问,Cocos最有价值专家(CVP),智捷课堂首席培训专家。担任51CTO社区iOS技术顾问。 著有多部移动开发畅销书。