【问题标题】:CoreData performFetch in viewDidLoad not workingviewDidLoad 中的 CoreData performFetch 不起作用
【发布时间】:2012-07-05 07:00:48
【问题描述】:

当我的主视图控制器被加载并调用viewDidLoad 时,我正在执行一个获取请求并停用一个数组或我的核心数据对象:

+ (NSArray *)getData {

    // Fetch Data
    NSError *error = nil;
    if (![[[AppDelegate instance] fetchedResultsController] performFetch:&error]) {
        // Update to handle the error appropriately.
        NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
        abort();
    }

    NSArray *items = [[AppDelegate instance].fetchedResultsController fetchedObjects];
    return items;

}//end

由于某种原因,当从viewDidLoad 调用时,它会返回一个空数组。

如果我从viewDidAppear: 调用相同的方法,它会正常工作并返回我的CoreData 对象的NSArray

这在viewDidLoad 中不起作用有什么原因吗?

编辑:

获取结果控制器方法:

/**
 * The controller the gets our results from core data
 *
 * @version $Revision: 0.1
 */
- (NSFetchedResultsController *)fetchedResultsController {

    if (fetchedResultsController != nil) {
        return fetchedResultsController;
    }

    // Create and configure a fetch request
    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"SiteConfig" inManagedObjectContext:self.managedObjectContext];
    [fetchRequest setEntity:entity];
    // Create the sort descriptors array
    NSSortDescriptor *sectionTitle = [[NSSortDescriptor alloc] initWithKey:@"createdDate" ascending:NO];
    NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sectionTitle, nil];
    [fetchRequest setSortDescriptors:sortDescriptors];

    // Create and initialize the fetch results controller
    NSFetchedResultsController *aFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:self.managedObjectContext sectionNameKeyPath:nil cacheName:@"Root"];
    self.fetchedResultsController = aFetchedResultsController;
    fetchedResultsController.delegate = self;

    // Memory management

    return fetchedResultsController;

}//end

【问题讨论】:

    标签: objective-c ios cocoa-touch core-data nsfetchedresultscontroller


    【解决方案1】:

    我的猜测是您的视图控制器是 MainWindow.xib 的一部分,因此它的 viewDidLoad 在您的应用委托准备好核心数据上下文之前被调用。

    无论如何,您可能都想在 viewWillAppear 中运行它,以便在离开屏幕并返回时获得新数据。另一种选择是确保应用委托响应 fetchedResultsController,方法是让核心数据堆栈准备好(如果尚未准备好)。

    【讨论】:

    • 我宁愿让我的应用委托准备好它。我应该在application:didFinishLaunchingWithOptions: 中做些什么来准备fetchedResultsController 吗?
    • 我相信,你可以确认,application:didFinishLaunchingWithOptions: 在你的视图控制器的viewDidLoad 之后被调用。您现在如何准备 fetchedResultsController?
    • 我用fetchedResultsController 代码更新了我的问题。
    • 人力资源部。假设您正在使用 Apple 的 Core Data 项目模板中的 managedObjectContext 方法,这看起来不错。您是否在 application:didFinishLaunchingWithOptions: 中进行任何其他设置:可能会影响这一点?或者在任何其他视图控制器的 viewDidLoad 或 viewWillAppear 方法中?你也可以试试enabling SQL debugging 看看为什么你的 fetch 没有返回结果。
    • 是的,没有做任何奇怪的事情。我应该在什么时候进行提取?我的getData 方法不能处理吗?很困惑为什么 fetch 不会返回任何东西......
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-11
    • 1970-01-01
    • 2021-06-28
    相关资源
    最近更新 更多