核心数据获取请求失败,并显示:'NSInternalInconsistencyException',reason:'语句仍处于活动状态' [英] Core Data fetch request fails with: 'NSInternalInconsistencyException', reason: 'statement is still active'

查看:87
本文介绍了核心数据获取请求失败,并显示:'NSInternalInconsistencyException',reason:'语句仍处于活动状态'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用包含核心日期的导航控制器模板的基本应用布局



委托:persistenceCoordinator,managedObjectModel,managedObjectContext




  • RootView:managedObjectContext(来自委托),addManagedObjectContext(使用模板中的模式),fetchResultsController


    • 添加/编辑视图(添加对象A和其B的层次结构)




对象,A&共享一对一的关系/反关系



使用模板中的模式应用程序:


  1. 创建addManagedObjectContext

  2. 插入A实体

  3. 推送Add / Edit A视图已插入的实体)

取代步骤1:






  • 将对象插入到
    数据库中(通过$ b上的sqlite3验证) $ b命令行)

  • fetchResultsController更新
    列表视图

  • 我可以关闭并打开应用程序b不会只是应用程序切换)和
    列表视图将正确获取



我可以选择添加B并添加B视图(并传递给A实体)




  • 填写B详细信息并保存

  • 将B插入到A的
    managedObjectContext中,设置
    关系,并保存上下文

  • 视图弹出,所有出现
    很好

  • 对象都在数据库
    中具有正确的关系

  • 重新载入记录并查看
    关系,进行编辑等

  • 例外:如果我关闭/重新打开应用程序,
    以触发提取,



EXCEPTION步骤2:



如果我在初始add中填写A属性和B属性




  • 完全保存A的
    上下文(如上)

  • 通过模板
    设置的委托
    关联保存A并保存结果

  • 但是fetchResultsController
    没有更新到
  • 显示新的
    记录
  • 例外:如果我关闭/重新打开应用程序,
    以触发提取,则提示的
    错误

  • 我尝试先保存A,为B添加单独的上下文等等。 m迷惑说至少。任何帮助将非常感激。



    干杯,
    Ryan

    解决方案

    这听起来像你有两个或更多的管理对象上下文,当你只需要一个。



    我不知道为什么你会在RootViewController中有一个名为 addManagedObjectContext 的方法,除了最复杂的设计,你一次只有一个上下文,它们不重叠。上下文应由应用程序委托或专用模型对象持有。每次打开特定视图时,都不会创建新的上下文。



    您可以使用不同的上下文来写入同一个永久存储,所以我想这就是为什么你看到的商店中的数据。但是,如果将一个对象放在一个上下文中的关系中,而将另一个对象放在另一个上下文中,则不能在上下文之间维护图完整性。我相信这是什么产生你的错误。


    Basic app layout using the navigation controller template with core date:

    Delegate: persistenceCoordinator, managedObjectModel, managedObjectContext

    • RootView: managedObjectContext (from delegate), addManagedObjectContext (using the pattern from the templates), fetchResultsController
      • Add/Edit view (a hierarchy for adding Object A & its B)

    I have two objects, A & B that share a 1-to-1 relationship/inverse relationship

    Using the pattern from the templates the app:

    1. Creates an addManagedObjectContext
    2. Inserts an A entity
    3. Pushes on the Add/Edit A view (having passed it the inserted entity)

    EXCEPTION STEPS 1:

    If I simply fill in the A attributes and save everything works fine.

    • The object is inserted into the database (verified with sqlite3 on the command line)
    • The fetchResultsController updates the list view
    • I can close and open the app (full close/open not just app switch) and the list view will re fetch correctly

    If I then Edit A, I can select Add B and an add B view is pushed on (and passed the A entity)

    • Fill in B details and save
    • B is inserted into A's managedObjectContext, the relationship is set, and the context is saved
    • The view pops off and all appears well
    • The objects are both in the database with the correct relationship
    • I can reload the record and see the relationship, make edits etc
    • EXCEPTION: If I close/reopen the app, so as to trigger a fetch, then the error mentioned in the title will occur.

    EXCEPTION STEPS 2:

    If I fill in the A attributes AND the B attributes in the initial "add"

    • Saving B triggers a full save of A's context (as above)
    • Saving A saves via the delegate relationship set up by the template (and merges the result into the main managedObjectContext etc)
    • The objects are inserted into the database correctly
    • However the fetchResultsController DOES NOT update to show the new records
    • EXCEPTION: If I close/reopen the app, so as to trigger a fetch, then the error mentioned in the title will occur.

    I've tried saving A first, adding a separate context for B etc etc. I'm confused to say the least. Any help would be greatly appreciated.

    Cheers, Ryan

    解决方案

    It sounds like you have two or more managed object context when you just need one.

    I don't know why you would have a method called addManagedObjectContext in the RootViewController or what it would do. In all but the most complex designs, you have only one context at a time and they don't overlap. The context/s should be held by the app delegate or a dedicated model object. You would never create a new context every time you open a particular view.

    You can have different context all writing to the same persistent store so I think this is why you are seeing the data in the store. However, you can't maintain graph integrity across context if you put one object in a relationship in one context and the other side of the relationship in another context. I believe this is what is producing your error.

    这篇关于核心数据获取请求失败,并显示:'NSInternalInconsistencyException',reason:'语句仍处于活动状态'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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