在视图初始化时无法访问受管对象上下文

编程入门 行业动态 更新时间:2024-10-09 06:22:57
本文介绍了在视图初始化时无法访问受管对象上下文的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我使用的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 。

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

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

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

    • 调用它。

      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:

    • 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.

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

更多推荐

在视图初始化时无法访问受管对象上下文

本文发布于:2023-11-09 07:59:52,感谢您对本站的认可!
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:上下文   视图   初始化   无法访问   对象

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!