Delete/Reset all entries in Core Data?

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

    If you want to go the delete all objects route (which is much simpler than tearing down the Core Data stack, but less performant), than this is a better implementation:

    - (void)deleteAllManagedObjectsInModel:(NSManagedObjectModel *)managedObjectModel context:(NSManagedObjectContext *)managedObjectContext
    {
        NSBlockOperation *operation = [NSBlockOperation blockOperationWithBlock:^{
            [managedObjectContext performBlockAndWait:^{
                for (NSEntityDescription *entity in managedObjectModel) {
                    NSFetchRequest *fetchRequest = [NSFetchRequest new];
                    [fetchRequest setEntity:entity];
                    [fetchRequest setIncludesSubentities:NO];
                    NSArray *objects = [managedObjectContext executeFetchRequest:fetchRequest error:nil];
                    for (NSManagedObject *managedObject in objects) {
                        [managedObjectContext deleteObject:managedObject];
                    }            
                }
    
                [managedObjectContext save:nil];
            }];
        }];
        [operation setCompletionBlock:^{
            // Do stuff once the truncation is complete
        }];
        [operation start];
    }
    

    This implementation leverages NSOperation to perform the deletion off of the main thread and notify on completion. You may want to emit a notification or something within the completion block to bubble the status back to the main thread.

    0 讨论(0)
  • 2020-11-22 16:33

    Here's a version that deletes every record in every table you have.

    Swift 4

    static func resetDatabase() {
        do {
            try dataStore.persistentStoreCoordinator.managedObjectModel.entities.forEach { (entity) in
                if let name = entity.name {
                    let fetch = NSFetchRequest<NSFetchRequestResult>(entityName: name)
                    let request = NSBatchDeleteRequest(fetchRequest: fetch)
                    try mainContext.execute(request)
                }
            }
    
            try mainContext.save()
        } catch {
            print("error resenting the database: \(error.localizedDescription)")
        }
    }
    
    0 讨论(0)
  • you can also find all the entity names, and delete them by name. Its a longer version but works well, that way you dont have to work with persistence store

     - (void)clearCoreData
    {
    NSError *error;
    NSEntityDescription *des = [NSEntityDescription entityForName:@"Any_Entity_Name" inManagedObjectContext:_managedObjectContext];
    NSManagedObjectModel *model = [des managedObjectModel];
    NSArray *entityNames = [[model entities] valueForKey:@"name"];
    
    for (NSString *entityName in entityNames){
    
        NSFetchRequest *deleteAll = [NSFetchRequest fetchRequestWithEntityName:entityName];
        NSArray *matches = [self.database.managedObjectContext executeFetchRequest:deleteAll error:&error];
    
    }
        if (matches.count > 0){
            for (id obj in matches){
    
                [_managedObjectContext deleteObject:obj];
            }
           [self.database.managedObjectContext save:&error];
        }
    }
    

    for "Any_Entity_Name" just give any one of your entity's name, we only need to figure out the entity description your entities are within. ValueForKey@"name" will return all the entity names. Finally, dont forget to save.

    0 讨论(0)
  • 2020-11-22 16:36

    You can still delete the file programmatically, using the NSFileManager:removeItemAtPath:: method.

    NSPersistentStore *store = ...;
    NSError *error;
    NSURL *storeURL = store.URL;
    NSPersistentStoreCoordinator *storeCoordinator = ...;
    [storeCoordinator removePersistentStore:store error:&error];
    [[NSFileManager defaultManager] removeItemAtPath:storeURL.path error:&error];
    

    Then, just add the persistent store back to ensure it is recreated properly.

    The programmatic way for iterating through each entity is both slower and prone to error. The use for doing it that way is if you want to delete some entities and not others. However you still need to make sure you retain referential integrity or you won't be able to persist your changes.

    Just removing the store and recreating it is both fast and safe, and can certainly be done programatically at runtime.

    Update for iOS5+

    With the introduction of external binary storage (allowsExternalBinaryDataStorage or Store in External Record File) in iOS 5 and OS X 10.7, simply deleting files pointed by storeURLs is not enough. You'll leave the external record files behind. Since the naming scheme of these external record files is not public, I don't have a universal solution yet. – an0 May 8 '12 at 23:00

    0 讨论(0)
  • 2020-11-22 16:36

    iOS9+, Swift 2

    Delete all objects in all entities

    func clearCoreDataStore() {
        let entities = managedObjectModel.entities
        for entity in entities {
            let fetchRequest = NSFetchRequest(entityName: entity.name!)
            let deleteReqest = NSBatchDeleteRequest(fetchRequest: fetchRequest)
            do {
                try context.executeRequest(deleteReqest)
            } catch {
                print(error)
            }
        }
    }
    
    0 讨论(0)
  • 2020-11-22 16:37

    You can delete the SQLite file - but I choose to do it by purging the tables individually with a functions:

    - (void) deleteAllObjects: (NSString *) entityDescription  {
        NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
        NSEntityDescription *entity = [NSEntityDescription entityForName:entityDescription inManagedObjectContext:_managedObjectContext];
        [fetchRequest setEntity:entity];
    
        NSError *error;
        NSArray *items = [_managedObjectContext executeFetchRequest:fetchRequest error:&error];
        [fetchRequest release];
    
    
        for (NSManagedObject *managedObject in items) {
            [_managedObjectContext deleteObject:managedObject];
            DLog(@"%@ object deleted",entityDescription);
        }
        if (![_managedObjectContext save:&error]) {
            DLog(@"Error deleting %@ - error:%@",entityDescription,error);
        }
    
    }
    

    The reason I chose to do it table by table is that it makes me confirm as I am doing the programming that deleting the contents of the table is sensible and there is not data that I would rather keep.

    Doing it this will is much slower than just deleting the file and I will change to a file delete if I this method takes too long.

    0 讨论(0)
提交回复
热议问题