re: Search on Core data Table issue?
re: Search on Core data Table issue?
- Subject: re: Search on Core data Table issue?
- From: Ben Trumbull <email@hidden>
- Date: Thu, 22 Oct 2009 14:39:07 -0700
Not sure if this is the right place (I am sure someone will let me
know if it is not) I have a iPhone application that has a UITable
view that is backed by core data. I want to perform a reducing search
so that only the items starting with the characters entered into the
search bar are shown. This is normally done with delegate - (void)
searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)
searchText no problem. I am a little confused as I am new to Core
Data how to do this. One of the big problems as I see it is going to
be updating the interface to let it know what to present. I assume an
alternative NSFetchedResultsController needs to be sent to the
UITableView.
You're basically on the right track.
So here are my issues:
1) I assume I need to create a NSFetchedResultsController with only
the correct items in it then tell the UITableView to use this as the
dataSource and reload the table?
Yes. Most of the Apple apps use an overlay, so it's also a different
UITableView entirely. See search in the Contacts app, for example.
2) is there a better way than executing a full sorted fetch and
removing those objects that do not conform. ie is there a way of
doing a select where type fetch?
I'm not sure exactly what you mean by "select where type" fetch. What
type ?
The typical pattern is if the user typing has gone from a 0 length
string to a 1 character string, you'll need to do a fetch. (that
includes the user deleting a character and starting over). If the
string already has 1 or more characters, you can choose between a new
fetch, or simply an in memory filter of the previous results. The
results should always be sorted, so you can binary search the range to
filter, instead of filtering all the strings.
The balance of when to fetch and when to filter is complicated, and
depends on your data set, as well as your UI. If you have an
extremely large data set, you should use a fetch limit, so that the
user cannot get back all the results until they've entered a
sufficiently discriminating search string. If the number of results
you get back is equal to the fetch limit, you can consider executing a
count request or just saying "and more...".
If the number of results is small, then it's much faster to filter the
old results in memory. Fetching will save you a lot of memory, but it
is I/O so it has a high fixed cost (like ~2ms on a desktop). You'll
want to balance the two. You might start out with a threshold of 100
and tune from there.
Thanks in advance and sorry if this is a dumb question.
It's not.
- Ben
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden