我对core data比较新,如果术语有误请谅解。
我的xcdatamodel文件中有几个对象。它们都通过关系和反向关系相互连接。如果我使用以下代码连接这些对象中的两个,则反向关系不会被设置。
[managedObj1 setValue: managedObj2 forKey:@"relatiohipName"];
我似乎需要手动使用以下代码设置反向关系
[managedObj1 setValue: managedObj2 forKey:@"relatiohipName"];
[managedObj2 setValue: managedObj1 forKey:@"inverseRelatiohipName"];
这对我来说似乎是错误的,但这是我唯一能让机制工作的方法。在运行第一段代码后,我查看了sqlite数据库,发现反向关系没有填充,但如果我运行第二段代码,则关系就存在了。
另外,似乎一旦我在Core Data中创建了一个对象,就无法在那之后更改它。dp保持不变。一旦我退出应用程序并重新启动它,似乎我会失去对象的所有关系和属性,我的代码中生成的对象只有空成员变量。
编辑:
注释掉的内容是以前的做法,取消注释的内容是我现在的做法,但没有成功。
以下是我创建对象的位置:
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();
}
}