0

I've created an app containing multiple databases and I need to use Kinvey for maintaining this data. Currently, I'm storing all the data locally using sqlite. I can write code to sync myself, but it would be much better just to let Kinvey do it. When I'm not trying to use Cache and Offline, the data is written properly to the Kinvey database...I can verify this through the web console. When I try to add the two lines of code for cache and offline policy, nothing gets written to Kinvey db and I get an error similar to what is shown below. This is just part of the logcat error. There appears to be some problem with offline user login?? I included some simple code below that reproduces the problem I'm having. I've read the guides on the website many times, and I just seem to be overlooking something. I added the services and permission to manifest as the guide stated. I don't know what else to try or where else to go. Any ideas as to what I'm doing wrong? Thanks.

AsyncAppData gear = kinveyClient.appData("myCollection",
        MyEntity.class);
gear.setCache(new InMemoryLRUCache(), CachePolicy.CACHEFIRST);
gear.setOffline(OfflinePolicy.LOCAL_FIRST, new SqlLiteOfflineStore(this));
kinveyClient.user().login(username, password, new KinveyUserCallback() {
    @Override
    public void onFailure(Throwable arg0) {
    }

    @Override
    public void onSuccess(User arg0) {
    }
});

MyEntity event = new MyEntity();
event.setTitle("Launch Party");

gear.save(event, new KinveyClientCallback() {

    @Override
    public void onFailure(Throwable arg0) {
    }

    @Override
    public void onSuccess(MyEntity arg0) {
        Toast.makeText(getApplicationContext(),
                "Object saved is: " + arg0.getTitle(),
                Toast.LENGTH_LONG).show();
    }

});

kinveyClient.user().logout().execute();

This is part of the LogCat error:

01-20 15:42:15.257: E/SQLiteDatabase(6992): Error inserting json={"Title":"Launch Party","id":"5e8684e321664fa797f4d1cf53f55082"} deleted=0 _id=5e8684e321664fa797f4d1cf53f55082 _user=null 01-20 15:42:15.257: E/SQLiteDatabase(6992): android.database.sqlite.SQLiteConstraintException: offlinemyCollection._user may not be NULL (code 19)

MAThrasher
  • 97
  • 1
  • 1
  • 9

1 Answers1

0

I'm an engineer at Kinvey working on the android library and can help you out with this--

the issue is that our library performs operations asynchronously, so you need to wait until the login request finishes in the background before attempting to perform that save:

AsyncAppData gear = kinveyClient.appData("myCollection",
    MyEntity.class);
gear.setCache(new InMemoryLRUCache(), CachePolicy.CACHEFIRST);
gear.setOffline(OfflinePolicy.LOCAL_FIRST, new SqlLiteOfflineStore(this));
kinveyClient.user().login(username, password, new KinveyUserCallback() {
    @Override
    public void onFailure(Throwable arg0) {
    }

    @Override
    public void onSuccess(User arg0) {
        MyEntity event = new MyEntity();
        event.setTitle("Launch Party");

        gear.save(event, new KinveyClientCallback() {

        @Override
        public void onFailure(Throwable arg0) {}

        @Override
        public void onSuccess(MyEntity arg0) {
        Toast.makeText(getApplicationContext(),
            "Object saved is: " + arg0.getTitle(),
            Toast.LENGTH_LONG).show();
        }

        });

    }
});

Also, don't call logout, as the offline sync service needs an active user context to be able to sync. For security purposes we clear the queue and the offline store when a logout is performed.

edthethird
  • 6,263
  • 2
  • 24
  • 34