Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS - How to refresh/update Core Data transient property?

I'm using a core data, NSFetchedResultsController UITableView, with a transient NSDate attribute. The main reason I have this as a transient property is so my UITableView entries get put into sections based on NSDate, but can move between the sections when the date changes.

So far it seems to work great, but it only updates/refreshes (I'm really new to this, so I don't know if I'm using the correct terminology, sorry!) when I either close the app and kill it from multitasking, or re-run it through Xcode. If I don't do that, the items don't change and aren't put into their correct sections. Is there a way to manually get it to refresh so the user doesn't need to do that to get it to run right?

Thank you!

like image 470
mhbdr Avatar asked Aug 04 '12 21:08

mhbdr


People also ask

How to use Transformable attribute in Core Data?

To implement a Transformable attribute, configure it by setting its type to Transformable and specifying the transformer and custom class name in Data Model Inspector, then register a transformer with code before an app loads its Core Data stack.

What is transient property in Core Data?

Transient attributes are properties that you define as part of the model, but that are not saved to the persistent store as part of an entity instance's data. Core Data does track changes you make to transient properties, so they are recorded for undo operations.

What is Core Data stack?

As I mentioned earlier, the Core Data stack is the heart of Core Data. It's a collection of objects that make Core Data tick. The key objects of the stack are the managed object model, the persistent store coordinator, and one or more managed object contexts.

How do I create a Core Data class?

You create and maintain your class, including its properties, manually. Core Data then locates these files using the values you supply in the class name and module fields. To generate the class and properties files initially: From the Xcode menu bar, choose Editor > Create NSManagedObject Subclass.


2 Answers

First, make sure that you transient property is only used for the sectionNameKeyPath when creating your fetched results controller. Best name it sectionIdentifier (as Apple does in their sample code.) The actual date should be a separate attribute of your entity. (I will call it dateAttribute.

Second, make sure that you specify the key path dependencies in your Entity.m file:

+ (NSSet *)keyPathsForValuesAffectingSectionIdentifier {
    // If the value of dateAttribute changes, the section identifier may change as well.
    return [NSSet setWithObject:@"dateAttribute"];
}

Third, make sure that in your controller, you react appropriately to changes in the managed object context through

- (void)controllerDidChangeContent:(NSFetchedResultsController *)controller
{
   if (!self.tableView.editing) [self.tableView reloadData];
   // the quick and dirty method without animations;
   // see referenced code for a more pleasant approach
}

If anything is unclear, take a look at the Apple example DateSectionTitles.

like image 57
Mundi Avatar answered Sep 27 '22 17:09

Mundi


Transient properties are refreshed when you send a refreshObject:mergeChanges: to your object.

The solution provided by Mundi to apply key value observing mechanism might work too and if it does, it is definitely much more convenient than refreshing explicitly.

like image 35
svena Avatar answered Sep 27 '22 18:09

svena