I want to set a custom resolution for this scenario: 1- increment an integer field in realmobject in one device in offline mode 2- increment the same integer field in same realmobject in another device in offline mode The default custom resolution is last update wins but in my case I want the increment in both devices take effect on result after going live not last update. I tried this code for test:
Realm realm = Realm.getDefaultInstance();
final RealmResults<Number> results= realm.where(Number.class).findAll();
realm.executeTransaction(new Realm.Transaction() {
@Override
public void execute(Realm realm) {
int num = results.get(0).getNumber()+1;
results.get(0).setNumber(num);
}
});
the Number class is like this:
public class Number extends RealmObject {
@PrimaryKey
private String id;
private int number;
public String getId() {
return id;
}
public void increment(){
this.number++;
}
public void setId(String id) {
this.id = id;
}
public int getNumber() {
return number;
}
public void setNumber(int number) {
this.number = number;
}
This problem is very crucial to my app. If I can't do this in client side I will not be able to use realm mobile platform which I was get so interested in.
Thanks to @ast code example. I also solved the problem by caching command pattern here is my code:
before getting increment action done I check online state and if it is offline the increment string cached in Command Pattern object after going online again those cached commands get invoked by following code:
this is general custom conflict resolution and can be used for any type of command