Delete/Reset all entries in Core Data?

后端 未结 30 2714
天命终不由人
天命终不由人 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:20

    The accepted answer is correct with removing URL by NSFileManager is correct, but as stated in iOS 5+ edit, the persistent store is not represented only by one file. For SQLite store it's *.sqlite, *.sqlite-shm and *.sqlite-wal ... fortunately since iOS 7+ we can use method

    [NSPersistentStoreCoordinator +removeUbiquitousContentAndPersistentStoreAtURL:options:error:]

    to take care of removal, so the code should be something like this:

    NSPersistentStore *store = ...;
    NSError *error;
    NSURL *storeURL = store.URL;
    NSString *storeName = ...;
    NSPersistentStoreCoordinator *storeCoordinator = ...;
    [storeCoordinator removePersistentStore:store error:&error];
    [NSPersistentStoreCoordinator removeUbiquitousContentAndPersistentStoreAtURL:storeURL.path options:@{NSPersistentStoreUbiquitousContentNameKey: storeName} error:&error];
    

提交回复
热议问题