Obtain sorted related data

Hello Backendless team.

My question is: Supppose i have two tables Conversations and Messages.
Conversations has 1:N property ‘messages’.
When i do request for Conversations its return all Conversations with array [Messages], wich not sorted.
Is there a way to get sorted related data without using additional requests?

Thanks.

Hello,

To get related data in sorted order, you need to use the two-step retrieval API.

Regards,
Mark

Hello @mark-piller ! At first thank you for your awesome product! I really have shocked about opportunities Backendless. Also thanks for professional team, which always try to help you with any issues!

So what about my question, technically i use DataQueryBuilder object, which allow me to obtain array [Conversations], with all needed data, exept sorted property ‘messages’.
Your answer assume using LoadRelationsQueryBuilder object, but this mean repeat new request for each element of array [Conversations]. It could be waste of API…
I also dont find any properties with type LoadRelationsQueryBuilder inside DataQueryBuilder class (ios sdk).

Regards, Dmitry

Hello Dmitry,

thank you for your kind words!

While using DataQueryBuilder with a single-step retrieval (Conversations list with messages in each conversation object) you’re limited to retrieving no more than 100 messages per each conversation object. Definitely not what you want. The two-step retrieval API allows you to fetch all related objects (of course using pagination if there are more than 100 messages in conversation). Also, it allows specifying sorting, properties, etc using LoadRelationsQueryBuilder.
Indeed it will require additional API requests, but that should not be a problem.

As for iOS SDK, our iOS engineer will assist you here.

Regards,
Stanislaw

Hello @Dmitry_Kalenkov,

I also dont find any properties with type LoadRelationsQueryBuilder inside DataQueryBuilder class (ios sdk).

The LoadRelationsQueryBuilder and DataQueryBuilder are 2 different classes.
The LoadRelationsQueryBuilder allows to get a related objects list for a specific relation property in a parent object is retrieved from the server.
This class also has a sortBy and properties fields, e.g:

let loadRelationsQueryBuilder = LoadRelationsQueryBuilder(relationName: "friends")
loadRelationsQueryBuilder.sortBy = ["name"]
loadRelationsQueryBuilder.properties = ["name"]
        
Backendless.shared.data.ofTable("Person").loadRelations(objectId: "88BF9CE7-AA84-4FF5-966D-A80D44E16AE0", queryBuilder: loadRelationsQueryBuilder, responseHandler: { response in
    if let friends = response as? [[String : Any]] {
        for friend in friends {
            print(friend["name"] ?? "")
        }
    }
}, errorHandler: { fault in
    print("Error: \(fault.message ?? "")")
})

The more information about two-step retrieval with examples can be found in our documentation.

Hello @stanislaw.grin, @olhadanylova.

Thanks for your answers!

I completely understand all what you are talking about. I have figured out how Backendless requests works. I also know that LoadRelationsQueryBuilder and DataQueryBuilder are 2 different classes. I just said that i dont have opportunitie using LoadRelationsQueryBuilder object as DataQueryBuilder property (together using).

I also dont need more than 100 messages per each conversation object. Screen with messages - anoter screen (with own pagination functions). I need last 100 messages (sorted created) for each conversation to continue sort it on client. (show last, chek count of unreaded, etc.)
My guess right now Backendles return related objects sorted by objectId… But this is mean returned related data unvalid to me.