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:
// Call-(void) updateubiquitousdocuments (nsnotification *) Notification {...... If (_ myclouddocument) {// register the clouddocument object to the document coordinator. Only when the document status changes can you receive the notification [nsfilecoordinator addfilepresenter: _ myclouddocument]; ① // notification of registration document status change [[nsnotificationcenter defacenter center] addobserver: Self selector: @ selector (resolveconflict :) name: uidocumentstatechangednotification object: Nil]; ②} // file conflict resolution-(void) resolveconflict :( nsnotification *) Notification {If (_ myclouddocument & _myclouddocument.doc umentstate = uido Cumentstateinconflict) {③ 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]; ⑥} [[nsicationicationcenter defacenter center] removeobserver: selfname: uidocumentstatechangednotification object: Nil]; 7. // remove the clouddocument object [nsfilecoordinator removefilepresenter: _ myclouddocument]; identifier} from the document coordinator}
From IOS network programming and cloud application best practices: Guan Dongsheng @ Tony _ Guan Dongsheng