[英]NSFetchedResultsController not updating data into UITableViewController
I have the function: 我有功能:
- (void)initFetchRequest
{
NSManagedObjectContext *context = self.managedObjectContext;
NSFetchRequest *request = [[NSFetchRequest alloc] init];
request.entity = [NSEntityDescription entityForName:@"Shindy" inManagedObjectContext:self.managedObjectContext];
// Configure the request's entity, and optionally its predicate.
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"details" ascending:YES];
NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor, nil];
[request setSortDescriptors:sortDescriptors];
fetchedResultsController = [[NSFetchedResultsController alloc]
initWithFetchRequest:request
managedObjectContext:context
sectionNameKeyPath:nil
cacheName:nil];
NSError *error = nil;
[self.managedObjectContext executeFetchRequest:request error:&error];
[fetchedResultsController performFetch:&error];
if (![self.fetchedResultsController performFetch:&error]) {
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
}
}
With the following within my viewDidLoad
method 在我的viewDidLoad
方法中包含以下内容
NSURL *url = [NSURL fileURLWithPath:[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject]];
document = [[UIManagedDocument alloc] initWithFileURL:url];
self.managedObjectContext = document.managedObjectContext;
[self initFetchRequest];
And finally, this within my cellForRowAtIndexPath
最后,这在我的cellForRowAtIndexPath
Shindy *shindy = [fetchedResultsController objectAtIndexPath:indexPath];
cell.textLabel.text = shindy.details;
Data is being inputted from another controller, and when I segue back to this controller, the UITableViewController
isn't updating. 数据是从另一个控制器输入的,当我重新选择该控制器时, UITableViewController
不会更新。
What am I missing here? 我在这里想念什么?
If you are coming back from another View Controller (eg with pop in a navigation controller), your viewDidLoad
is not called. 如果您是从另一个View Controller(例如,导航控制器中带有pop)返回的,则不会调用viewDidLoad
。 Therefor your data will not be updated. 因此,您的数据将不会更新。 If you want that to happen, you should implement the NSFetchedResultsControllerDelegate and set fetchedResultsController.delegate = self;
如果你想这样的事情发生,你应该实现NSFetchedResultsControllerDelegate并设置fetchedResultsController.delegate = self;
. 。 It will update your table whenever a change occurs. 每当发生更改时,它将更新您的表。 There is a less elegant solution, to call your initFetchRequest
method from viewWillAppear
but I would recommend the first solution. 有一个不太initFetchRequest
解决方案,可以从viewWillAppear
调用initFetchRequest
方法,但我建议使用第一个解决方案。 Hope this helps! 希望这可以帮助!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.