我需要在iPhone上的代码中手动设置核心数据对象的反向关系 [英] Do I Have to Manually Set a Core Data Object's Inverse Relationship in Code on the iPhone

查看:116
本文介绍了我需要在iPhone上的代码中手动设置核心数据对象的反向关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新的核心数据,所以请原谅我,如果我得到一些条款错误。



我有xcdatamodel文件中的几个对象。它们都与关系和逆关系相互关联。如果我使用以下代码连接其中两个对象,则不会设置逆关系。

  [managedObj1 setValue:managedObj2 forKey:@ relatiohipName]; 

我似乎必须手动设置与以下代码相反的关系:

  [managedObj1 setValue:managedObj2 forKey:@relatiohipName]; 
[managedObj2 setValue:managedObj1 forKey:@reverseRelatiohipName];

这似乎是错误的,但它是唯一的方式,我似乎得到的机制工作。我在运行第一个代码块后看过sqlite数据库,但是如果我运行第二个代码的关系是存在的。



,似乎一旦我创建一个对象在Core Data我不能改变它之后。 dp保持不变。一旦我退出应用程序并重新启动它,我似乎失去了对象的所有关系和属性。

p>注释掉的东西是以前的方式,没有注释的东西是我现在做没有运气。



这里是我创建的对象:

  NSEntityDescription * mobileEntity = [NSEntityDescription entityForName:@WebServiceAuthService_mobileAdvertisementVOinManagedObjectContext:managedObjectContext]; 
WebServiceAuthService_mobileAdvertisementVO * newObject = [NSEntityDescription insertNewObjectForEntityForName:[mobileEntity name] inManagedObjectContext:managedObjectContext];
// WebServiceAuthService_mobileAdvertisementVO * newObject = [NSEntityDescription insertNewObjectForEntityForName:@WebServiceAuthService_mobileAdvertisementVOinManagedObjectContext:managedObjectContext];

这里是我分配对象成员变量的地方:

  [self setValue:newChild forKey:@advertisement]; 
//self.advertisement = newChild;

这是我保存上下文的位置:

  NSError * error = nil; 
if(managedObjectContext!= nil)
{
if([managedObjectContext hasChanges]&&![managedObjectContext save:& error])
{
DLog (@未解决的错误%@,%@,错误,[错误userInfo]);
abort();
}

}


解决方案

重要安全提示。如果你的子类NSManagedObject不要使用@synthesize为你的成员变量。请改用@dynamic。


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();
    }

}

解决方案

Important safety tip. If you subclass NSManagedObject DO NOT use @synthesize for your member variables. Use @dynamic instead.

这篇关于我需要在iPhone上的代码中手动设置核心数据对象的反向关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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