Hi,
In the current version of my iOS app I've disabled live search. So you have
to type in your search term then tap on the Search button.
My app then fetches the records on the main thread and displays the results
to the customer.
However, I would like to use live search so that as they tap on the keys,
my app filters the records and displays them.
The problem is because the searching is done on the main thread, the
response while typing is "jerky" with delays as you type each character.
In a previous version of my app, I used the following code to fetch in a
background thread using an NSOperationQueue:
- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
{
self.savedSearchTerm = searchText;
__weak FormEntryListViewController *weakSelf = self;
[self.searchQueue cancelAllOperations];
[self.searchQueue addOperationWithBlock:^{
dispatch_async(dispatch_get_main_queue(), ^{
[weakSelf displayLoadingHud];
});
// Ensure you're assigning to a local variable here.
// Do not assign to a member variable. You will get
// occasional thread race condition related crashes
// if you do.
NSMutableDictionary *results = [weakSelf freshFormEntries];
[[NSOperationQueue mainQueue] addOperationWithBlock:^{
// Modify your instance variables here on the main
// UI thread.
[weakSelf.formEntries removeAllObjects];
[weakSelf.formEntries addEntriesFromDictionary:
results];
[weakSelf loadRecords];
[MBProgressHUD hideHUDForView:weakSelf.view animated
:YES];
}];
}];
}
So as you type in a search term, previous in-progress searches are
cancelled and new ones are created until finally the user stops typing and
the results are displayed on the main thread. It was fast and smooth.
However, this technique doesn't really work with Couchbase Lite because I
cannot access the model objects on the main thread those objects that I
fetched on the background thread using the NSOperationQueue.
The only thing I can think of is fetching document IDs from the search
results, then fetching again all of the model objects for those IDs in one
big fetch. The problem with this is there could potentially be thousands of
results and now I'm fetching twice.
FYI, I'm using SQLite's FTS for the searching to make searching quick. But
it still doesn't help to prevent the
Is there a better way?
Thanks,
Brendan
--
You received this message because you are subscribed to the Google Groups
"Couchbase Mobile" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/mobile-couchbase/3401cc35-8251-4aaa-9884-49314b99804b%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.