androidkinveymbaas

The best approach to save kinvey object in Android


I would like to create an apps with backend service in kinvey. Previously i use Parse.com as my backend service, due to Parse.com is going to stop their service, so i need to consider another mbaas.

i read many reference in internet like http://devcenter.kinvey.com/android/guides/datastore or github, but i still got no idea how to make a simple saving data to kinvey in android apps.

Parse.com adding data was easy, just create an new Parse Object like

ParseObject object = new ParseObject("booking_details");
object.put("username", struser);

Can anyone with experience in Kinvey android apps development give me a short tutorial about saving data? thank in advance!


Solution

  • It's not that different with Kinvey. But, you must define a java class to model your data first. Kinvey records also always include three meta-data fields: a primary key (_id) and _kmd/_acl to store timestamps and ACLs. So something like this:

    public class BookingDetails extends GenericJson {
        @Key("_id")
        private String _id; 
        @Key("_kmd")
        private KinveyMetaData meta;
        @Key("_acl")
        private KinveyMetaData.AccessControlList acl;
        public EventEntity(){}  //GenericJson classes must have a public empty constructor
    }
    

    In this example, I did not actually add the "username" field to the class. But you can, just like the "String _id" field. And add getter/setters for it in your class. But if you like to work it the easy way, and follow the Parse example, you can add dynamic fields, like this:

    BookingDetails object = new BookingDetails();
    object.put("username", struser);
    

    Then, first link a Kinvey collection to a local "AppData" object:

    AsyncAppData<BookingDetails > bookingdetails = mKinveyClient.appData("bookingDetail", BookingDetails.class);
    

    and save your "object" to Kinvey:

    bookingdetails.save(object, new KinveyClientCallback<EventEntity>() {
    @Override
        public void onFailure(Throwable e) {
            Log.e("TAG", "failed to save event data", e); 
        }
        @Override
        public void onSuccess(EventEntity r) {
            Log.d("TAG", "saved data for entity "+ r.getName()); 
        }
    });
    

    (for a full syntax, see the devcenter)