0
votes

I'm working on an app using Core Data and NSFetchedResultController but for many reasons I would like to switch it to use Realm. I saw that there is a "Realm version" of the NSFetchedResultController on github but It wouldn't be compatible with my current code using Core Data.

The view controller is displaying a list of People from the same school, like an address book. This list is a sublist of a people who studied in the same city.

So I was thinking to make 1 unique request to the database to retrieve the list of all people and then filter locally this list within 1 Dictionary per school [String: AnyObject] with String as Section name within the tableView and AnyObject an array of people.

  1. first solution, make a NSFetchedResultController for each school with a predicate filtering the location. Then all delete actions etc.. are handled by the delegates -> this is not compatible with Realm

  2. Create those dictionaries and update them for each actions... -> this works with Realm but it's very annoying to code.

Any better solution?

EDIT:

I need to clarify my request: I'd like to write a class that inherit UITableViewController.

  • This class has a list of people sorted in the alphabetical order
  • The tableview has section with the first letter of their firstname
  • The tableview controller needs to handle updates of the data model (insert, update, delete)

As we might move from CoreData to Realm, I'd like to write code that is compatible with both so that I don't need to modify it again later. The only "smart" way I found to do it so far is to forget about the NSFetchedResultController and the RBQFetchedResultsController, because they are respectively linked to CoreData and Realm, and then use data structure like Dictionaries.

1
Maybe I didn't understand the question completely but how about writing one function which receives the school you want and fetches the people from core data who are in this school? (Or doesn't this work for you?)Godlike
I d like a NSFetchedResultController but on a Dictionnary, not a database. Then I could load data from any sources and make local requests in my ViewController.Mikael

1 Answers

2
votes

Just to clarify, you're creating a UITableView with multiple sections; 1 per school, and you want to sort a flat list of people in a Realm database into the table based on their school, correct?

If that Realm fetched results controller you mentioned (I'm guessing it's RBQFetchedResultsController) doesn't fit your app's architecture, then yeah, dictionaries would be the way to go, but it shouldn't hopefully be as 'manual' as you'd think.

The good thing about Realm Results objects are that they are 'live' in the sense that if a new item is added to Realm after the Results query was made, it'll be retroactively updated to include the new item. That being the case, as long as you're managing a dictionary of Results objects that each relate to fetching the people for each specific school, the only manual aspect would be managing the table sections themselves.

The only thing to be aware of is you'll need a mechanism to be notified when a new person has been added to a specific school (in order to know to refresh that section of the table view), but for now, it would be best if you did that in your own logic (Either through a callback block, or a delegate call).

On a sidenote, we're in the middle of adding a set of new APIs to make implementing native fetched results controller behaviour possible in Realm (i.e., automating the need to post a notification when a new object is added). We haven't got a proper release date confirmed yet, but it should be within the first quarter of 2016! :)