IOS network programming-solves document conflicts in the Process of iCloud document storage

Source: Internet
Author: User

There will inevitably be conflicts in the process of saving the iCloud documents. We must have a set of conflict resolution policies. Policy adoption depends on user needs. Some of them are simple and complicated. The simplest is to directly use the current version to overwrite conflicting versions. Complex policies: for example, if two text files conflict, you can list the two conflict points for the user to determine and save them.


Our policy is to use the current version to overwrite previous versions. To resolve the conflict, you must first register the UIDocumentStateChangedNotification notification in the updateUbiquitousDocuments: method:

[Cpp]
// Called when files in iCloud change
 
-(Void) updateUbiquitousDocuments :( NSNotification *) notification {
 
... ...
 
If (_ myCloudDocument ){
 
// Register the CloudDocument object to the document coordinator. Only when the document status changes can the object be notified.
 
[NSFileCoordinator addFilePresenter: _ myCloudDocument]; ①
 
// Registration Document Status Change Notification
 
[[Nsicationcenter center defacenter center] addObserver: self selector: @ selector (resolveConflict :)
 
Name: UIDocumentStateChangedNotification object: nil]; ②
 
}
 
}
 
// Document conflict resolution
 
-(Void) resolveConflict :( NSNotification *) notification {
 
If (_ myCloudDocument
 
& _MyCloudDocument.doc umentState = UIDocumentStateInConflict) {③
 
NSLog (@ "conflict occurred ");
 
// Document Conflict Resolution Policy
 
NSError * error;
 
If (! [NSFileVersion removeOtherVersionsOfItemAtURL :_
 
MyCloudDocument. fileURL error: & error]) {④
 
NSLog (@ "remove other documents: % @", [error localizedFailureReason]);
 
Return;
 
}
 
_ MyCloudDocument. contents = _ txtContent. text; ⑤
 
[_ MyCloudDocument updateChangeCount: UIDocumentChangeDone]; ⑥
 
}
 
[[Nsicationcenter center defacenter center] removeObserver: self
 
Name: UIDocumentStateChangedNotification object: nil]; 7
 
// Remove the CloudDocument object from the document Coordinator
 
[NSFileCoordinator removeFilePresenter: _ myCloudDocument]; delimiter
 
}

// Called when files in iCloud change

-(Void) updateUbiquitousDocuments :( NSNotification *) notification {

... ...

If (_ myCloudDocument ){

// Register the CloudDocument object to the document coordinator. Only when the document status changes can the object be notified.

[NSFileCoordinator addFilePresenter: _ myCloudDocument]; ①

// Registration Document Status Change Notification

[[Nsicationcenter center defacenter center] addObserver: self selector: @ selector (resolveConflict :)

Name: UIDocumentStateChangedNotification object: nil]; ②

}

}

// Document conflict resolution

-(Void) resolveConflict :( NSNotification *) notification {

If (_ myCloudDocument

& _MyCloudDocument.doc umentState = UIDocumentStateInConflict) {③

NSLog (@ "conflict occurred ");

// Document Conflict Resolution Policy

NSError * error;

If (! [NSFileVersion removeOtherVersionsOfItemAtURL :_

MyCloudDocument. fileURL error: & error]) {④

NSLog (@ "remove other documents: % @", [error localizedFailureReason]);

Return;

}

_ MyCloudDocument. contents = _ txtContent. text; ⑤

[_ MyCloudDocument updateChangeCount: UIDocumentChangeDone]; ⑥

}

[[Nsicationcenter center defacenter center] removeObserver: self

Name: UIDocumentStateChangedNotification object: nil]; 7

// Remove the CloudDocument object from the document Coordinator

[NSFileCoordinator removeFilePresenter: _ myCloudDocument]; delimiter

}

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.