Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Room persistent library with new Thread and data-binding issue

I am working on Android Room Persistent library. I have also implemented Two-way data binding in my project. In the project, I am inserting users by filling a form and displaying all the users in rows and updating them by clicking particular user row. Below is my UserDao Class:

@Dao
public interface UserDao {

  @Query("SELECT * FROM user")
  List<User> getAllUsers();

  @Insert
  void insertAll(User... users);

  @Update
  void updateUser(User... users);

  @Query("SELECT * FROM user WHERE user_id IN (:userId)")
  User getSpecifiedUser(int...userId);
 }

For databinding I am binding User Model in UserFormActivity

binding.setUserModel(userModel);

Now, as Room persistent library doesn't allow you to do any database operation in Main thread, for updating the user, I am getting data of particular user in User Form by clicking in the row in new Thread as below:

 private void getUserFormData(final int userId) {
    try {
        Thread t = new Thread(new Runnable() {
            public void run() {
                userModel = db.userDao().getSpecifiedUser(userId);
            }
        }, "Thread A");
        t.start();
    } catch (NumberFormatException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

After getting data of user in userModel for updating, I want the User data to reflect in User Form where data binding is performed, but it's not reflecting. I am stuck with this issue.

like image 366
Priyanka Alachiya Avatar asked May 29 '17 11:05

Priyanka Alachiya


1 Answers

I implemented LiveData and ViewModel in my project and also extended LifecycleActivity in my UserListActivity. Here is my AddUserViewModel class:

public class AddUserViewModel extends AndroidViewModel {
     private UserDatabase userDatabase;

     public AddUserViewModel(Application application) {
      super(application);

     userDatabase = UserDatabase.getDatabase(this.getApplication());
     }

     public void addUser(User user) {
     new AddUserAsyncTask(userDatabase).execute(user);
     }

     private static class AddUserAsyncTask extends AsyncTask<User, Void, 
        Void> {

        private UserDatabase db;

        public AddUserAsyncTask(UserDatabase userDatabase) {
             db = userDatabase;
        }

     @Override
     protected Void doInBackground(User... params) {
         db.userDao().insertUser(params);
         return null;
         }
     }

    public User getSpecificUserData(int userId) {
        User user = null;
        try {
             user = new 
             getUserDataAsyncTask(userDatabase).execute(userId).get();
        } catch (InterruptedException | ExecutionException e) {
            e.printStackTrace();
           }
        return user;
    }

    private static class getUserDataAsyncTask extends AsyncTask<Integer, 
        Void, User> {

        private UserDatabase db;

        public getUserDataAsyncTask(UserDatabase userDatabase) {
            db = userDatabase;
        }

        @Override
        protected User doInBackground(Integer... userId) {
            User user = db.userDao().getSpecifiedUser(userId);
            return user;
        }

        @Override
        protected void onPostExecute(User user) {
            super.onPostExecute(user);
        }
      }
  }

In AddUserActivity I am getting particular User data from database method as

userModel = addUserViewModel.getSpecificUserData(userId);

and updating the data on click of submit button. My UserListActivity extends LifecycleActivity in which the user list gets observed and when ever change is there it updates the Use list as below:

 userViewModel = ViewModelProviders.of(this).get(UserViewModel.class);

    userViewModel.getUserList().observe(UserListActivity.this, new 
    Observer<List<User>>() {
        @Override
        public void onChanged(@Nullable List<User> user) {
            adapter.addItems(user);
        }
    });

This worked fine for me and I got my solution!

like image 163
Priyanka Alachiya Avatar answered Oct 24 '22 00:10

Priyanka Alachiya