20

我知道如何删除 CoreData 中的单个对象我只是想知道是否有更简单的方法来删除多个对象?

对于单个删除,您可以使用

[moc deleteObject:someManagedObject];

但是多个对象没有等价物。

目前我正在考虑做...

NSArray *arrayOfManagedObjectsToDelete = //...

for (SomeManagedObjectClass *managedObject in arrayOfManagedObjectsToDelete) {
    [moc deleteObject:managedObject];
}

但我不确定是否有其他方法可以做到这一点?

理想情况下,像...这样的方法

- (void)deleteObjects:(NSSet*)objects

onNSManagedObjectContext或类似的方法。

4

5 回答 5

22

iOS 9

迅速

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 及更早版本

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
于 2015-08-24T11:20:51.893 回答
12

iOS10斯威夫特 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 }

这会删除所有对象,EntityName但您可以使用NSPredicate

于 2017-05-23T20:50:03.633 回答
7

据我所知,没有一种方法......你应该像你已经在做的那样做。有一个方法被调用deletedObjects,但它只返回一组对象,这些对象将在下一次保存操作期间从其持久存储中删除,如类参考中所述。

于 2013-01-28T11:30:20.947 回答
5

不,没有特定的方法可以删除多个对象 atm。但我会做这样的事情,类似于你已经在做的事情:

- (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];
    }
}
于 2013-01-28T11:34:31.547 回答
0

来自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])
    }
}

我的目标是删除一些对象所以这是我如何使用它

var objectsToRemove = [NSManagedObjectID]()
// gather objectIDs

let batchDeleteRequest = NSBatchDeleteRequest(objectIDs: objectsToRemove)
do {
    try defaultContext.executeAndMergeChanges(using: batchDeleteRequest)            
} catch {
    // handle error
}
于 2020-11-14T13:19:45.227 回答