Realm findFirst () method returns null

I searched and found FindFirst returns a null question, but no one answered it. Since I think I'm doing something wrong, let me explain my problem in more detail.

I am working on an application that asks the user to log in first and then allow the user to use the application.

My User class is as follows:

 public class User extends RealmObject { @PrimaryKey @SerializedName("uid") String id; @SerializedName("ufname") String firstName; @SerializedName("ulname") String lastName; String avatar; int sessions; int invites; String nextSessionTime; String nextSessionTitle; @SerializedName("lastlogin") String lastLogin; String token; @Override public String toString() { return new GsonBuilder().create().toJson(this, User.class); } // other setters and getters } 

I save the User object in Realm db after successfully entering the SigninActivity class:

 @Override public void onSignInResponse(final GeneralResponse response) { if (response == null) { Timber.e("response is null"); return; } Timber.d(response.toString()); if (response.isSuccess()) { // Store user info including Token realm.executeTransaction(new Realm.Transaction() { @Override public void execute(Realm realm) { realm.copyToRealmOrUpdate(response.getUser()); } }); // Goto main screen MainVideoActivity.startActivity(this); this.finish(); } else { String errorMessage = response.getErrorMessages(); super.displayMessage(errorMessage); } } 

After a successful login, the application directs the user to MainVideoActivity . I want to find the user in the area, following the code, but I get null .

 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main_video); // Create the Realm instance realm = Realm.getDefaultInstance(); User user = realm.where(User.class).findFirst(); // RealmResults<User> user = realm.where(User.class).findAll(); Timber.d(user.toString()); } 

User null in both approaches.

enter image description here

However, I do not see my user null in db.

enter image description here

I use the classpath "io.realm:realm-gradle-plugin:2.0.2"

+6
source share
3 answers

There are two things here:

  • null values ​​in the debug window. This is a known limitation when using Realm with a debugger. Because Realm created the Proxy class to access values, checking the RealmObject field in the debugger will not go through the proxy class. More here

  • Fields with null values ​​are not printed in the toString() method. The Realm annotation handler generates the toString() method if there is no toString () method defined in RealmObject . I think the problem here is that a User.toString() ignores null values. Try removing the toString () method in the User class.

+7
source

In the Realm doc section of Realm, you can try adding isNotNull() to your request as follows:

 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main_video); // Create the Realm instance realm = Realm.getDefaultInstance(); User user = realm.where(User.class).isNotNull("id").findFirst(); //add in this line isNotNull() //RealmResults<User> user = realm.where(User.class).findAll(); Timber.d(user.toString()); } 

I have not tested yet, but it should work.

+1
source

Try replacing

  realm.executeTransaction(new Realm.Transaction() { @Override public void execute(Realm realm) { realm.copyToRealmOrUpdate(response.getUser()); } }); // Goto main screen MainVideoActivity.startActivity(this); this.finish(); 

from

  realm.executeTransactionAsync(new Realm.Transaction() { @Override public void execute(Realm realm) { realm.copyToRealmOrUpdate(response.getUser()); } }, new Realm.Transaction.OnSuccess() { @Override public void onSuccess() { // Goto main screen MainVideoActivity.startActivity(this); finish(); } }); 
+1
source

All Articles