Delete/Reset all entries in Core Data?

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

    Several good answers to this question. Here's a nice concise one. The first two lines delete the sqlite database. Then the for: loop deletes any objects in the managedObjectContext memory.

    NSURL *storeURL = [[(FXYAppDelegate*)[[UIApplication sharedApplication] delegate] applicationDocumentsDirectory] URLByAppendingPathComponent:@"AppName.sqlite"];
    [[NSFileManager defaultManager] removeItemAtURL:storeURL error:nil];
    for (NSManagedObject *ct in [self.managedObjectContext registeredObjects]) {
        [self.managedObjectContext deleteObject:ct];
    }
    

提交回复
热议问题