Retrieving User Relation Objects using Swift

I currently have a one-to-many relation set up on my Users class to Items. This column is called Favorites. Whenever a user favorites an item I plan to add that item as a relation. Anyways, now that I’ve figured that part out in Swift, how would I go about retrieving the Users Favorites? I tried something like this (see below) but I recieved a print statement with an array and the current user BackendlessUser added to it. I was hoping to expect just the favorites themselves and be able to put them into an array.



 let dataQuery = BackendlessDataQuery()
        let queryOptions = QueryOptions()
        queryOptions.related = ["Favorites"]
        dataQuery.queryOptions = queryOptions


        var error: Fault?
        let bc = backendless.data.of(BackendlessUser.ofClass()).find(dataQuery, fault: &error)
        if error == nil {
            print("Favorites have been retrieved: \(bc.data)")
            
            
        }


Hi!

This line

queryOptions.related = ["Favorites"]

says server to return related data object with parent (Users) objects.
On client side you can retrieve Favorites objects from server response.

I’m not sure I quite get what you mean. You’re saying that the line you mentioned above is returning the parent users object?

I take it I’m approaching it wrong. All I’m trying to do is obtain the current user’s Favorited objects from the User class Favorite column I created which is a one-to-many relation.

You should be able to get the related object as a field of your User object. Please see the documentation for reference: https://backendless.com/documentation/data/ios/data_relations_retrieve.htm

Since I’m using the BackendlessUser class isn’t this data already persisted upon logging in (now that I give it more thought)? Since I already have autoload checked off for this Column in the User class, I assume that all of these objects I’d be able to retrieve just by calling the getProperty on the current user. Do I have this correct? I believe I was over thinking it, or maybe I am now…

Yes, using getProperty is the way to go.