Skip to content

Retrieving User Properties

Description

This operation allows retrieving user properties from the BackendlessUser object stored locally in the client application.

Method

// get property value. Return value must be cast to the expected data type.
public Object getProperty( String key )

// get all user object properties
public Map<String, Object> getProperties()

There is a special consideration for a user property containing a collection of related data. For any one-to-many user property, the related data is returned as an array. Before application casts the array object to an array of a specific type, it must check if the size of the array is greater than zero. The following code demonstrates the recommended approach:

// suppose the user object contains a relation user property called "events":
Object[] eventsObjectArray = (Object[]) user.getProperty( "events" );
Event[] eventsArray;

// if array is not empty, it can be cast to an array of specific type
if( eventsObjectArray != null && eventsObjectArray.length > 0 )
  eventsArray = (Event[]) eventsObjectArray;

Return Value

An object containing properties and corresponding values.

{  
  "propName1" : value,  
  "propName2": value,   
}

Example

The example below logs in as "alice@yourmail.com" and retrieves the phoneNumber user property.

public void loginUserAndGetProperties()
{
  Backendless.UserService.login( "alice@yourmail.com", 
                                 "wonderland", 
                                 new AsyncCallback<BackendlessUser>()
  {
    @Override
    public void handleResponse( BackendlessUser loggedUser )
    {
      Toast.makeText( MainActivity.this, 
                      "User has been logged in: " + loggedUser, 
                      Toast.LENGTH_SHORT ).show();

      BackendlessUser user = Backendless.UserService.CurrentUser();
      if( user != null )
      {
        // get user's phone number (i.e. custom property)
        String phoneNumber = (String) user.getProperty( "phoneNumber" );
        Toast.makeText( MainActivity.this, 
                        String.format( "phone number: %s", phoneNumber ), 
                        Toast.LENGTH_SHORT ).show();
      }
      else
      {
        Toast.makeText( MainActivity.this, 
                        "User hasn't been logged", 
                        Toast.LENGTH_SHORT ).show();
      }
    }

    @Override
    public void handleFault( BackendlessFault fault )
    {
      new AlertDialog.Builder( MainActivity.this ).
        setMessage( "Server reported an error: " + fault ).
        setIcon( android.R.drawable.ic_dialog_alert ).
        setPositiveButton( android.R.string.ok, null ).show();
    }
  } );
}