I searched and found FindFirst returns null question but no one answered it. As I'm thinking I am doing something wrong, let me explain my problem with more details.
I'm working on an app that asks the user to sign in first and then lets the user use the app.
My User
class looks like this:
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 store User's object in Realm db after successful login in 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's 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);
}
}
Once the login is successful, app directs user to MainVideoActivity
. I want to find user in realm by following code however I'm getting 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
is null
in both approaches.
However, I can see my none null
user in db.
I'm using classpath "io.realm:realm-gradle-plugin:2.0.2"