Issue
Am using the MVVM android architecture.I want to get the currently signed in user using the a user_id that i stored in my shared preference class.
I tried getting the user directly from the room database but had an error stating that i have to do it in the background thread.I'm trying to use AsyncTask but its not working out.
my user Model
@Entity(tableName = "users")
public class User {
@PrimaryKey(autoGenerate = true)
private long id;
private String name;
private String email;
private String imageUrl;
public User(String name, String email, String imageUrl) {
this.name = name;
this.email = email;
this.imageUrl = imageUrl;
}
public User() {
}
my getters and setters here
}
my database
@Database(entities = {AnimalTreatment.class, Animals.class,
FarmTask.class, Finance.class, LandAndCrop.class, Machine.class,
User.class}, version = 1)
public abstract class AppDatabase extends RoomDatabase {
private static AppDatabase instance;
public static synchronized AppDatabase getInstance(Context context) {
if (instance == null) {
instance =
Room.databaseBuilder(context.getApplicationContext(), AppDatabase.class,
"database")
.fallbackToDestructiveMigration()
.build();
}
return instance;
}
public abstract UserDao userDao();
}
my Dao
@Dao
public interface UserDao {
@Query("SELECT * FROM users WHERE id = :id")
User getUser(long id);
}
my Repository class
public class UserRepository {
public UserDao userDao;
private User user;
private long userId;
public UserRepository(Application application) {
userId = Settings.getUserId();
AppDatabase database = AppDatabase.getInstance(application);
userDao = database.userDao();
}
public User getCurrentUser(){
new getCurrentUser(userId,userDao).execute();
return user;
}
####This where am confussed how do i return a user in the
getCurrentUser() above ###
public static class getCurrentUser extends AsyncTask<Void, Void,
User> {
private long userId;
private User user;
private UserDao userDao;
public getCurrentUser(long userId, UserDao userDao) {
this.userId = userId;
this.userDao = userDao;
}
@Override
protected User doInBackground(Void... voids) {
// user = new UserRepository(UserRepository.this);
user = userDao.getUser(userId);
return user;
}
}
}
my viewModel class
public class UserViewModel extends AndroidViewModel {
private static final String TAG = "UserViewModel";
private UserRepository userRepository;
private User user;
private UserDao userDao;
public UserViewModel( Application application) {
super(application);
Log.d(TAG, "UserViewModel: Instantiation of UserViewModel");
userRepository = new UserRepository(application);
}
public User getCurrentUser() {
user = userRepository.getCurrentUser();
return user;
}
}
I want to get the currently signed in user from my database(room)
Solution
You don't need to execute the query in a background thread, the room will automatically handle it.
public static User getUser(long id, Context application){
return AppDatabase.getInstance(application).userDao().getUser(id);
}
You can call it from anywhere in the app. Example: in an onCreate
of an Activity
User user=ClassName.getUser(234628263, this);
When initializing DB
Room.databaseBuilder(context.getApplicationContext(),AppDatabase.class,
"database")
.allowMainThreadQueries()
.fallbackToDestructiveMigration()
.build();
This will allow you to execute queries on MainThread
.allowMainThreadQueries()
Answered By - Lakhwinder Singh
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.