I know how to delete a single object in CoreData I\'m just wondering if theres a simpler way of deleting multiple objects?
For single delete you can use
iOS10 & Swift 3
let fetchRequest = NSFetchRequest<NSFetchRequestResult>(entityName: "EntityName")
let deleteRequest = NSBatchDeleteRequest( fetchRequest: fetchRequest)
do{
try mContext.execute(deleteRequest)
}catch let error as NSError {//handle error here }
This deletes all the objects of EntityName
but you can apply additional filtering with NSPredicate
No, there is no specific method to remove multiple objects atm. But I would do something like this, simillar to what you already are doing:
- (void)removeFromManagedObjectContext {
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
fetchRequest.entity = [NSEntityDescription entityForName:@"YourEntity" inManagedObjectContext:managedObjectContext];
NSArray *results = [managedObjectContext executeFetchRequest:fetchRequest error:nil];
for (Your Entity *Entity in results) {
[managedObjectContext deleteObject:Entity];
}
}
iOS 9
Swift
let fetchRequest = NSFetchRequest(entityName: "EntityName") let deleteRequest = NSBatchDeleteRequest(fetchRequest: fetchRequest)
do {
try myPersistentStoreCoordinator.executeRequest(deleteRequest, withContext: myContext) } catch let error as NSError {
// TODO: handle the error
}
Objective-C
NSFetchRequest *request = [[NSFetchRequest alloc] initWithEntityName:@"EntityName"];
NSBatchDeleteRequest *deleteRq = [[NSBatchDeleteRequest alloc] initWithFetchRequest:request];
NSError *deleteError = nil;
[myPersistentStoreCoordinator executeRequest:deleteRq withContext:myContext error:&deleteError];
iOS 8 and older
NSFetchRequest *fr = [[NSFetchRequest alloc] init];
[fr setEntity:[NSEntityDescription entityForName:@"EntityName" inManagedObjectContext:myContext]];
[fr setIncludesPropertyValues:NO]; //only fetch the managedObjectID
NSError *error = nil;
NSArray *objects = [myContext executeFetchRequest:fr error:&error];
//error handling goes here
for (NSManagedObject *object in objects) {
[myContext deleteObject:object];
}
NSError *saveError = nil;
[myContext save:&saveError];
//more error handling here
As I know, there isn't a method for that... You should do like you're already doing. There is a method called deletedObjects
but it just returns the set of objects that will be removed from their persistent store during the next save operation, as described in class reference.
From https://www.avanderlee.com/swift/nsbatchdeleterequest-core-data/
extension NSManagedObjectContext {
/// Executes the given `NSBatchDeleteRequest` and directly merges the changes to bring the given managed object context up to date.
///
/// - Parameter batchDeleteRequest: The `NSBatchDeleteRequest` to execute.
/// - Throws: An error if anything went wrong executing the batch deletion.
public func executeAndMergeChanges(using batchDeleteRequest: NSBatchDeleteRequest) throws {
batchDeleteRequest.resultType = .resultTypeObjectIDs
let result = try execute(batchDeleteRequest) as? NSBatchDeleteResult
let changes: [AnyHashable: Any] = [NSDeletedObjectsKey: result?.result as? [NSManagedObjectID] ?? []]
NSManagedObjectContext.mergeChanges(fromRemoteContextSave: changes, into: [self])
}
}
My goal is to delete some objects so here's how I use it
var objectsToRemove = [NSManagedObjectID]()
// gather objectIDs
let batchDeleteRequest = NSBatchDeleteRequest(objectIDs: objectsToRemove)
do {
try defaultContext.executeAndMergeChanges(using: batchDeleteRequest)
} catch {
// handle error
}