I have two entities Friend and Message now i want to get the latest message of friend how can i do this
我有两个实体朋友和消息现在我想得到朋友的最新消息我该怎么做
Friend contain these things
朋友包含这些东西
Friend{
name:String
id:Int
email:String
}
and Message Contain these thing and also have one to many relationShip from friend to message
和消息包含这些东西,并且还有从朋友到消息的一对多关系
Message{
id:String
text:String
created_at:Date
/...// and many things
}
1 个解决方案
#1
1
You can get the latest message of user by sort Descriptor like this:-
您可以通过排序描述符获取用户的最新消息,如下所示: -
let fetchRequest = NSFetchRequest<Message>.init(entityName: "Message")
fetchRequest.sortDescriptors = [NSSortDescriptor.init(key: "created_at", ascending: false)]
let predicate = NSPredicate.init(format: "friend.id = %@","1")// pass the id of friend for you want to access latest message
fetchRequest.predicate = predicate
now you can fetch this request
现在您可以获取此请求
#1
1
You can get the latest message of user by sort Descriptor like this:-
您可以通过排序描述符获取用户的最新消息,如下所示: -
let fetchRequest = NSFetchRequest<Message>.init(entityName: "Message")
fetchRequest.sortDescriptors = [NSSortDescriptor.init(key: "created_at", ascending: false)]
let predicate = NSPredicate.init(format: "friend.id = %@","1")// pass the id of friend for you want to access latest message
fetchRequest.predicate = predicate
now you can fetch this request
现在您可以获取此请求