将NSManagedObject的实例添加到NSManagedObjectContext ok,更新同一实例失败 [英] Add an instance of NSManagedObject to NSManagedObjectContext ok, updating the same instance failed

查看:247
本文介绍了将NSManagedObject的实例添加到NSManagedObjectContext ok,更新同一实例失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的iPhone应用程式中使用核心资料。我创建了一个简单的类朋友,它源于 NSManagedObject ,并使用以下属性:

I am using core data in my iPhone app. I have created a simple class Friend, which derives from NSManagedObject and which uses the following property:

@property (nonatomic, retain) NSString *name;



我可以添加和删除这个类的实例到我的上下文,我的更改也是永久的。

现在我想要更新/修改 朋友 - 例如,再次持续。

I am able to add and delete instances of this class to my context and my changes are persistent also.
Now I want to update/modify a Friend-instance and make it persistent again.

但这似乎不起作用。

这是一段显示我问题的代码:

Here is piece of code which shows my problem:

//  NSManagedObjectContext *context  = < my managed context> 
//   NSFetchedResultsController *nsfrc= < my fetched result controller>

NSEntityDescription *entity = [nsfrc entity];
NSManagedObject *newManagedObject = [NSEntityDescription 
       insertNewObjectForEntityForName:[entity name]  inManagedObjectContext:context];

Friend *f = (Friend *) newManagedObject;
f.name = @"name1";
//1.  --- here context.hasChanges == 1 ---   ok

NSError *error = nil;
if (![context save:&error]) { ... }
//2.  --- here context.hasChanges == 0 ---   ok

f.name = @"name2";
//3.  --- here context.hasChanges == 0 ---   nok?

if (![context save:&error]) { ... }


$ b b

在评论1,一切都很好。我有一个新的 NSManagedObject Friend 类型,我可以更改name属性。上下文显示我有东西要保存。
保存上下文后,我看到context.hasChanges == 0 。还要注意,保存上下文后数据是持久的。

At comment 1 everything is fine. I got a new NSManagedObject of the Friend-type and I can change the name property. The context shows me that there is something to save. After saving the context I see context.hasChanges == 0. Note also that the data is persistent after saving the context.

注释2后,我更改了name属性。现在我会期望 context.hasChanges == 1 以及在上下文保存后,我会期望新的名称是持久的。
但不幸的是不是。再次启动应用程序,使用name-property = @name1加载朋友实例。

After comment 2 I change the name property. Now I would expect context.hasChanges == 1 and also after the context save I would expect the new name to be persistent. But unfortunately It is not. Starting the application again, loads the Friend instance with the name-property = @"name1".

I在核心数据文档中找不到任何提示或示例。那么我做错了什么?
我需要做什么来更新/修改现有的朋友实例并使其持久化?

I cannot find any hint or example inside the core data documentation. So what am I doing wrong? What do I have to do to update/modify an existing Friend-instance and to make it persistent?

我看到的是删除条目,更改它,并再次添加。但我不认为这是正确的方式。

The only solution I see is to delete the entry, change it, and add it again. But I don't think that this is the correct way for it.

谢谢!

推荐答案

终于找到了解决方案!对于我上面的问题,我删除所有unnessesary代码(不幸的是,我也删除了我的问题)。所以,要完成这个线程:

我的朋友 -class也有一个成员:

Finally found the solution! For my above question I removed all unnessesary code (unfortunately I also removed my problem). So, to complete this thread:
My Friend-class also has a member:

@property (nonatomic) int duration;

在我的测试环境中,我还设置 duration (每当我设置名称)。 这似乎是上下文无法识别任何更改的原因。如果我将属性更改为

In my test environment I also set the duration (whenever I set the name). This seems to be the reason why the context is not able to recognize any change. If I change the property to

@property (nonatomic, retain) NSNumber duration;

一切正常。注意,我的xdatamodel有一个属性 duration 类型 Int32 。我不明白是为什么一切都为第一个 [context save]; 起作用。

everything worked fine. Note that my xdatamodel has a property duration of type Int32. What I not understand is, why everything worked for the first [context save];. But this is ok for me for now.

这里提供一些好建议:

不要生成xdatemodel从你的NSManagedObject派生类(像我做的)。
而不是创建你的xdatemodel,然后从你的模型生成你的类(Xcode-> Design-> DataModel-> Copy Obj-C2.0 ..)!

Update 03 / 2011:在XCode 4.0中生成类的模型就像一个魅力!)

Do NOT generate your xdatemodel out of your NSManagedObject-derived class (as I did). Instead create your xdatemodel and then generate your class out of your model (Xcode->Design->DataModel->Copy Obj-C2.0..)!
(Update 03/2011: Generating the Classes out of the Model works like a charm in XCode 4.0!)

我希望我早在这个美丽的星期天早上就知道了。

晚上好!

I wish I had known this early on this beautiful sunday morning.
Good evening!

这篇关于将NSManagedObject的实例添加到NSManagedObjectContext ok,更新同一实例失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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