Message:

Subscribe rss
Blog categories
All | Backendless features
Users and Relations in Java/Android and iOS
March 27, 2014 by markpiller
Spread the love

Establishing relations between user objects and other entities in an application is a very common use case. This post describes various scenarios and shows sample code using Backendless SDK for Java/Android and Backendless SDK for iOS. Make sure the version of the client libraries are at least 1.5 for Backendless Java/Android and 1.11 for iOS.

  1. User to Object Relationship. In this scenario a property is added to a user. The value of the property is an object (or a collection of objects) from the Backendless data store. Consider the following code:
    Java: 

    Objective-C:

    The code above initialized the app with app-id and secret key and logs in a user. Then a complex type (Address) is constructed and added as a property (“address”) to the user object. The user is saved using the “update” method in the User Service. Once the user is updated, you can see the relationship in Backendless Console (select your app, then Users and User Properties):
    address prop in user - Users and Relations in Java/Android and iOS

    Additionally, you can see the relation at the data level in Data browser. Click Data and navigate to the user object for which the code performed the update. You should be able to see the “address” column as shown below:
    users to address relation - Users and Relations in Java/Android and iOS

  2. Loading User Relations. Тhe login operation returns BackendlessUser object after validating login credentials. Even though a user may have related properties (relations), the returned BackendlessUser object does not automatically include them. Backendless requires a separate API call to load relations. For example, the following code loads the “address” relation created in the sample above:
    Java: 

    Objective-C:

    As a result of the “loadRelations” operation, the same “user” instance is automatically updated and has the “address” property containing an array of Address objects. To load more than one relation, simply add the names of the related properties to the “rels” list. Alternatively, to load all available relations, use “*”:
    Java:

    Objective-C:

  3. Object to User Relation. This is similar to item (1), but the relationship goes in the opposite direction – from an object to a user account. Consider the following sample: a Java class which includes a field referencing a user:
    Java: 

    Objective-C:

    Create an instance of Person, login a user and link the two together. Then save the instance of Person:

    Java:


    Objective-C:

    Once the call is complete, you will see the Person object in the Data browser and it will contain a link to the related Backendless User account:object to user relation - Users and Relations in Java/Android and iOS
Share this post
Tweet about this on TwitterShare on FacebookGoogle+
  • John Oldman

    Hello) Thank you for so interesting ideas) I as an app developer am really interested in such an issue) so I also want to create something like Instagram for videos)