7*4 2009-09-05 15:29 采纳率: 50%
浏览 911
已采纳

核心数据: 删除一个实体的所有实例的最快方法

I'm using Core Data to locally persist results from a Web Services call. The web service returns the full object model for, let's say, "Cars" - could be about 2000 of them (and I can't make the Web Service return anything less than 1 or ALL cars.

The next time I open my application, I want to refresh the Core Data persisted copy by calling the Web Service for all Cars again, however to prevent duplicates I would need to purge all data in the local cache first.

Is there a quicker way to purge ALL instances of a specific entity in the managed object context (e.g. all entities of type "CAR"), or do I need to query them call, then iterate through the results to delete each, then save?

Ideally I could just say delete all where entity is Blah.

转载于:https://stackoverflow.com/questions/1383598/core-data-quickest-way-to-delete-all-instances-of-an-entity

  • 写回答

22条回答 默认 最新

  • local-host 2009-09-05 15:49
    关注

    iOS 9 and later:

    iOS 9 added a new class called NSBatchDeleteRequest that allows you to easily delete objects matching a predicate without having to load them all in to memory. Here's how you'd use it:

    Swift 2

    let fetchRequest = NSFetchRequest(entityName: "Car")
    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:@"Car"];
    NSBatchDeleteRequest *delete = [[NSBatchDeleteRequest alloc] initWithFetchRequest:request];
    
    NSError *deleteError = nil;
    [myPersistentStoreCoordinator executeRequest:delete withContext:myContext error:&deleteError];
    

    More information about batch deletions can be found in the "What's New in Core Data" session from WWDC 2015 (starting at ~14:10).

    iOS 8 and earlier:

    Fetch 'em all and delete 'em all:

    NSFetchRequest *allCars = [[NSFetchRequest alloc] init];
    [allCars setEntity:[NSEntityDescription entityForName:@"Car" inManagedObjectContext:myContext]];
    [allCars setIncludesPropertyValues:NO]; //only fetch the managedObjectID
    
    NSError *error = nil;
    NSArray *cars = [myContext executeFetchRequest:allCars error:&error];
    [allCars release];
    //error handling goes here
    for (NSManagedObject *car in cars) {
      [myContext deleteObject:car];
    }
    NSError *saveError = nil;
    [myContext save:&saveError];
    //more error handling here
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(21条)

报告相同问题?

悬赏问题

  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?