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
}