Reputation: 381
I have coredata with 450.000 records.
To show it in uitableview I am using NSFetchedResultsController. It works, but with big problem.
Before NSFetchedResultsController start works, we need to call performFetch: I my case this function works about 2-3 minutes. After that I can show data in UITable without any problems. But that 2-3 minutes killing me :(
It would not be so bad, but also I need to make search in this table. So for search I need to change predicate, call performFetch: and wait about 2-3 minutes again!
Is there anyway to make performFetch: faster? Or maybe at least somebody can tell me how to make search without call performFetch:?
Upvotes: 1
Views: 564
Reputation: 141859
2-3 minutes is a really long time to go through 450,000 records. I suspect that the most time is being spent inside some sqlite call instead of your own code. Use the Time Profiler and look at the calls that are taking the most time under the performFetch:
tree.
To see if your SQL is optimized, enable SQLite logging by adding this to the "Arguments Passed on Launch" in the Scheme settings:
-com.apple.CoreData.SQLDebug 1
See this question for more details on this.
Run your app and make note of the queries that are executed when you call performFetch:. Next, read up about EXPLAIN QUERY PLAN
. This is the single most helpful tool you can use if sqlite is the bottleneck. You'd need to grab the .sqlite file from your application (simulator or device), and run EXPLAIN QUERY PLAN on your query and see if is taking too long. If I had to guess, I'd say your query is doing a full table scan instead of using an appropriate index. A full table scan is costly.
If that is indeed the bottleneck, it could also help you optimize your searches.
Here are some helpful links.
Upvotes: 0
Reputation: 80265
2-3 minutes is definitely too long for a fetch for just 450.000 records. Are you parsing strings with a predicate? Make sure you used these optimization strategies:
fetchBatchSize
. (Try a few settings and see what works best for your data type). As for search, here is a strategy that worked for me:
Upvotes: 3