开发者

Speed up Core Data fetching

This is on iOS.

I have a Core Database with about 350 000 objects. The objects (Product) have two properties: "Barcode" and "Designation". The user can search for an object by searching for the "Barcode", and the "Designation" should be returned. Everything is working fine, except it's slow. The code I use is:

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

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

 NSError *error = ni开发者_StackOverflow中文版l;
 NSArray *results = [importContext executeFetchRequest:fetch error:&error];

NSManagedObject *object = [results objectAtIndex:0];

Since I only want to fetch one object, is there a way to speed it up?

If I load every object into an Array at the start-up I get a very slow start-up for the app and taking a lot of RAM.

Thanks in advance!

EDIT: I added [fetch setFetchLimit:1]; which speed it up a little bit. But the speed is getting slower the further down in the Database the object is.


Is the Barcode attribute indexed?


First, as @paulbailey wrote, check if Barcode is indexed.

But, if you have that many entries, and if your entry only has two properties (barcode and designation), and if you only query from the barcode side and return the designation side, using CoreData might be an overkill.

CoreData gives you a lot of object-oriented facilities with persistence to the disk, but it of course comes with a penalty.

It might be better for you to drop CoreData altogether, and use sqLite directely. There's a nice light-weight Objective-C wrapper called FMDB for that, see here.

If you want to stick to CoreData, one way to make things better is to fetch in the background thread and to show the result in the main thread, as described in this Apple document. This way the UI doesn't freeze while the database is searched.


The reason why it takes longer the further down the database the object is, is that Core Data uses a rather dull search algorithm which just places a pointer to the first object, comprehends its value to the searchitem, places the pointer to the next one and so one untill the comparison matches.

There are tons of search algorithms you can use, depending on your database (sorted/ not sorted lists, tree structure etc.) you could use Quicksearch, Hashsearches, treesearch and so on.

You might also think about setting up a SQlite database instead, which has some nice frameworks with intelligent search algorithms.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜