当我尝试将关系保存到核心数据时,它会在标题中抛出上述错误。我基本上是在创建一个消息传递应用程序并且附加了数据模型。在尝试保存到其他关系时,这也是做同样的事情。我可以一次保存所有数据模型,还是应该一次保存一个数据模型,然后返回并在使用常规属性保存模型后添加关系。 ** Data Model Visual
Cat
它在线上失败“newConvo.addMessage(message:newMessage)”
static func save(sender: UserCD, message: String) -> Conversation? {
let convo: Conversation? = nil
guard let appDelegate = UIApplication.shared.delegate as? AppDelegate else {
return convo
}
if #available(iOS 10.0, *) {
let context = appDelegate.persistentContainer.viewContext
let now = Date()
let messageEntity = NSEntityDescription.entity(forEntityName: "Message", in: context)
let newMessage = NSManagedObject(entity: messageEntity!, insertInto: context) as! Message
let conversationEntity = NSEntityDescription.entity(forEntityName: "Conversation", in: context)
let newConvo = NSManagedObject(entity: conversationEntity!, insertInto: context) as! Conversation
newMessage.setValue(UUID().uuidString, forKey: "messageId")
newMessage.setValue(message, forKey: "text")
newMessage.setValue(now, forKey: "timestamp")
newMessage.setValue(sender, forKey: "sender")
newConvo.setValue(UUID().uuidString, forKey: "conversationId")
newConvo.setValue(now, forKey: "startDate")
newConvo.setValue(now, forKey: "lastMessageDate")
newConvo.addMessage(message: newMessage)
sender.addMessage(message: newMessage)
sender.addConversation(conversation: newConvo)
do {
try newMessage.managedObjectContext?.save()
try sender.managedObjectContext?.save()
try newConvo.managedObjectContext?.save()
return newConvo
} catch {
let saveError = error as NSError
print("Failed to save conversastion: \(saveError.debugDescription)")
}
}
return convo
}
答案 0 :(得分:0)
错误消息
ALL或ANY运算符的左侧必须是NSArray或NSSet
很清楚:
您将messages
声明为 to-one 关系Message
。
方法addMessage
的发件人必须是 to-many 关系Set<Message>