加速核心数据获取

Mik*_*ael 3 iphone core-data objective-c nsfetchrequest ios

这是在iOS上.

我有一个核心数据库,有大约35万个对象.对象(Product)有两个属性:"条形码"和"指定".用户可以通过搜索"条形码"来搜索对象,并且应该返回"指定".一切都很好,除了它很慢.我使用的代码是:

    NSEntityDescription *_product = [NSEntityDescription entityForName:@"Product" inManagedObjectContext:importContext];
NSFetchRequest *fetch = [[NSFetchRequest alloc]init];

[fetch setEntity:_product];
[fetch setPredicate:[NSPredicate predicateWithFormat:@"Barcode == %@",theBarcode]];

 NSError *error = nil;
 NSArray *results = [importContext executeFetchRequest:fetch error:&error];

NSManagedObject *object = [results objectAtIndex:0];
Run Code Online (Sandbox Code Playgroud)

由于我只想获取一个对象,有没有办法加快速度?

如果我在启动时将每个对象加载到一个数组中,那么应用程序的启动速度非常慢,占用大量内存.

提前致谢!

编辑:我添加了[fetch setFetchLimit:1]; 这加快了一点点.但是数据库中对象的速度越来越慢.

pau*_*ley 7

Barcode属性是否已编入索引?