What is the most efficient way to delete a large number (10.000+) objects in Core Data?

后端 未结 6 1339
青春惊慌失措
青春惊慌失措 2020-12-17 01:07

The way I\'m trying to delete multiple sets of 10.000+ NSManagedObjects is just too memory intensive (around 20MB live bytes), and my app is being jettisoned. Here is the im

6条回答
  •  攒了一身酷
    2020-12-17 01:49

    If you do not want to use another API, try another feature of NSFetchRequest, fetchLimit, maybe in conjunction with fetchOffset. I have seen this in one of the iTunes U iPad courses in an example involving heavy number crunching with Core Data.

    NSInteger limit = 500;
    NSInteger count = [context countForFetchRequest:fetch error:nil];
    [fetch setFetchLimit:limit];
    for (int i=0; i < count/limit+1; i++) {
       // do the fetch and delete and save
    }
    

    You can adjust the fetchLimit to satisfy your memory requirements.

提交回复
热议问题