swift parse query get last createdAt objects

拟墨画扇 提交于 2019-12-12 02:46:25

问题


I have did a lot of querys, searched in websites , And already asked this question before and i didn't found a good answer!

I have Parse backend looks like this:

In my view controller I just want to show Last createdAt for each sender i want to get all the row of last object for sender:.

so we should ignore "name1: Hello" and "name2: Really when was.." because this old rows we already got new objects.

I want one result for each sender depends on createdAt

so can I get help with query to do this? or how can we do that?

let query = PFQuery(className: "test")
        query.whereKey("receivers", equalTo: PFUser.currentUser()!.username!)
        query.orderByDescending("createdAt")
        query.findObjectsInBackgroundWithBlock {
            (objects, error) -> Void in

            if error == nil {

                // now what? I've done everything i could none worked fine

I hope if i'll get a help to do that, So please help me if you could.


回答1:


you can just add a limit to the query.. see parse docs..

 let query = PFQuery(className: "test")
    query.whereKey("receivers", equalTo: PFUser.currentUser()!.username!)
    query.orderByDescending("createdAt")
    query.limit = 1
    query.findObjectsInBackgroundWithBlock {
        (objects, error) -> Void in

        if error == nil {



回答2:


Your data model isn't really suitable for doing this operation quickly. But, how you setup your data model is dictated by all of your requirements, not just one.

To make this easy you should add a pointer from your User to your Message, and every time a message is sent by a user you set that pointer (which replaces the old pointer).

Now, you can simply query Users, using includeKey to get the message, and display your list.



来源:https://stackoverflow.com/questions/32149883/swift-parse-query-get-last-createdat-objects

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!