使用 ModalView 通过 CoreData 保存数据
我的应用程序只是一个数据库,用户只需在其中搜索一些科学数据。它的构建如下:
- 它必须是可由 TabBarViewController 选择的“主”视图。 - 第一个视图是用户刚刚进行搜索查询的位置。 -第二个视图只是一些设置、信息和免责信息。
我使用第二个视图来预填充将随应用程序一起提供的数据库。现在它已填充,我将实际实现设置、信息等。
ManagedObjectContext 在 AppDelegate (applicationDidFinish...) 中设置,如下所示:
firstViewViewController.managedObjectContext = self.managedObjectContext;
secondViewController.managedObjectContext = self.managedObjectContext;
saveCustom.managedObjectContext = self.managedObjectContext;
self.window.rootViewController = self.tabBarController;
[self.window makeKeyAndVisible];
return YES;
ManagedObjectContext 在 secondaryViewController 中声明并正确合成。
到目前为止,该应用程序运行正常,我可以将项目保存到数据库中。
现在,我创建了一个从firstViewController 调用的modalView。我希望用户能够保存自定义数据。为此,我复制了在 seconViewController 中所做的操作,并在 AppDelegate 中添加了相应的代码(或我认为的代码)(见上文)。
当我尝试使用 modalVIew 保存数据时,出现以下错误。
* 由于未捕获的异常“NSInternalInconsistencyException”而终止应用程序,原因:“+entityForName:无法找到实体名称 XXX 的 NSManagedObjectModel,
其中 XXX 是我的实体的正确名称。
我知道这与 ManagedObjectContext 未初始化有关。但我不明白为什么它在 seconViewController 上工作而不在 modalViewController 上工作。
那么,我怎样才能让它发挥作用呢?
我知道将 AppDelegate 导入到 modalView 中,虽然它可以工作,但这不是一个好的做法。
提前致谢!
编辑:
我用来保存数据的代码如下:
-(void)saveDataToCD{
NSString *entityString=[NSString stringWithFormat:@"%@",[arrayAdquiritDelMV objectAtIndex:1]];
NSString *deltaString=[NSString stringWithFormat:@"%@",[arrayAdquiritDelMV objectAtIndex:3]];
NSManagedObjectContext *context = [self managedObjectContext];
NSManagedObject *pepe = [NSEntityDescription insertNewObjectForEntityForName:entityString inManagedObjectContext:context]; // aqui un string
[pepe setValue:assigment.text forKey:@"definition"];
[pepe setValue:impurity.text forKey:@"impurity"];
[pepe setValue:[NSNumber numberWithDouble:[delta.text doubleValue]] forKey:deltaString]; //aquí un string
NSError *error;
if (![context save:&error])
{
NSLog(@"Problem saving: %@", [error localizedDescription]);
}}
编辑#2,这是我调用模态视图的方式:
-(IBAction)saveCustom:(id)sender{
if(saveCustomController == nil)
{
SaveCustom *viewTwo = [[SaveCustom alloc] initWithNibName:@"SaveCustom" bundle:[NSBundle mainBundle]];
self.saveCustomController = viewTwo;
[viewTwo release];
}
[self presentModalViewController:self.saveCustomController animated:YES];
}
My app is just a DB where the user just search some scientifical data. It is build like this:
-It has to "main" views selecteable by a TabBarViewController.
-The first View is where the user just proceeds to the search query.
-The secon view are just some settings, info and disclaimer information.
I used the second view to pre populate the DB which will be shipped along with the app. Now that it's populated, I'm going to actually implement the settings, info and so on.
The managedObjectContext is set up in the AppDelegate (applicationDidFinish...), like this:
firstViewViewController.managedObjectContext = self.managedObjectContext;
secondViewController.managedObjectContext = self.managedObjectContext;
saveCustom.managedObjectContext = self.managedObjectContext;
self.window.rootViewController = self.tabBarController;
[self.window makeKeyAndVisible];
return YES;
The managedObjectContext is declared ans synthesized properly in the secondViewController.
The App, so far, works OK and I can save items in to the DB.
Now, I created a modalView which is called from the firstViewController. I want the user to be able to save custom data. To do that, I duplicated what I've done in the seconViewController and I added the corresponding code (or what I think it is) in the AppDelegate (see above).
When I try to save data using the modalVIew, I'm getting the following error.
* Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: '+entityForName: could not locate an NSManagedObjectModel for entity name XXX
where XXX is the correct name of my entity.
I know it's something related to the managedObjectContext not being initialized. But I don't understand why does it work on the seconViewController and not on the modalViewController.
So, how can I make it work?
I know that importing the AppDelegate to the modalView, although it would work, it's not a good practise.
Thanks in advance!
Edit:
The code I'm using to save the data is as follows:
-(void)saveDataToCD{
NSString *entityString=[NSString stringWithFormat:@"%@",[arrayAdquiritDelMV objectAtIndex:1]];
NSString *deltaString=[NSString stringWithFormat:@"%@",[arrayAdquiritDelMV objectAtIndex:3]];
NSManagedObjectContext *context = [self managedObjectContext];
NSManagedObject *pepe = [NSEntityDescription insertNewObjectForEntityForName:entityString inManagedObjectContext:context]; // aqui un string
[pepe setValue:assigment.text forKey:@"definition"];
[pepe setValue:impurity.text forKey:@"impurity"];
[pepe setValue:[NSNumber numberWithDouble:[delta.text doubleValue]] forKey:deltaString]; //aquí un string
NSError *error;
if (![context save:&error])
{
NSLog(@"Problem saving: %@", [error localizedDescription]);
}}
Edit #2, here is how I call the modal view:
-(IBAction)saveCustom:(id)sender{
if(saveCustomController == nil)
{
SaveCustom *viewTwo = [[SaveCustom alloc] initWithNibName:@"SaveCustom" bundle:[NSBundle mainBundle]];
self.saveCustomController = viewTwo;
[viewTwo release];
}
[self presentModalViewController:self.saveCustomController animated:YES];
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
从你的描述来看,它看起来不像这样,但仍然值得一试。
自从启动应用程序以来,您是否修改过核心数据模型?
如果是,您需要进入应用程序文件夹并删除 Core-Data 支持文件。
因为它现在与您的模型不同步,并且核心数据不知道如何处理它。
我唯一的疑问是,当我在修改后启动应用程序时,而不是 2 或 3 个视图后启动应用程序时,我会收到该错误。
如何以模态方式呈现该视图,在哪里创建它以及在哪里设置以模态方式呈现的视图的上下文?
您忘记告诉 UIViewController 要使用什么上下文。
您缺少像这样的一行
当您在 applicationFinished 中执行此操作时...您的实例尚不存在。
在 Obj-C 中,你可以向 nil 发送消息,它只会被忽略,不会像其他语言那样崩溃。
From what you are describing it don't look like this,but it still worth a try.
Have you modified your Core-Data model since you've launch your application?
If yes you need to go in your application folder and delete the Core-Data backing file.
Because it is now out of sync with your model and core data don't know what to do with it.
The only doubt I have it's that I would received that error when I launch my application after a modification, not 2 or 3 views later.
How do you present that view modally, where do you create it and where do you set the context on that view that you present modally?
you are forgetting to tell the UIViewController what context to use.
You are missing a line like this one
When you do it in applicationFinised... your instance don't exist yet.
And in Obj-C you can send a message to nil, it will just be ignored, you won't crash like in other language.