如何提高Core Data的性能? [英] How to improve Core Data performance?

查看:99
本文介绍了如何提高Core Data的性能?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程式有一个UISearchBar,让使用者输入搜寻关键字。每个键击都会执行核心数据查询,以便在搜索栏更改时以文本形式显示结果。

My app has a UISearchBar allowing user to enter search keywords. Each keystroke executes a Core Data query in order to display the results as text in search bar changes.

问题是搜索栏的击键相当糟糕...肯定因为缓慢获取。任何想法如何提高性能?

The problem is that search bar keystrokes are lagging pretty bad... surely because of slow fetching. Any ideas how to improve the performance?

我的核心数据支持包含1000个对象的sqlite数据存储。

My Core Data is backed with sqlite data store which contains 1000 objects.

// searchKeyword is the string appears in UISearchBar
// Both title and author may contain several words so I can't use BEGINSWITH
NSPredicate* predicate = [NSPredicate predicateWithFormat:@"(author CONTAINS[c] %@) OR (title CONTAINS[c] %@)", searchKeyword, searchKeyword];

NSEntityDescription* entity = [NSEntityDescription entityForName:@"Book" inManagedObjectContext:managedObjectContext];

NSFetchRequest* request = [[NSFetchRequest alloc] init];
[request setEntity:entity];
[request setPredicate:predicate];
[request setFetchLimit:10];

NSSortDescriptor* sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"title" ascending:YES];
NSArray* sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor, nil];
[request setSortDescriptors:sortDescriptors];

[sortDescriptor release];
[sortDescriptors release];

execute request and fetch the results
fetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:request
                                managedObjectContext:managedObjectContext
                                sectionNameKeyPath:nil
                                cacheName:nil];
NSError* error = nil;
BOOL success = [fetchedResultsController performFetch:&error];
[request release];


推荐答案

使用CONTAINS会减慢速度。您需要做的是创建一个名为searchWords(或其他)的新表,并在该商店中的所有标题内的字,小写和删除重音。这些具有将它们链接回原始对象的关系。

Using CONTAINS slows it down. What you need to do is create a new table called searchWords (or whatever), and in that store all the words within your titles, made lower case and with accents removed. These have a relationship linking them back to the original objects. Make sure the word field is indexed.

对此表执行查询,但不使用CONTAINS或BEGINSWITH,请执行类似

Perform your query on this table, but instead of using CONTAINS or BEGINSWITH, do something like

word>termAND word< tern

word > "term" AND word < "tern"

请注意,第一个字符串是搜索字词,第二个字符是最后一个字符递增的搜索字词。这允许Core Data使用SQL索引执行搜索。

Note that the first string in there is the search term, and the second is the search term with the last character incremented. This allows Core Data to use the SQL index to perform the search.

Apple有一个 Core Data 解释此问题的WWDC会话,包括示例代码。示例代码包含一个类,用于处理对字符串进行规范化(即删除大小写),并以Unicode编码方式增加单词的最后一个字符。

Apple have a Core Data WWDC session that explains this, including sample code. The sample code contains a class that handles normalising the string (I.e. Removing case), and incrementing the last character of a word in a unicode aware way.

这篇关于如何提高Core Data的性能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆