Realm and auto increment Behavior (Android)

后端 未结 8 2082
野的像风
野的像风 2020-12-14 16:22

I\'m trying to get data from Realm using an ID as a reference. However, when querying for an ID, I\'ve found that Realm is giving me the same ID for all elements (ID of 0).

8条回答
  •  囚心锁ツ
    2020-12-14 16:58

    // EDIT

    I found this new solution on this problem

    @PrimaryKey
    private Integer _id = RealmAutoIncrement.getInstance().getNextIdFromDomain(AccountType.class);
    

    // Original Answer

    I just wanted to share my attempt on solving this Problem, because i don't want to pass a primary Key value all the time. First i created a Database-Class to handle the storage of a RealmObject.

    public class RealmDatabase {
    Realm realm;
    
    public RealmDatabase() {
        RealmConfiguration realmConfiguration =  new RealmConfiguration.Builder()
                .name("test").schemaVersion(1).build();
    
        try {
            realm = Realm.getInstance(realmConfiguration);
        } catch (Exception e) {
            Log.e("Realm init failed", e.getMessage());
        }
    }
    
    protected void saveObjectIntoDatabase(final RealmObject object) {
        realm.executeTransactionAsync(new Realm.Transaction() {
            @Override
            public void execute(Realm bgRealm) {
                bgRealm.copyToRealm(object);    
            }
        }, new Realm.Transaction.OnSuccess() {
            @Override
            public void onSuccess() {
                // onSuccess
            }
        }, new Realm.Transaction.OnError() {
            @Override
            public void onError(Throwable error) {
                // onError
            }
        });
    }
    

    After creating the database-class i created an Interface to distinguish between Objects with a Primary Key and without.

    public interface AutoIncrementable {
        public void setPrimaryKey(int primaryKey);
        public int getNextPrimaryKey(Realm realm);
    }
    

    Now you will just need to edit this code of the previous execute method

    if(object instanceof AutoIncrementable){
      AutoIncrementable autoIncrementable = (AutoIncrementable) object;
      autoIncrementable.setPrimaryKey(autoIncrementable.getNextPrimaryKey(bgRealm));
      bgRealm.copyToRealm((RealmObject)autoIncrementable);
    } else {
      bgRealm.copyToRealm(object);
    }
    

    With this solution the database logic will still be in one class and this class can passed down to every class which needs to write into the database.

    public class Person extends RealmObject implements AutoIncrementable{
    
        @PrimaryKey
        public int id;
        public String name;
    
        @Override
        public void setPrimaryKey(int primaryKey) {
            this.id = primaryKey;
        }
    
        @Override
        public int getNextPrimaryKey(Realm realm) {
            return realm.where(Person.class).max("id").intValue() + 1;
        }
    }
    

    For further suggestions feel free to leave a comment below.

提交回复
热议问题