核心数据 - 在viewDidLoad之前调用数据源方法 [英] Core Data - Datasource methods is called before viewDidLoad

查看:102
本文介绍了核心数据 - 在viewDidLoad之前调用数据源方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我注意到我的应用程序中的一个问题是因为 UITableView 的数据源方法在之前调用viewDidLoad

I have noticed that a problem in my app is caused because the Datasource methods of the UITableView are called before viewDidLoad.

问题是 UITableView 没有正确的行数,从 NSFetchedResultsController 中调用,但是 viewDidLoad中调用 performFetch

The problem is that the UITableView doesn't have the correct amount of rows, it gets the amount of rows from the NSFetchedResultsController but the performFetch "function" for that is called in the viewDidLoad method which for some reason is called after the Datasource methods.

以下是两个重要方法的来源:

Here's the source of the two important methods:

- (void)viewDidLoad {
    [super viewDidLoad];

 self.tableView.allowsSelectionDuringEditing = NO;
 self.tableView.editing = YES;
 self.title = [NSString stringWithFormat:@"%@", [[game valueForKey:@"title"] description]];

    NSError *error = nil;
    if (![[self fetchedResultsController] performFetch:&error]) {
        NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
        abort();
    }

}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    id <NSFetchedResultsSectionInfo> sectionInfo = [[fetchedResultsController sections] objectAtIndex:section];
    return [sectionInfo numberOfObjects];
}

如何解决这个问题?

推荐答案

正常的程序流程要求调用 -viewDidLoad 之前的任何 UITableView 数据源方法。如果你以相反的顺序得到它们,那么在你的代码库中有一些东西会导致它们被乱序调用。

The normal program flow calls for the -viewDidLoad to be called before any of the UITableView data source methods. If you are getting them in the reverse order then there is something in your code base causing them to be called out of order.

首先,在两个

然后,查看堆栈跟踪,当你在这个问题中显示的方法和确认 -tableView:numberOfRowsInSection:断点并查看为什么之前调用 -viewDidLoad

Then, look at the stack trace when you are in the -tableView:numberOfRowsInSection: breakpoint and see why it is being called before the -viewDidLoad.

-viewDidLoad最肯定是先被调用,如链接所示。当你在堆栈跟踪中选择 [PlayerViewController -viewDidLoad] 行时,它坐在什么代码行?如果它坐在 [super viewDidLoad] 那么什么是你的超类,什么是 做它 -viewDidLoad

The -viewDidLoad is most definitely being called first as shown in the link. When you select the [PlayerViewController -viewDidLoad] line in that stack trace, what line of code is it sitting on? If it is sitting on [super viewDidLoad] then what is your superclass and what is it doing it's -viewDidLoad?

这篇关于核心数据 - 在viewDidLoad之前调用数据源方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆