Delete/Reset all entries in Core Data?

后端 未结 30 2759
天命终不由人
天命终不由人 2020-11-22 16:00

Do you know of any way to delete all of the entries stored in Core Data? My schema should stay the same; I just want to reset it to blank.


Edit

30条回答
  •  清酒与你
    2020-11-22 16:17

    [Late answer in response to a bounty asking for newer responses]

    Looking over earlier answers,

    • Fetching and deleting all items, as suggested by @Grouchal and others, is still an effective and useful solution. If you have very large data stores then it might be slow, but it still works very well.
    • Simply removing the data store is, as you and @groundhog note, no longer effective. It's obsolete even if you don't use external binary storage because iOS 7 uses WAL mode for SQLite journalling. With WAL mode there may be (potentially large) journal files sitting around for any Core Data persistent store.

    But there's a different, similar approach to removing the persistent store that does work. The key is to put your persistent store file in its own sub-directory that doesn't contain anything else. Don't just stick it in the documents directory (or wherever), create a new sub-directory just for the persistent store. The contents of that directory will end up being the persistent store file, the journal files, and the external binary files. If you want to nuke the entire data store, delete that directory and they'll all disappear.

    You'd do something like this when setting up your persistent store:

    NSURL *storeDirectoryURL = [[self applicationDocumentsDirectory] URLByAppendingPathComponent:@"persistent-store"];
    if ([[NSFileManager defaultManager] createDirectoryAtURL:storeDirectoryURL
            withIntermediateDirectories:NO
            attributes:nil
            error:nil]) {
        NSURL *storeURL = [storeDirectoryURL URLByAppendingPathComponent:@"MyApp.sqlite"];
        // continue with storeURL as usual...
    }
    

    Then when you wanted to remove the store,

    [[NSFileManager defaultManager] removeItemAtURL:storeDirectoryURL error:nil];
    

    That recursively removes both the custom sub-directory and all of the Core Data files in it.

    This only works if you don't already have your persistent store in the same folder as other, important data. Like the documents directory, which probably has other useful stuff in it. If that's your situation, you could get the same effect by looking for files that you do want to keep and removing everything else. Something like:

    NSString *docsDirectoryPath = [[self applicationDocumentsDirectory] path];
    NSArray *docsDirectoryContents = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:docsDirectoryPath error:nil];
    for (NSString *docsDirectoryItem in docsDirectoryContents) {
        // Look at docsDirectoryItem. If it's something you want to keep, do nothing.
        // If it's something you don't recognize, remove it.
    }
    

    This approach may be error prone. You've got to be absolutely sure that you know every file you want to keep, because otherwise you might remove important data. On the other hand, you can remove the external binary files without actually knowing the file/directory name used to store them.

提交回复
热议问题