[英]How to access room db using viewmodel, repository, and livedata
I am new to android development.我是 android 开发的新手。 In my app, I need to have a user table in the database.
在我的应用程序中,我需要在数据库中有一个用户表。 I have created the database using room.
我已经使用房间创建了数据库。 Now, if I allow to run the db in the main thread, my app is running fine.
现在,如果我允许在主线程中运行数据库,我的应用程序运行良好。 But, I don't want to run it in the main thread.
但是,我不想在主线程中运行它。 So, I created the repository and viewmodel classes and calling the db in the main thread using the viewmodel.
因此,我创建了存储库和 viewmodel 类,并使用 viewmodel 在主线程中调用了 db。 Still if I don't put
allowMainThreadQueries()
in the database building statement, java.lang.IllegalStateException: Cannot access database on the main thread since it may potentially lock the UI for a long period of time.
尽管如此,如果我不将
allowMainThreadQueries()
放入数据库构建语句中, java.lang.IllegalStateException: Cannot access database on the main thread since it may potentially lock the UI for a long period of time.
error pops up.错误弹出。 I checked the following question: How does the Room Database with LiveData, repository, and viewmodel work together?
我检查了以下问题: 带有 LiveData、存储库和视图模型的房间数据库如何协同工作? , still, don't find the mistake in my code.
,仍然,不要在我的代码中找到错误。 What is wrong with my code?
我的代码有什么问题?
My database class:我的数据库 class:
@Database(entities = {User.class,Order.class,Items.class}, version = 1)
public abstract class MyDatabase extends RoomDatabase {
public abstract UserDao userDao();
public abstract OrderDao orderDao();
public abstract ItemsDao itemsDao();
private static volatile MyDatabase INSTANCE;
public static MyDatabase getDatabase(final Context context) {
if (INSTANCE == null) {
synchronized (MyDatabase.class) {
if (INSTANCE == null) {
INSTANCE = Room.databaseBuilder(context.getApplicationContext(),
MyDatabase.class, "new_database")
.addCallback(sRoomDatabaseCallback).build();
}
}
}
return INSTANCE;
}
private static RoomDatabase.Callback sRoomDatabaseCallback = new RoomDatabase.Callback() {
@Override
public void onOpen(@NonNull SupportSQLiteDatabase db) {
super.onOpen(db);
}
};
}
My repository class:我的存储库 class:
public class UserRepository {
private UserDao mUserDao;
private LiveData<List<User>> mAllUsers;
public UserRepository(Application application){
AatchalaDatabase db = AatchalaDatabase.getDatabase(application);
mUserDao = db.userDao();
}
LiveData<List<User>> getAllUsers() {
return mAllUsers;
}
public void insert(User newDBUser){
mUserDao.addUser(newDBUser);
}
}
My viewmodel:我的视图模型:
public class UserViewModel extends AndroidViewModel {
private UserRepository mRepository;
private LiveData<List<User>> mAllUsers;
public UserViewModel(Application application) {
super(application);
mRepository = new UserRepository(application);
mAllUsers = mRepository.getAllUsers();
}
LiveData<List<User>> getAllUsers() {
return mAllUsers;
}
void insert(User user) {
mRepository.insert(user);
}
}
Part of my fragment:我的片段的一部分:
public class RegisterFragment extends Fragment implements View.OnClickListener {
private UserViewModel nUserViewModel;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_register, container, false);
Button submitRegister = (Button) rootView.findViewById(R.id.registerSubmit);
submitRegister.setOnClickListener(this);
EditText et = (EditText) rootView.findViewById(R.id.registerDOB);
DatePickerUniversal dob = new DatePickerUniversal(et, "dd-MM-yyyy");
nUserViewModel = new ViewModelProvider(this).get(UserViewModel.class);
return rootView;
}
@Override
public void onClick(View view) {
if (view.getId() == R.id.registerSubmit) {
if (readInput(R.id.registerName).matches("") || readInput(R.id.registerAddress).matches("")
|| readInput(R.id.registerPhone).matches("") || readInput(R.id.registerDOB).matches("")
|| readInput(R.id.registerPassword).matches(""))
showErrorStatus("Please fill all required fields");
else if (!readInput(R.id.registerPassword).equals(readInput(R.id.registerReTypePassword)))
showErrorStatus("Password doesn't match");
else if (!invalidUser(readInput(R.id.registerPhone)))
showErrorStatus("Sorry this mobile number is already registered");
else {
User newDBUser=new User(readInput(R.id.registerPhone), readInput(R.id.registerName),
readInput(R.id.registerDOB), readInput(R.id.registerAddress),
readInput(R.id.registerEmail), readInput(R.id.registerPassword));
Log.d("userdb",newDBUser.toString());
nUserViewModel.insert(newDBUser);
Toast.makeText(getActivity().getApplicationContext(), "Registration successful", Toast.LENGTH_LONG).show();
getActivity().getSupportFragmentManager().popBackStackImmediate();
}
}
}
I also created the necessary dao and entity files.我还创建了必要的 dao 和实体文件。
The documentation explains that: 该文档解释说:
Room doesn't support database access on the main thread unless you've called allowMainThreadQueries() on the builder because it might lock the UI for a long period of time.
Room 不支持在主线程上访问数据库,除非您在构建器上调用了 allowMainThreadQueries(),因为它可能会长时间锁定 UI。
This applies to insert, update and delete operations also, not just queries.这也适用于插入、更新和删除操作,而不仅仅是查询。
You are performing an insert on the main thread:您正在主线程上执行插入:
nUserViewModel.insert(newDBUser);
That needs to be moved onto a background thread.这需要移动到后台线程。
In Database class add below:在数据库 class 中添加以下内容:
private static final int NUMBER_OF_THREADS = 4;
static final ExecutorService databaseWriteExecutor =
Executors.newFixedThreadPool(NUMBER_OF_THREADS);
In Repository class change below在存储库 class 更改如下
AatchalaDatabase db = AatchalaDatabase.getDatabase(application);
AatchalaDatabase db = AatchalaDatabase.getinstance(application);
Add below to functions添加以下功能
`LiveData<List<User>> getAllUsers() {
AatchalaDatabase.databaseWriteExecutor.execute(() -> {
mAllUsers = mUserDao.getAllUsers();
}
return mAllUsers;
}`
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.