dimanche 18 octobre 2015

CoreData search too slow

I am working on an iPhone app that searches through 91,000 entries. I am using CoreData, which works beautifully, except that it takes about 2 seconds to create the table of data. I have programmed it to do the search in the background, and added a spinner, but the user experience is still not ideal. I tried using FMDB which worked fine but wasn't any faster.

Essentially, I created my xcdatamodel in swift, added my entries to it, which works fine. (The code below is typed freehand, so sorry if there are typos)

I then create an NSFetchedResultsController:

let moc =(UIApplication.sharedApplication().delegate as! AppDelegate).managedObjectContext
searchText: String? = "name contains[c] \"seachText\""
nsfr = NSFetchRequest(entityName: "Example")
nsfr.fetchBatchSize = 20
nsfr.predicate = NSPredicate(format: searchText!)
nsfrc = NSFetchedResultsController(fetchRequest: nsfr, managedObjectContext: moc, sectionNameKeyPath: "section", cacheName: nil)

Then in my UITableViewController class (which conforms to NSFetchedResultsControllerDelegate):

nsfrc.delegate = self
nsfrc.performFetch()
self.tableView.reloadData()

Is there any way to speed up requests, like hashing the database? Any way to narrow search to the start of words? My data includes thousands of sentences, and I want to be able to search for parts of strings within. For example, "laz fo" should find "The lazy brown fox". Right now "azy own" will also find the string, which is not necessary. Any help would be appreciated! Even cutting the time in half would be wonderful.

Aucun commentaire:

Enregistrer un commentaire