core-dataiclouduimanageddocument

What is the correct way to delete a UIManagedDocument?


I can't seem to find any reliable documentation that explains the correct procedure for deleting a UIManagedDocument, and in particular one where the iCloud options have been turned ON.

I understand that this option would delete the file at this fileURL. And this would appear to be fine if iCloud is not being used.

[[NSFileManager defaultManager] removeItemAtURL:fileURL error:&error];

If iCloud is being used then CoreData creates files all over the place, including in /Document/CoreDataUbiquitySupport and in the iCloud /CoreData folder. So in this case is it up to me to call removeUbiquitousContentAndPersistentStoreAtURL for each store in the UIManagedDocument prior to calling [NSFileManager removeItemAtURL]. If so is this documented somewhere ?

[NSPersistentStoreCoordinator removeUbiquitousContentAndPersistentStoreAtURL:storeURL
                                                                     options:@{NSPersistentStoreUbiquitousContentNameKey:fileName,
   NSMigratePersistentStoresAutomaticallyOption:@YES,
         NSInferMappingModelAutomaticallyOption:@YES,
                          NSSQLitePragmasOption:@{ @"journal_mode" : @"DELETE" }}
                                                                       error:&error];

Solution

  • For iCloud core-data content you want to call the static method removeUbiquitousContentAndPersistentStoreAtURL:options:error: on the NSPersistentStoreCoordinator class, then call removeItemAtURL:error:

    See deleteManagedDocumentWithIdentifier: in my APManagedDocument project. This is on the ubiquitous_experiment branch which I am currently working on finalizing before I merge it back down to the master branch.