简体   繁体   中英

Keeping realm data up to date with live data

Really hoping someone may be able to point me in the right direction for this one. We have an app that uses Realm to manage our data locally on the device.

Through general use there will be data inputted to the device and then this will be attempted to be sent to the live service we have on our api. However due to the location of the users we cannot be sure if this has been uploaded before we re-download the data the next time the app is opened.

We need it to work just like Git in a way. You can't pull without first committing your changes. But instead of committing we are pushing up.

We believe the live data should take priority here so if there is a change on live it should get pulled down, but if there is something null on live but we have it locally then we should change that.

Is there something we can use to code this functionality, can't believe we are the first to face this issue. Either that or a flow for how we should get the data before the user can start working.

An example:

We are looking to save the imei number that the user enters. This will save to the realm db at the time and attempt to update the live database. If this is not possible when the app is restarted and the imei numbers are pulled we don't what that to be possible until the data we have locally that is missing on live to be pushed.

Imei.java (Realm model)

public class Imei extends RealmObject {

    @PrimaryKey
    public int id;

    public int deviceId;

    public String imei;

}

When we get a response on the fetch:

@Override
public void onResponse(JSONObject response) {
    Gson gson = new GsonBuilder()
   .setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES)
   .create();
    ImeiResponse imeiResponse = gson.fromJson(response.toString(), ImeiResponse.class);

    for (Imei imei : imeiResponse.imeis()) {
        addToDB(imei);
    }
}

And then we use addToDb, which is where I would imagine that some kind of check would take place as if we were able to pull then we have a good enough connection to push the data we have locally

public void addToDB(Imei imei)
    {
        Realm realm = Realm.getDefaultInstance();

        realm.beginTransaction();

        realm.copyToRealmOrUpdate(imei);

        realm.commitTransaction();
    }

Just in case someone stumbles across this question we did eventually find a solution.

Any time we need to save data we are adding it into a request queue table and then attempting to run each of the requests. If they pass they are removed, if they fail they are deleted.

Every time we then go to pull data we check that requests queue first and run if there is anything in there, if they continue to fail you then don't pull.

This seemed the best solution for us. Hope it helps

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM