私はコアデータに慣れていないので、いくつかの用語が間違っている場合はご容赦ください。
xcdatamodel ファイルにいくつかのオブジェクトがあります。それらはすべて、関係と逆の関係で相互に接続されています。これらのオブジェクトの 2 つを次のコードで接続すると、逆の関係は設定されません。
[managedObj1 setValue: managedObj2 forKey:@"relatiohipName"];
次のコードを使用して、自分で逆関係を手動で設定する必要があるようです
[managedObj1 setValue: managedObj2 forKey:@"relatiohipName"];
[managedObj2 setValue: managedObj1 forKey:@"inverseRelatiohipName"];
これは私には間違っているように思えますが、メカニズムを機能させる唯一の方法です。コードの最初のブロックを実行した後に sqlite DB を調べましたが、逆の関係は埋められていませんが、2 番目のコードを実行すると関係が存在します。
また、Core Data でオブジェクトを作成すると、その後は変更できないようです。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];
ここでは、オブジェクト メンバー変数の 1 つを割り当てています。
[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();
}
}