// this is called from mergeChanges: method,
// requested to be made on the main thread so we can update our table with our new earthquake objects
//
- (void)updateContext:(NSNotification *)notification
{
NSManagedObjectContext *mainContext = [self managedObjectContext];
[mainContext mergeChangesFromContextDidSaveNotification:notification];
// keep our number of earthquakes to a manageable level, remove earthquakes older than 2 weeks
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Earthquake"
inManagedObjectContext:managedObjectContext];
[fetchRequest setEntity:entity];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"date < %@", self.twoWeeksAgo];
[fetchRequest setPredicate:predicate];
NSError *error = nil;
NSArray *olderEarthquakes = [self.managedObjectContext executeFetchRequest:fetchRequest error:&error];
Earthquake *earthquake;
for (earthquake in olderEarthquakes) {
[self.managedObjectContext deleteObject:earthquake];
}
// update our fetched results after the merge
//
if (![self.fetchedResultsController performFetch:&error]) {
// Replace this implementation with code to handle the error appropriately.
// abort() causes the application to generate a crash log and terminate.
// You should not use this function in a shipping application, although it may be useful
// during development. If it is not possible to recover from the error, display an alert
// panel that instructs the user to quit the application by pressing the Home button.
//
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
abort();
}
[fetchRequest release];
[self.tableView reloadData];
}
// requested to be made on the main thread so we can update our table with our new earthquake objects
//
- (void)updateContext:(NSNotification *)notification
{
NSManagedObjectContext *mainContext = [self managedObjectContext];
[mainContext mergeChangesFromContextDidSaveNotification:notification];
// keep our number of earthquakes to a manageable level, remove earthquakes older than 2 weeks
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Earthquake"
inManagedObjectContext:managedObjectContext];
[fetchRequest setEntity:entity];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"date < %@", self.twoWeeksAgo];
[fetchRequest setPredicate:predicate];
NSError *error = nil;
NSArray *olderEarthquakes = [self.managedObjectContext executeFetchRequest:fetchRequest error:&error];
Earthquake *earthquake;
for (earthquake in olderEarthquakes) {
[self.managedObjectContext deleteObject:earthquake];
}
// update our fetched results after the merge
//
if (![self.fetchedResultsController performFetch:&error]) {
// Replace this implementation with code to handle the error appropriately.
// abort() causes the application to generate a crash log and terminate.
// You should not use this function in a shipping application, although it may be useful
// during development. If it is not possible to recover from the error, display an alert
// panel that instructs the user to quit the application by pressing the Home button.
//
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
abort();
}
[fetchRequest release];
[self.tableView reloadData];
}
本文介绍了如何在主线程中更新上下文,并通过合并更改通知来更新地震对象。重点包括删除超过两周的旧地震记录,确保数据管理效率,并在合并更改后刷新数据视图。
870

被折叠的 条评论
为什么被折叠?



