Search code examples
iosuitableviewcore-datatransientnsfetchedresultscontroller

iOS - core data - UITableView - update calculated value - NSFetchedResultsController


Overview

  • I have an iOS project using core data (NSFetchedResultsController) and table view. I have a field called "joingDate".

  • I have a set of labels on the table view cell to represent the data.

  • If the joiningDate is today / tomorrow, then I would like to display as "today" or "tomorrow" instead of specifying the actual date.

  • I was just thinking, I would check for the lastCheckedDate (last time when the date was checked) and if it over a day, then I would have to update all those labels in the rows which have a joining date and "today" or "tomorrow".

  • In the database the "JoiningDate" would be stored as a Date, only the label needs to display "today" / "tomorrow" / date in a specific format.

I have implemented, the following method, where the label "joingDateComments" is updated based on it's current date.

- (UITableViewCell *)tableView:(UITableView *)tableView 
         cellForRowAtIndexPath:(NSIndexPath *)indexPath

Question

Note - I am using NSFetchedResultsController

  1. what is the best approach to update all those rows labels which have a joiningDate as today / tomorrow ?
  2. Should I reload all the records in the table ? (but this would only affect a few rows.)
  3. Should I loop through all indexes and check and update. I am not sure this would work because I am using NSFetchedResultsController.
  4. Should I use transient data, so that I can just query the database and get the records which have a "JoingDate" as today / tomorrow and update the transient field JoingDateComments, so that the those records would automatically be refreshed by the NSFetchedResultsController

Answer:

Pls go through Andrew's comments.


Solution

  • user1046037,

    This really isn't a Core Data question but a UITableView question. Most everything is handled in the -tableView:cellForRowAtIndexPath: method. There you will choose how you represent the cell. The other place will be when the NSFetchedResultsController calls you with the changed objects. (The standard Core Data table view controller writes this boiler plate for you.) In other words you should not have to loop over all of your objects. If you want finer grain control of when these cells are updated, you can listen for the did save notification. Then query the tableView for the array of visible cells. The cell index paths can then be used with to update the cell contents.

    Andrew