UISearchDisplayController/NSFetchedResultsController with custom sort order

I'm implementing a search feature in my app. I would like the user to look up a word simultaneously in multiple attributes of a given Entity.

Here is an example for an Entity with 3 String attributes: Person (firstName, lastName, notes)

Let's use a mock dataset with 3 people:

  • "Emily", "Bridges", "She will be in town real soon."
  • "Johnny", "Williams", "This dude is really cool."
  • "Will", "Smith", "He does not remember anything for some reason."
  • Now, let's assume the user is looking up the occurence "will" and that we run a case insensitive search. All three previously described people will match the word "will" thanks to the use of an orPredicateWithSubpredicates

    Ideally I would like the results to be displayed in this order for relevancy purposes:

  • "Will", "Smith", "He does not remember anything for some reason."
  • "Johnny", "Williams", "This dude is really cool."
  • "Emily", "Bridges", "She will be in town real soon."
  • For this search feature "firstName" is more relevant than "lastName" which are both more relevant than the "notes" attribute.

    Since I'm using a UISearchDisplayController, I also use an NSFetchedResultsController which requires an NSSortDescriptor . The problem for me now is what attribute/key I am going to use to init the NSSortDescriptor?

    I've been through many posts already and thought a transient property could help me with this issue, but I can't figure out how/when to set up this transient property which could be named something like "sortKey" and be set to these values:

  • 1: For a match on "firstName"
  • 2: For a match on "lastName"
  • 3: For a match on "notes"
  • Eventually I guess I could try to run three different requests but then I'd have to give up using NSFetchedResultsController and all its magic...

    I don't know whether I'm hitting the limits of NSFetchedResultsController or something but any pointer would be great, thanks!

    Joss.

    链接地址: http://www.djcxy.com/p/6138.html

    上一篇: 使用to来排序NSFetchedResultsController

    下一篇: UISearchDisplayController / NSFetchedResultsController具有自定义排序顺序