Artem Artem - 1 year ago 120
Android Question

Adding only unique values into Realm

Is there an elegant way to add a batch of new objects from JSON, taking into consideration that the new bunch might contain values that already in DB and that DB must contain only unique values?

Answer Source

Why not using the same id in the JSON object?, check that a unique id is being sent from the server and prepare a method that checks out for the id if it exists.

//Check if item exists already with id

public boolean checkIfExists(String id){

    RealmQuery<Data> query = realm.where(Data.class)
            .equalTo("id", id);

    return query.count() != 0;
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download