iPhone上是否需要在代码中手动设置核心数据对象的逆关系
我是核心数据的新手,所以如果我弄错了一些术语,请原谅。
我的 xcdatamodel 文件中有几个对象。它们都是相互关联、相互关联的。如果我使用以下代码连接其中两个对象,则不会设置逆关系。
[managedObj1 setValue: managedObj2 forKey:@"relatiohipName"];
我似乎必须使用以下代码自己手动设置逆关系,
[managedObj1 setValue: managedObj2 forKey:@"relatiohipName"];
[managedObj2 setValue: managedObj1 forKey:@"inverseRelatiohipName"];
这对我来说似乎是错误的,但这是我似乎可以让该机制发挥作用的唯一方法。我在运行第一个代码块后查看了 sqlite DB,并且未填写逆关系,但如果我运行第二个代码,则关系就在那里。
另外,似乎一旦我在核心数据中创建了一个对象,我就无法再更改它了。 dp 保持不变。一旦我退出应用程序并重新启动它,我似乎就失去了对象的所有关系和属性。我的代码中生成的对象除了 nil 成员变量之外什么都没有。
编辑:
注释掉的内容是以前完成的方式,未注释的内容是我现在的做法,但运气不好。
这是我创建对象的位置:
NSEntityDescription* mobileEntity = [NSEntityDescription entityForName:@"WebServiceAuthService_mobileAdvertisementVO" inManagedObjectContext:managedObjectContext];
WebServiceAuthService_mobileAdvertisementVO *newObject = [NSEntityDescription insertNewObjectForEntityForName:[mobileEntity name] inManagedObjectContext:managedObjectContext];
//WebServiceAuthService_mobileAdvertisementVO *newObject = [NSEntityDescription insertNewObjectForEntityForName:@"WebServiceAuthService_mobileAdvertisementVO" inManagedObjectContext:managedObjectContext];
这是我分配对象成员变量之一的位置:
[self setValue:newChild forKey:@"advertisement"];
//self.advertisement = newChild;
这是我保存上下文的位置:
NSError *error = nil;
if (managedObjectContext != nil)
{
if ([managedObjectContext hasChanges] && ![managedObjectContext save:&error])
{
DLog(@"Unresolved error %@, %@", error, [error userInfo]);
abort();
}
}
I am new to core data so please excuse me if I get some of the terms wrong.
I have several objects in my xcdatamodel file. They are all inter connected with relationships and inverse relationships. If I connect two of these objects with the following code the inverse relationship is not set.
[managedObj1 setValue: managedObj2 forKey:@"relatiohipName"];
I seem to have to manually set the inverse relationship myself with the following code
[managedObj1 setValue: managedObj2 forKey:@"relatiohipName"];
[managedObj2 setValue: managedObj1 forKey:@"inverseRelatiohipName"];
This seems wrong to me but its the only way I can seem to get the mechanism to work. I have looked at the sqlite DB after running the first block of code and the inverse relationship is not filled in but if I run the second code the relationship is there.
Also, it seems like once I create an object in Core Data I can't alter it after that. The dp remains the same. Once I exit the app and restart it I seem to lose all the relationships and attributes of the object. the resulting objects in my code have nothing but nil member variables.
EDIT:
The commented out stuff is the way it was done before and the uncommented stuff is how I'm doing it now with no luck.
Here is where I am creating the objects:
NSEntityDescription* mobileEntity = [NSEntityDescription entityForName:@"WebServiceAuthService_mobileAdvertisementVO" inManagedObjectContext:managedObjectContext];
WebServiceAuthService_mobileAdvertisementVO *newObject = [NSEntityDescription insertNewObjectForEntityForName:[mobileEntity name] inManagedObjectContext:managedObjectContext];
//WebServiceAuthService_mobileAdvertisementVO *newObject = [NSEntityDescription insertNewObjectForEntityForName:@"WebServiceAuthService_mobileAdvertisementVO" inManagedObjectContext:managedObjectContext];
Here is where I am assigning one of the objects member variables:
[self setValue:newChild forKey:@"advertisement"];
//self.advertisement = newChild;
Here is where I am saving the context:
NSError *error = nil;
if (managedObjectContext != nil)
{
if ([managedObjectContext hasChanges] && ![managedObjectContext save:&error])
{
DLog(@"Unresolved error %@, %@", error, [error userInfo]);
abort();
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您没有在数据模型中设置互惠关系,则必须手动设置关系的双方。如果这样做,则分配关系的一侧会自动分配另一侧。这是数据模型的功能之一。
在我看来,您实际上并没有将对象插入到 NSManagedObjectContext 对象中。上下文是“管理”对象并执行诸如自动关系设置之类的操作的东西。上下文还将对象写入持久存储。
You have to set both sides of a relationship manually if you don't set the reciprocal relationship in the data model. If you do, then assigning one side of the relationship automatically assigns the other. That is one of the functions of the data model.
It sounds to me like you don't actually have your objects inserted into a NSManagedObjectContext object. The context is what "manages" the objects and does things like automatic relationship setting. The context is also what writes the objects to the persistent store.
重要的安全提示。如果您是 NSManagedObject 的子类,请不要使用 @synthesize 作为您的成员变量。请改用@dynamic。
Important safety tip. If you subclass NSManagedObject DO NOT use @synthesize for your member variables. Use @dynamic instead.