如何使用AsyncTask中的用户ID获取当前登录的用户

时间:2019-07-15 12:13:49

标签: android database mvvm android-asynctask

我使用的是MVVM android体系结构。我想使用存储在共享首选项类中的user_id获取当前登录的用户。

我尝试直接从会议室数据库中获取用户,但出现错误,提示我必须在后台线程中进行操作。我正在尝试使用AsyncTask,但无法正常工作。

我的用户模型

@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
}

我的数据库

 @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();
}

我的道

 @Dao
 public interface UserDao {
     @Query("SELECT * FROM users WHERE id = :id")
     User getUser(long id);
 }

我的存储库类

 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;
        }
    }
}

我的viewModel类

 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)

1 个答案:

答案 0 :(得分:0)

您不需要在后台线程中执行查询,房间会自动处理它。

  public static User getUser(long id, Context application){
     return AppDatabase.getInstance(application).userDao().getUser(id);
 }

您可以从应用程序中的任何位置调用它。示例:在活动的onCreate

User user=ClassName.getUser(234628263, this);

初始化数据库时

Room.databaseBuilder(context.getApplicationContext(),AppDatabase.class, 
 "database")
                .allowMainThreadQueries()
                .fallbackToDestructiveMigration()
                .build();

这将允许您在MainThread上执行查询

.allowMainThreadQueries()