温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

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

发布时间:2020-09-07 08:55:27 来源:网络 阅读:314 作者:9ria 栏目:移动开发

为大家介绍一下iOS网络编程的教程。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]; ⑧


}



向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI