I don't know how to use LinkingObject
in below situation.
class Message: Object{
...
dynamic fromUser : User?
dynamic toUser : User?
}
class User: Object {
....
let messages = List<Message>()
}
messages
should has whole sent and received message.
How can I use LinkingObject?
Thanks.
From the Docs:
With linking objects properties, you can obtain all objects that link to a given object from a specific property.
In your case I guess you want to get all linked User
s in your Message
instances, so you can define LinkingObjects
like that:
class Message: Object {
dynamic var fromUser : User?
dynamic var toUser : User?
let users = LinkingObjects(fromType: User.self, property: "messages")
}
Otherwise if you need to get all messages from or to user, you can use LinkingObjects
this way:
class Message: Object {
dynamic var fromUser : User?
dynamic var toUser : User?
}
class User: Object {
let fromMessages = LinkingObjects(fromType: Message.self, property: "fromUser")
let toMessages = LinkingObjects(fromType: Message.self, property: "toUser")
}
user.fromMessages
will contain all Message
s with fromUser == user
.
Please check out the Relationships section in the Docs for more info.