I just have a quick question. Do you have any save/update method that can on duplicate entry update the fields for the already existing entry or do I need to first check if it’s available in the database already, fetch that object and then update it?
You need to know the objectId, then you can save the object which results in an update. If you do not know the objectId you would create a New object when saving, no matter if data is the same.
Since one field in my table is “Unique Value” and “Indexed” as constraint it won’t create a new object for me. It throws an exception instead.
So the reason why I asked for on duplicate entry - update was to figure out if there was another way around the exception besides knowing the objectId.
But I guess then I’ll have to stick with doing first one access to check if available, if not create a new else update the old one. To bad, this requires 2 requests to the database and not only one, well well!
I just tried exactly the same code you posted and it worked for me. Here’s what I have (this is using your Person and Relationship classes without any changes):
final AsyncCallback<Relationship> saveRelationshipCallback = new AsyncCallback<Relationship>() {
public void handleResponse(Relationship savedContact) {
Log.v("USPJELI SMO", "USPJELI SMO");
}
@Override
public void handleFault(BackendlessFault backendlessFault) {
Log.v("No, it doesn't work", backendlessFault.toString());
}
};
AsyncCallback<Relationship> getRelationshipCallback = new AsyncCallback<Relationship>() {
@Override
public void handleResponse(Relationship relationship) {
Relationship newrelationship = new Relationship();
newrelationship.setObjectId(relationship.getObjectId());
newrelationship.setFriends(relationship.getFriends());
newrelationship.setPendings(relationship.getPendings());
newrelationship.setRequests(relationship.getRequests());
newrelationship.saveAsync( saveRelationshipCallback );
}
@Override
public void handleFault(BackendlessFault fault) {
Log.e( "APP ERROR", fault.getMessage() );
}
};
Backendless.Data.of( Relationship.class ).findFirst( getRelationshipCallback );
On the server-side I have the following table definitions: