在视图初始化时无法访问受管对象上下文 [英] Can't access managed object context when view is initializing

查看:238
本文介绍了在视图初始化时无法访问受管对象上下文的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的MainWindow.xib文件具有以下配置:



MainWindow.xib




  • UITabBarController

    • UINavigationBarController

      • UITableViewControllerAlpha


    • UINavigationBarController

      • UITableViewControllerBeta


    • UINavigationBarController

      • UITableViewControllerCharlie





    • 但基于以下帖子:
      为什么当我尝试在这些点上执行时,Core Data初始化失败?
      和断点我已经做了,我得出结论,initWithCoder被用于初始化所有的UITableViewControllers,并且没有办法在应用程序生命周期的那一点可靠地使用managedObjectContext。



      这是否意味着我必须抛弃我在MainWindow.xib中执行的所有简单设计工作和布局,并编程?使用核心数据,真的意味着不能使用NIB文件?

      解决方案



      将Core Data,表视图和NIB一起使用没有问题。我假设你需要从你的表视图中获取一些对象从MOC,并且你有困难找到MOC时,你需要这样做。



      这种方法工作对我来说:




      • 定义nib作为目标的主界面。

      • 加载nav控制器和根视图控制器 c> / li>
      • 在根视图控制器的 viewDidLoad 中从MOC提取



      所以,请先尝试:




      • 让应用程序委托在中设置控制器的managedObjectContext applicationDidFinishLaunching: / code>

      • viewDidLoad 中执行提取,而不是 initWithCoder



      如果这不起作用,您至少仍然有以下选项:


      1. applicationDidFinishLaunching:中设置控制器的managedObjectContext,但在调用数据源方法之前不要提取数据。


      2. 编写一个类似 loadDataFromMOC:在您的控制器,这两个设置MOC和从它的抓取,并从 applicationDidFinishLaunching:


      3. 调用它。


      I am using a MainWindow.xib file that has the following configuration:

      MainWindow.xib

      • UITabBarController
        • UINavigationBarController
          • UITableViewControllerAlpha
        • UINavigationBarController
          • UITableViewControllerBeta
        • UINavigationBarController
          • UITableViewControllerCharlie

      But based on the following post: Why does Core Data initialization fail when I attempt to do it at these points? and the breakpoints that I've placed, I've come to the conclusion that initWithCoder is being used to init all of the UITableViewControllers and that there is no way to reliably use the managedObjectContext at that point in the applicaiton lifecycle.

      So does this mean that I have to throw out all the "easy" design work and layout that I've performed in MainWindow.xib and do it programatically? Does using core data, really mean not being able to use NIB files? Or is there some middle-ground?

      Please let me know, Thanks!

      解决方案

      There's no problem using Core Data, table views, and NIBs together. I presume you need to fetch some objects from the MOC for your table view, and you're having problems finding the MOC when you need to do that.

      This approach works for me:

      • Define nib as target's main interface.
      • Load nav controller and root view controller in nib.
      • Set root view controller's managedObjectContext property in applicationDidFinishLaunching:
      • Fetch from MOC in root view controller's viewDidLoad

      So, try this first:

      • Have the app delegate set the controller's managedObjectContext in applicationDidFinishLaunching:
      • Do the fetch in viewDidLoad instead of initWithCoder.

      If that doesn't work, you still have at least these options:

      1. Set the controller's managedObjectContext in applicationDidFinishLaunching:, but don't fetch data until your data source methods are called. In those methods, conditionally complete the fetch if it hasn't already been done.

      2. Write a method like loadDataFromMOC: on your controller, which both sets the MOC and fetches from it, and call it from applicationDidFinishLaunching:.

      这篇关于在视图初始化时无法访问受管对象上下文的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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