2

我想从 CoreData 表中删除选定的项目列表:A number of Persons with certain namesToDelete

NSError*        error        = nil;
NSFetchRequest* request      = [[NSFetchRequest alloc] init];
[request setEntity:[NSEntityDescription entityForName:@"Person"
                               inManagedObjectContext:managedObjectContext]];
NSPredicate*    predicate    = [NSPredicate predicateWithFormat:@"NOT (name IN %@)", namesToDelete];
[request setPredicate:predicate];
NSArray*         deleteArray = [managedObjectContext executeFetchRequest:request error:&error];
if (error == nil)
{
    for (NSManagedObject* object in deleteArray)
    {
        [managedObjectContext deleteObject:object];
    }

    [managedObjectContext save:&error];
    //### Error handling.
}
else
{
    //### Error handling.
}

这可行,但这是在 CoreData 中执行此操作的最简单/最短的方法吗?

4

4 回答 4

6

关于最好的修剪是这样的:

NSError*        error        = nil;
NSFetchRequest* request      = [NSFetchRequest fetchRequestWithEntityName:@"Person"];
[request setPredicate:[NSPredicate predicateWithFormat:@"NOT (name IN %@)", namesToDelete]];
NSArray*         deleteArray = [managedObjectContext executeFetchRequest:request error:&error];

if (deleteArray != nil)
{
    for (NSManagedObject* object in deleteArray)
    {
        [managedObjectContext deleteObject:object];
    }

    [managedObjectContext save:&error];
    //### Error handling.
}
else
{
    //### Error handling.
}

另请注意,您检查数组是否作为成功标准返回,而不是在nil. 同样,save:您应该检查返回的BOOL.

于 2013-09-03T07:04:18.313 回答
4

在 iOS 9 之前,我们会一个一个地删除对象,但在 iOS 9.0+ 上,我们可以批量删除它们。

您可以在, , ,上使用NSBatchDeleteRequestavailableiOS 9.0+macOS 10.11+tvOS 9.0+watchOS 2.0+

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"NOT (name IN %@)", namesToDelete]];
NSFetchRequest *fetchRequest = [Person fetchRequest];
[fetchRequest setPredicate:predicate];
// Create batch delete request
NSBatchDeleteRequest *deleteReq = [[NSBatchDeleteRequest alloc] initWithFetchRequest:fetchRequest];
deleteReq.resultType = NSBatchDeleteResultTypeCount;
NSError *error = nil;
NSBatchDeleteResult *deletedResult = [appDelegate.persistentContainer.viewContext executeRequest:deleteReq error:&error];
if (error) {
  NSLog(@"Unable to delete the data");
}
else {
  NSLog(@"%@ deleted", deleteReq.result);
}

Swift 代码(来自上面的链接)

let fetch = NSFetchRequest<NSFetchRequestResult>(entityName: "Employee")
fetch.predicate = NSPredicate(format: "terminationDate < %@", NSDate())
let request = NSBatchDeleteRequest(fetchRequest: fetch)

do {
    let result = try moc.execute(request)
} catch {
    fatalError("Failed to execute request: \(error)")
}

笔记:

我在下面找到了关于execute的评论moc

在不影响托管对象上下文内容的情况下将请求传递给存储的方法。

这意味着任何未保存的数据moc都不会受到影响。即,如果您创建/更新了符合删除请求标准的实体并且未调用该实体,savemoc该对象将不会被删除。

于 2017-04-13T09:48:35.413 回答
1

对,就是那样。没有类似 SQL 的删除功能。

于 2013-09-02T21:28:56.940 回答
1

这就是我在 Swift 中的使用方式。

func deleteRecentWithId(recentID : String)
{
    let fetchRequest: NSFetchRequest<Recent_Message> = Recent_Message.fetchRequest()
    fetchRequest.predicate = NSPredicate(format: "recentUserOrGroupid = %@", recentID)
    let request = NSBatchDeleteRequest(fetchRequest: fetchRequest as! NSFetchRequest<NSFetchRequestResult>)

    do {
        try context.execute(request)
        try context.save()
    } catch {
        print ("There was an error")
    }

}

只需更改您的表名和谓词条件。享受!!!

于 2019-04-26T15:08:20.753 回答