Skip to content Skip to sidebar Skip to footer

Organize Android Realm Data In Lists

I'm looking into migrating our current app to realm and trying to figure out what's the best way to organize the data into ream. For this question I'll focus on the Photo object of

Solution 1:

But because it's data that is never visually presented to the user, it is not sent to the client (I would like to migrate to Realm, but would be better to do it without having to change server-side data models and endpoit behaviors)

That's pitiful but understandable.

What you can do is give your realm Photo objects the fields anyway, like

public class Photo extends RealmObject {
    // other fields

    private double latitude;
    private double longitude;
    private String score;
}

Then, you query the server with the API endpoint and parameters as you do now.

When the results are in, you store them in realm and also assign them the values for lat/long/score. You have these available at this point because you just used them in the server query.

So if you query for api/getNearbyPhotos(1, 2)?sort=best:
(pseudo code)

api.fetch("getNearbyPhotos(1, 2)?sort=best", new GetPhotoCompleteListener {
    @Override
    void onSuccess(List<Photo> photos) {
        // assuming gson or something has already parsed the server response into photo objects
        for (Photo p : photos) {
            p.setLatitude(1);
            p.setLongitude(2);
            p.setScore("best");
        }
        realm.copyToRealmOrUpdate(photos);
    }
}

Now you have a bunch of photo objects in realm that you can query in the same way as you did on the server.


Post a Comment for "Organize Android Realm Data In Lists"