正确存储核心数据中一对多关系的数据

时间:2022-02-02 12:32:06

I'm getting a Cocoa error 1570 while trying to store data in a core data object that has a to-many relationship.

在尝试将数据存储在具有多对多关系的核心数据对象时,我得到了Cocoa错误1570。

There error from the log file:

日志文件中有错误:

2012-05-25 12:02:38.919 TestProject[5059:12e03]   DetailedError: {
    NSLocalizedDescription = "The operation couldn\U2019t be completed. (Cocoa error 1570.)";
    NSValidationErrorKey = conversation;
    NSValidationErrorObject = "<Messages: 0x933e190> (entity: Messages; id: 0x933e1d0 <x-coredata:///Messages/tF3A62C22-456B-41EB-B9A4-0BA1E6738A6337> ; data: {\n    conversation = nil;\n    conversationID = nil;\n    createdAt = nil;\n    messageID = nil;\n    nickname = nil;\n    originNetwork = nil;\n    text = nil;\n    timestamp = nil;\n    userImageURL = nil;\n})";
}

The specific relationship is Conversations<--->>Messages, or one conversation can have multiple messages, and each message belongs to exactly one conversation.

具体关系是Conversations <--- >> Messages,或者一个会话可以有多条消息,每条消息只属于一个会话。

In my model, both of these NSManagedObjects are optional.

在我的模型中,这两个NSManagedObject都是可选的。

My question is, how do I properly store a message object in conversation?

我的问题是,如何在对话中正确存储消息对象?

I know it has something to do with sets, but I have yet to implement it properly.

我知道它与集合有关,但我还没有正确实现它。

Any and all specific or abstract code-snippets would be much appreciated!

任何和所有特定或抽象的代码片段将非常感谢!

Thanks!

1 个解决方案

#1


3  

It depends on whether you're using NSManagedObject subclasses or not. If you are (I like to do this), you'd have a ASConversation and ASMessage class (prefixes being whatever), then you have Xcode auto-generate them for you, and you can use something like:

这取决于您是否使用NSManagedObject子类。如果你(我喜欢这样做),你有一个ASConversation和ASMessage类(前缀是什么),然后你有Xcode为你自动生成它们,你可以使用类似的东西:

NSManagedObjectContext *moc;  // exists 
ASConversation *conv = 
    [NSEntityDescription insertNewObjectForEntityForName:@"Conversation" 
                                  inManagedObjectContext:moc];
// … set values on the conv
ASMessage *msg = 
    [NSEntityDescription insertNewObjectForEntityForName:@"Message" 
                                  inManagedObjectContext:moc];
// … set other parts of the message

[conv addMessagesObject:msg];

Provided you have the inverse relationship set up correctly from Message back to Conversation as a to-one, all should be set up for you when you send save to the moc.

如果您将消息回复到对话中的反向关系设置为正确,则在将保存发送到moc时应为您设置所有反向关系。

If you don't have subclasses, you'd have to use the generic way, a bit more of a pain IMHO.

如果你没有子类,你必须使用通用的方式,更多的痛苦恕我直言。

// Assume conv and msg exist as above, but both are of NSManagedObject types
NSMutableSet *set = [conv mutableSetValueForKey:@"messages"];
// Changes to the above set are managed by Core Data for you.
[set addObject:msg];

Then save as before. Do check that you have optional also set on the relationship - cocoa error 1570 is "mandatory value not set" - but it can apply to relationships too.

然后像以前一样保存。请检查您是否还在关系上设置了可选项 - 可可错误1570是“未设置强制值” - 但它也可以应用于关系。

#1


3  

It depends on whether you're using NSManagedObject subclasses or not. If you are (I like to do this), you'd have a ASConversation and ASMessage class (prefixes being whatever), then you have Xcode auto-generate them for you, and you can use something like:

这取决于您是否使用NSManagedObject子类。如果你(我喜欢这样做),你有一个ASConversation和ASMessage类(前缀是什么),然后你有Xcode为你自动生成它们,你可以使用类似的东西:

NSManagedObjectContext *moc;  // exists 
ASConversation *conv = 
    [NSEntityDescription insertNewObjectForEntityForName:@"Conversation" 
                                  inManagedObjectContext:moc];
// … set values on the conv
ASMessage *msg = 
    [NSEntityDescription insertNewObjectForEntityForName:@"Message" 
                                  inManagedObjectContext:moc];
// … set other parts of the message

[conv addMessagesObject:msg];

Provided you have the inverse relationship set up correctly from Message back to Conversation as a to-one, all should be set up for you when you send save to the moc.

如果您将消息回复到对话中的反向关系设置为正确,则在将保存发送到moc时应为您设置所有反向关系。

If you don't have subclasses, you'd have to use the generic way, a bit more of a pain IMHO.

如果你没有子类,你必须使用通用的方式,更多的痛苦恕我直言。

// Assume conv and msg exist as above, but both are of NSManagedObject types
NSMutableSet *set = [conv mutableSetValueForKey:@"messages"];
// Changes to the above set are managed by Core Data for you.
[set addObject:msg];

Then save as before. Do check that you have optional also set on the relationship - cocoa error 1570 is "mandatory value not set" - but it can apply to relationships too.

然后像以前一样保存。请检查您是否还在关系上设置了可选项 - 可可错误1570是“未设置强制值” - 但它也可以应用于关系。