简体   繁体   中英

Multiple NSFetchedResultController for a single UITable and CoreData Entity design

I am developing an iPad application which stores details about locations which shows in a tableview. User would be able to sort the locations by All (Single section with alphabetical order), Region wise (Each region as section) and Type wise (Each type as section). I am using separate NSFetchedResultController for each as the 'sectionNameKeyPath' has to be given while initialization. I am having 2 problems now. Spent more than 1 week searching but the solutions I found creating more related issues.

1 ) For any update (eg: set a location as favorite) gives NSFetchedResultsChangeDelete instead of NSFetchedResultsChangeUpdate.

  • Many forum says its a known bug if we use multiple fetchedResultController
  • Only way I could avoid is not using delegate methods and to fetch again for each change. So missing the tableview animation solely.
  • Not all update gives delegate call back. Am I going in right way or is there a better way that I can use single FetchedResultController?

2) tableview sorting for Types cannot use FetchedResultController efficiently with current Entity design. Current design use entities Location, Types and Region. Location to Region is a one-to-many relation. But Location-to-Types is a many-to-many relation in both direction. [I tried to upload entity design image but my reputation does not allows that.]

  • Types to Location relation is many-to-many in both direction. So cannot give sectionNameKeyPath because types is a Set in Location entity. So am fetching from Types entity and getting location array(set) from that.
  • So numberOfRows can be calculated from set.But its not ordered. so I am sorting it using comparator. Am not sure this is the right way, so am asking all of you to give your valuable suggestions about this Entity design.

Not quite sure but, CoreData has some set of flaws when it comes to notification of changes (which is a great exclusive feature) but I found that NSFetchedResultsChangeDelete doesn't necessarily mean that an object has been deleted from the database, it means that the object is no longer available on the fetched objects either because it was deleted or it no longer fits the current predicate (conditions). You should check into that.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM