App42 android offline storage doesn't work

96 Views Asked by At

I’m creating an Android Application and I want to use the offline storage. So, I’ve created test application to test app42 BaaS. App42 was initialized in TestApplication.java. App must save the information in offline storage (information about “Tonya” in JSON string) and when Internet connection is available, it must send this information to the online DB. Well, according to the log, “Tonya” was written offline, but it does not appear when I use “findAllDocuments”. And when the internet connection became available, it didn’t synchronize with the online DB. I've tried to use different Android and SDK versions, it didn’t help. What am I doing wrong?

This is AndroidManifest.xml:

 <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.luka94.testapplication">
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
    <uses-permission android:name="android.permission.WRITE_SYNC_SETTINGS"/>
    <application
        android:name=".TestApplication"
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <receiver android:name="com.shephertz.app42.paas.sdk.android.App42BroadcastReceiver">
            <intent-filter>
                <action android:name="android.intent.action.BOOT_COMPLETED"/>
            </intent-filter>
        </receiver>
        <receiver android:name="com.shephertz.app42.paas.sdk.android.AlarmReceiver"/>
        <service android:name="com.shephertz.app42.paas.sdk.android.App42DataSyncService"/>
    </application>
</manifest>

This is TestApplication.java :

package com.luka94.testapplication;

import android.app.Application;

import com.shephertz.app42.paas.sdk.android.App42API;
import com.shephertz.app42.paas.sdk.android.App42CacheManager;
import com.shephertz.app42.paas.sdk.android.App42Response;
import com.shephertz.app42.paas.sdk.android.App42Exception;
import com.shephertz.app42.paas.sdk.android.App42BadParameterException;
import com.shephertz.app42.paas.sdk.android.App42NotFoundException;
import com.shephertz.app42.paas.sdk.android.App42CallBack;
import com.shephertz.app42.paas.sdk.android.storage.StorageService;
import com.shephertz.app42.paas.sdk.android.user.User;
import com.shephertz.app42.paas.sdk.android.user.User.Profile;
import com.shephertz.app42.paas.sdk.android.user.User.UserGender;
import com.shephertz.app42.paas.sdk.android.user.UserService;

public class TestApplication extends Application {

    private UserService userService;
    private StorageService storageService;
    public static final String DB_NAME = "DBNAME";

    @Override
    public void onCreate() {
        super.onCreate();
        App42API.initialize(getApplicationContext(), "KEY_1", "KEY_2");
        App42CacheManager.setPolicy(App42CacheManager.Policy.NETWORK_FIRST);
        App42CacheManager.setExpiryInMinutes(60);
        userService = App42API.buildUserService();
        storageService = App42API.buildStorageService();
        App42API.setOfflineStorage(true);
    }

    public UserService getUserService() {
        return userService;
    }
    public StorageService getStorageService() {return storageService;}

}

This is MainActivity.java :

package com.luka94.testapplication;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;

import com.shephertz.app42.paas.sdk.android.App42API;
import com.shephertz.app42.paas.sdk.android.App42CallBack;
import com.shephertz.app42.paas.sdk.android.storage.Storage;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {

    private TestApplication mApplication;
    private String json;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mApplication = (TestApplication) getApplication();

        json = "{\"name\":\"Tonya\",\"age\":\"28\",\"phone\":\"xxx-xxx-xxx\"}";
        testWriteData();
        getRecord();
    }

    private void getRecord() {
        mApplication.getStorageService().findAllDocuments(TestApplication.DB_NAME,
                "test", new App42CallBack() {
                    @Override
                    public void onSuccess(Object o) {
                        ArrayList<Storage.JSONDocument> jsonDocList = ((Storage) o).getJsonDocList();

                        if (!jsonDocList.isEmpty()) {
                            Log.d("TestApp", "objects finded");

                            for (int i = 0; i < jsonDocList.size(); i++) {
                                Log.d("TestApp", String.format("%s", jsonDocList.get(i).toString()));
                            }
                        }
                        Log.d("TestApp", String.format("is from online storage: %b", ((Storage) o).isOfflineSync()));
                        Log.d("TestApp", String.format("is from cache: %b", ((Storage) o).isFromCache()));
                    }

                    @Override
                    public void onException(Exception e) {
                        Log.d("TestApp", e.getMessage());
                    }
                });
    }

    private void testWriteData() {
        mApplication.getStorageService().insertJSONDocument(TestApplication.DB_NAME, "test", json, new App42CallBack() {
            @Override
            public void onSuccess(Object response) {
                Storage storage = (Storage) response;
                if (storage.isOfflineSync()) {
                    Log.d("TestApp", "writed offline");
                } else {
                    Log.d("TestApp", "writed online");
                }
            }

            @Override
            public void onException(Exception ex) {
                System.out.println("Exception Message : " + ex.getMessage());
            }
        });
    }

}
1

There are 1 best solutions below

0
On

The App42 Offline Storage stores the response of an API call of type GET. When the same API call is made again then based on the Policy you have set, response will be delivered to you which may be from server or from cache. It will work only if the method signature is same. It caches the response of that request and delivers for its next call. If you are making different API call to retrieve the respective data, you will not be getting the response data. You can also have a look at its Documentation for more info.

I hope it will help.