iOS網路編程-解決iCloud文檔預存程序中文檔衝突問題

來源:互聯網
上載者:User

iCloud文檔在儲存的過程中難免會發生衝突,我們必須要有一套解決衝突的策略。策略的採用要根據使用者的需求而定,有的簡單有的複雜,最簡單的是直接使用目前的版本覆蓋衝突版本。複雜的策略,例如:如果是兩個文字檔衝突,可以將兩個衝突點列出來,讓使用者來判斷再進行儲存。


我們採用的策略是使用目前的版本覆蓋以前的版本。解決衝突首先需要在updateUbiquitousDocuments:方法中註冊UIDocumentStateChangedNotification通知:

[cpp]
//當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];     ⑧ 
 

//當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];     ⑧

}

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.