Start a new topic

Can I add KinveyReferences and other custom Arrays to the User entity?

For my app I want every user to have a list of other users they are "friends" with, how can I do this?
1 Comment

The User class extends GenericJson, which is used throughout the library as our JSON representation. This provides both put(String key, Object value) as well as get(String key) for setting and retrieving arbitrary JSON data.



To add new data to your User object, try something like this:



//add "myJsonKey":"myJsonValue" to the current User object

kinveyClient.user().put("myJsonKey", "myJsonValue");



//put a new KinveyReference to another user

kinveyClient.user().put("friend", new KinveyReference(User.USERCOLLECTIONNAME, myFriendID));



//and finally, call update to send your changes to your backend

kinveyClient.user().update(new KinveyUserCallback() {

@Override

public void onSuccess(User result) {

//update successfull!

}



@Override

public void onFailure(Throwable error) {

//uh oh, something went wrong

}

});



If you want to resolve KinveyReferences on the current user, you can use one of the variations of the retrieve method. This retrieve methods to resolve KinveyReferences all take a String[]. which contains all JSON keys to resolve. In this case, it is "friend" as it was set above.



kinveyClient.user().retrieve(new String[]{"friend"}, new KinveyUserCallback() {

@Override

public void onSuccess(User result) {

//got the user with friend resolved

}



@Override

public void onFailure(Throwable error) {

//uh oh, something went wrong

}

});



KinveyReferences can also be set in Arrays, so you can call store multiple references in one field.
Login or Signup to post a comment