0

I want get list of objects from DB and after network request update these objects in DB.

public class TripTrack extends RealmObject {
    private int tripId;

    private double latitude;

    private double longitude;

    private long downtime;

    private long timestamp;

    private boolean sent;

    public int getTripId() {
        return tripId;
    }

    public void setTripId(int tripId) {
        this.tripId = tripId;
    }

    public double getLatitude() {
        return latitude;
    }

    public void setLatitude(double latitude) {
        this.latitude = latitude;
    }

    public double getLongitude() {
        return longitude;
    }

    public void setLongitude(double longitude) {
        this.longitude = longitude;
    }

    public long getDowntime() {
        return downtime;
    }

    public void setDowntime(long downtime) {
        this.downtime = downtime;
    }

    public long getTimestamp() {
        return timestamp;
    }

    public void setTimestamp(long timestamp) {
        this.timestamp = timestamp;
    }

    public boolean isSent() {
        return sent;
    }

    public void setSent(boolean sent) {
        this.sent = sent;
    }
}

Get from DB:

List<TripTrack> localTrack = new ArrayList<>();

try (Realm realmInstance = Realm.getDefaultInstance()) {
    realmInstance.executeTransaction((realm) -> {
        List<TripTrack> trackFromDB = realm.where(TripTrack.class).equalTo("sent", false).findAll();

        localTrack.addAll(realm.copyFromRealm(trackFromDB));
    });
}

updateTrack(localTrack);

Send it:

private void updateTrack(List<TripTrack> localTrack) {
    JSONObject params = new JSONObject();
    try {
        JSONArray locations = new JSONArray();
        for(TripTrack tripTrack : localTrack) {
            JSONObject loc = new JSONObject();

            loc.put("trip_id", String.valueOf(tripTrack.getTripId()));
            loc.put("latitude", String.valueOf(tripTrack.getLatitude()));
            loc.put("longitude", String.valueOf(tripTrack.getLongitude()));
            loc.put("downtime", String.valueOf(tripTrack.getDowntime()));
            loc.put("timestamp", String.valueOf(tripTrack.getTimestamp()));

            locations.put(loc);
        }

        params.put("locations", locations);
    } catch (JSONException e) {
        e.printStackTrace();
    }

    UpdateTripTrackRequest updateTripTrackRequest = new UpdateTripTrackRequest<>(this, params, DefaultResponse.class,  new Response.Listener<DefaultResponse>() {
        @Override
        public void onResponse(DefaultResponse response) {
            if(response.getCode() == 0) {
                try (Realm realmInstance = Realm.getDefaultInstance()) {
                    realmInstance.executeTransaction((realm) -> {
                        for(TripTrack tripTrack : localTrack) {
                            tripTrack.setSent(true);
                            realm.copyToRealmOrUpdate(tripTrack);
                        }
                    });
                }
            }
        }
    }
// . . .

For copyToRealmOrUpdate I need @PrimaryKey. But Realm doesn't support autoincrement of PrimaryKey. Is it possible perform actions described above without primary key? Should I create primary key as described here?

ViT-Vetal-
  • 2,431
  • 3
  • 19
  • 35
  • Maybe I'm just completely misunderstanding your naming conventions but `trip_id` sounds like a primary key? – EpicPandaForce Jul 12 '18 at 11:08
  • @EpicPandaForce no. It is id of trip. Trip consist of lot of TripTracks. I added TripTrack class. – ViT-Vetal- Jul 13 '18 at 08:51
  • 1
    You need a primary key, a good solution is to use `UUID.randomUUID().toString()` to get a unique id. Even if you don't use it because you use other criterias to find your date – Maelig Aug 02 '18 at 12:45

1 Answers1

0
*Here invoice is realm object and invoiceList with an updated invoice.

suspend fun updateList(invoiceList: MutableList<Invoice>, mRealm: Realm) {
    withContext(Dispatchers.Main) {
        try {
            mRealm.executeTransaction {
                it.insertOrUpdate(invoiceList)
            }
        } catch (error: Exception) {
            error.printStackTrace()
        } finally {
            mRealm.close()
        }
    }
}
TOUSIF
  • 285
  • 5
  • 4