问题
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!
回答1:
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.
回答2:
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.
来源:https://stackoverflow.com/questions/11811996/ios-how-to-refresh-update-core-data-transient-property