简体   繁体   English

NSFetchedResultsController不将数据更新到UITableViewController

[英]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.

相关问题 NSFetchedResultsController,数据未更新 - NSFetchedResultsController with data not updating 在UITableViewController外部使用NSFetchedResultsController - Using NSFetchedResultsController outside of a UITableViewController UITableViewController和NSFetchedResultsController编辑问题 - UITableViewController and NSFetchedResultsController Editing Issues 使用NSFetchedResultsController的核心数据:更改后更新数据 - Core Data with NSFetchedResultsController: Updating Data After Changes NSFetchedResultsController不更新? - NSFetchedResultsController not updating? 如何将NSFetchedResultsController与UITableViewController分离? - How to decouple NSFetchedResultsController from a UITableViewController? 从UITableViewController将NSFetchedResultsController迁移到UIViewController - Migrating NSFetchedResultsController to UIViewController from UITableViewController 使用NSFetchedResultsController更新UITableView时,将数据从Web保存到核心数据 - Saving data from the web into Core Data while updating UITableView with NSFetchedResultsController 使用CoreData在UITableViewController中进行搜索:是否需要第二个NSFetchedResultsController? - Search in a UITableViewController with CoreData: Take a second NSFetchedResultsController? NSFetchedResultsController无法正确更新tableView - NSFetchedResultsController not properly updating tableView
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM