删除持久存储并将其添加到核心数据应用程序 [英] Removing and adding persistent stores to a core data application

查看:109
本文介绍了删除持久存储并将其添加到核心数据应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在iPhone应用程式中使用核心资料。我有多个persisntent存储,我从一个切换到另一个,所以只有一个商店可以是活动的时间。我有一个管理对象上下文,不同的持久存储在数据格式(sqlite)类似,并共享相同的管理对象模型。



我将数据导入到每个从相应的XML文件持久存储。对于第一次导入一切工作正常,但我删除导入的数据(持久存储和物理文件),然后重新导入,核心数据给我一个错误:

  ***由于未捕获异常NSObjectInaccessibleException终止应用程序,原因:'NSManagedObject ID为0x3c14e00   

此错误来自NSManagedObjectContext的save:。在重新导入之前,我从永久存储协调器中删除持久存储,并删除物理文件,所以一切都应该像第一次重新导入。 Alos,托管对象上下文中的对象被删除,上下文被发送reset:message(我不知道这是否真的需要)。



帮助我在这里?如何切换永久存储?



我基本上使用与tutored相同的逻辑: http://blog.sallarp.com/iphone-core-data-uitableview-drill-down/



提前感谢。



目标更新



谢谢你为你的答案。



对我的目标模糊不清的道歉。我正在开发一个圣经读者应用程序,它将翻译从XML导入到核心数据SQL。目前只有一个翻译可以使用。目前我只有不同的MOC和PS每个翻译,因为模型是相同的。然而,我相信,如果你说堆栈应该作为一个整体创建。翻译没有在彼此之间的连接,所以没有使用相同的堆栈的实际原因。唯一可能使这复杂化的是将引用主动翻译的笔记/书签等。然而,引用将是文本,所以再次没有必要需要一个共享堆栈。



感谢您的输入。


<我同意Louis,你正在做的是一个危险的设计,你应该重建整个堆栈,而不是像这样交换出来的商店。重建整个堆栈不是一个巨大的开销。



此外,如果你使用完全不同的模型与完全不同的数据对象,你不需要删除它们,你可以将所有模型一起添加到一个持久化存储,并根据您创建的实体,Core Data将做正确的事情。



如果您正在构建多个模型, em>相同实体,那么问题是为什么



更新



在您描述的情况下我将所有的存储放入一个单一的持久化存储协调器,然后指示上下文哪个存储使用 -assignObject:toPersistentStore:保存实体。这将消除建立和拆除多个Core数据堆栈的需要。



更新2



堆栈是整个堆栈。 PSC,MOM和MOC。



你的最终目标是什么?

你的目标是什么?

后期更新




Um,因此,只有创建PS MOC并使用现有的PSC和MOM?如果我猜,我会说,每次我对现有的堆栈进行修改,它应该重新创建,如果使用多个商店,他们应该在建设时引入。


NSManagedObjectModel NSPersistentStoreCoordinator NSPersistentStore 绑在一起相当紧密,所以不清楚你的目标是在这里。



有多个 NSManagedObjectContext 实例通常在处理多线程和几个非常罕见的边缘情况时使用。有多个 NSPersistentStore 实例通常用于当您需要为某种原因(部分只读,等)需要silo'ed的数据。



我的问题仍然存在,你想要完成什么?



更新目标




我正在开发一个将从XML转换为核心数据SQL的圣经阅读器应用程序。目前只有一个翻译可以使用。


好的,你不需要为每个翻译单独的文件,这是一个浪费。由于每个翻译由代码(如ASM)和名称,版权等标识,您可以在您的设计中创建一个顶级实体,称为翻译,其余的数据分支从那里。没有多个上下文,没有切换的东西,它只是工作。



只有你需要多个文件是,如果你是一个多文档设计,这不是什么你做在iPhone。


I'm using core data on an iPhone application. I have multiple persisntent stores that I'm switching from one to another so that only one of the stores can be active at the time. I have one managed object context and the different persistent stores are similar in data format (sqlite) and share the same managed object model.

I'm importing the data to each persistent store from a respective XML file. For the first import everything works fine, but after I remove the imported data (the persistent store and the physical file) and then re-import, core data gives me an error:

*** Terminating app due to uncaught exception 'NSObjectInaccessibleException', reason: 'The NSManagedObject with ID:0x3c14e00 <x-coredata://6D14F11E-2EA7-4141-9BE8-53747DE6FCC6/Book/p2> has been invalidated.'

This error comes from the save: of NSManagedObjectContext. Before re-importing, i'm removing the persistent store from the persistent store coordinator and removing the physical file, so everything should be as if re-importing was done for the first time. Alos, the objects in managed object context are removed and the context is sent the reset: message (I don't know if this is actually needed).

Could some one help me out here? How should the persistent store be switched?

I'm basically using the same logic as tutored here: http://blog.sallarp.com/iphone-core-data-uitableview-drill-down/

Thanks in advance.

Update on the goal

Thank you for your answers.

Apologies for the vagueness on my goal. I'm developing a Bible reader application that would import translations from XML to core data SQL. Currently only one translation can be in use. Currently I only have different MOC and PS for each translation since the model is the same. I do believe, however, if you say that the stack should be created as a whole. The translations have no connections between each other, so there is no actual reason for using the same stack. The only thing that could complicate this would be notes/bookmarks/etc that will reference to the active translation. The references, however, will be textual so again there is no necessary need for a shared stack.

Thank you for your input.

解决方案

I agree with Louis, what you are doing is a dangerous design and you should be rebuilding the entire stack instead of swapping out the stores like that. Rebuilding the full stack is not a huge overhead.

Also, if you are using completely different models with completely different data objects you don't need to remove them, you can add all of the models together into one persistent store and based on which entity you create, Core Data will do the right thing.

If you are building multiple models with the same entity then the question is why? You are not going to get any performance benefits out of it.

Update

In the situation you have described I would put all of the stores into a single persistent store coordinator and then instruct the context which store to save the entity with -assignObject:toPersistentStore:. That will eliminate the need to build up and tear down multiple Core Data stacks.

Update 2

A new stack is the entire stack. The PSC, the MOM and the MOC. You are pulling the rug out from under the Core Data stack and then expecting it to just figure out what you are doing.

What is your end goal?

Late Update

Um, so what would be an acceptable scenario for creating only the PS and MOC and using existing PSC and MOM? If I guessed, I'd say that every time I made modifications to the existing stack, it should be created anew and that if multiple stores were used, they should be introduced at the time of the construction.

The NSManagedObjectModel, NSPersistentStoreCoordinator and NSPersistentStore are tied together rather tightly so it is unclear what your goal is here. If you update your question with what you are trying to accomplish it will make answers a lot less vague.

Having multiple NSManagedObjectContext instances is normally used when you are dealing with multiple threads and a few very rare edge cases. Having multiple NSPersistentStore instances is normally used when you have data that needs to be silo'ed for one reason or another (partially read only, etc.). Those are the normal use cases.

My question still stands though, what are you trying to accomplish?

Update on goal

I'm developing a Bible reader application that would import translations from XML to core data SQL. Currently only one translation can be in use.

Ok, you do not need separate files for each translation, that is a waste. Since each translation is identified by a code (such as ASM) and name, copyright, etc. you can simply create a top level entity in your design that is called ... "Translation" and the rest of the data branches from there. No multiple contexts, no switching things around, it just works.

The only time you need multiple files is if you are in a multi-document design which is not something you do on the iPhone.

这篇关于删除持久存储并将其添加到核心数据应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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