Realm Android创建一个具有空值的对象

时间:2017-02-06 21:12:27

标签: android realm

我在使用Realm for Android时遇到了一些奇怪的行为。当我将User对象提交给Realm时,它似乎创建了另一个具有所有空值的User。有没有人遇到这个,我做错了什么?

User user = ...fetched from remote 
Realm realm = null;
try {
    realm = Realm.getDefaultInstance();
    realm.executeTransaction(new Realm.Transaction() {
        @Override
        public void execute(Realm realm) {
            // Here I get no results as expected, I have yet to commit the user to the realm. 
            for(User queriedUser: realm.where(User.class).findAll()) {
                Log.v("qwer", "Before: " + queriedUser);
            }

            // Commit the user to the realm
            realm.insertOrUpdate(user);

            // Results in 2 records: the User I just committed, and another User with all null values.
            for(User queriedUser: realm.where(User.class).findAll()) {
                Log.v("qwer", "After: " + queriedUser);
            }
        }
    });
} finally {
    if(realm != null) {
        realm.close();
    }
}

0 个答案:

没有答案