开发者

How can I create an Entity automatically, which has a one-to-one-not-optional-relationship to the Entity I'm creating?

How can I create an Entity automatically, which has a one-to-one-not-optional-relationship to the Entity I'm creating with:

NSManagedObject *managedObject = [NSEntityDescription insertNewO开发者_如何学PythonbjectForEntityForName:[entity name] inManagedObjectContext:context];

I can't find an answer to that. My Workaround is to set the relationship to "optional". Is there a better way to do this?


You set the property after creating the new instance. Validation rules (e.g. whether a relationship is optional, etc.) are only enforced on save, though you can test them earlier with certain methods. This means you can create the object, set up the properties and relationships, and then save. So in this case, you'd probably want something like

NSManagedObject *newObject = [NSEntityDescription insertNewObjectForEntityForName:[entity name] inManagedObjectContext:context];
[newObject setValue:entity forKey:@"relationship"];
// now the object should be valid when you call save
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜