Issue
I am trying to get the autogenerated id from a Room database, but am struggling with how to implement using MVVM with all the layers. I am new to Android programming. I believe onPostExecute is the key, but do not know how I get the id back to the UI.
I have this class:
@Entity
public class Scenario {
@PrimaryKey (autoGenerate = true)
private int id;
private String name;
public Scenario(String name) {this.name = name;}
public void setId(@NonNull int id) {this.id = id;}
@NonNull
public int getId() {return id;}
public String getName() {return name;}
}
And this DAO (only showing insert related code):
@Dao
public interface ScenarioDao {
@Insert
Long insert(Scenario scenario);
}
With this Repository (only showing insert related code):
public class ScenarioRepository {
private ScenarioDao scenarioDao;
private LiveData<List<Scenario>> allScenarios;
public ScenarioRepository(Application application) {
AppDatabase database = AppDatabase.getInstance(application);
scenarioDao = database.scenarioDao();
allScenarios = scenarioDao.getAllScenarios();
}
public void insert (Scenario scenario) {
new InsertScenarioAsyncTask(scenarioDao).execute(scenario);
}
private static class InsertScenarioAsyncTask extends AsyncTask<Scenario, Void, Long> {
private ScenarioDao scenarioDao;
private InsertScenarioAsyncTask (ScenarioDao scenarioDao) {
this.scenarioDao = scenarioDao;
}
//protected Void doInBackground(Scenario... scenarios) {
@Override
protected Long doInBackground(Scenario... scenarios) {
return scenarioDao.insert(scenarios[0]);
}
//protected void onPostExecute(Void aVoid) {
@Override
protected void onPostExecute(Long insertId) {
super.onPostExecute(insertId);
//onScenarioInserted(insertId, scenario);
}
}
}
And this ViewModel:
public class ScenarioDetailViewModel extends AndroidViewModel {
private ScenarioRepository repository;
public ScenarioDetailViewModel(@NonNull Application application) {
super(application);
repository = new ScenarioRepository(application);
}
public void insert(Scenario scenario) {
repository.insert(scenario);
}
}
Called by this function in a fragment which is called from a button OnClickListener:
private void saveScenario() {
String name = editTextName.getText().toString();
if (name.isEmpty()) {
Toast.makeText(getActivity(), "Please enter scenario name.", Toast.LENGTH_SHORT).show();
return;
}
Scenario scenario = new Scenario(name);
scenarioDetailViewModel.insert(scenario);
}
UPDATED Thanks to the comments below.
public class ScenarioRepository {
private ScenarioDao scenarioDao;
private LiveData<List<Scenario>> allScenarios;
public ScenarioRepository(Application application) {
AppDatabase database = AppDatabase.getInstance(application);
scenarioDao = database.scenarioDao();
allScenarios = scenarioDao.getAllScenarios();
}
public void insert (MutableLiveData<Long> id, Scenario scenario) {
new InsertScenarioAsyncTask(scenarioDao).execute(id, scenario);
}
private static class InsertScenarioAsyncTask extends AsyncTask<Scenario, Void, Long> {
private ScenarioDao scenarioDao;
private MutableLiveData<Long> id;
private InsertScenarioAsyncTask (MutableLiveData<Long> id, ScenarioDao scenarioDao) {
this.scenarioDao = scenarioDao;
this.id=id;
}
//protected Void doInBackground(Scenario... scenarios) {
@Override
protected Long doInBackground(Scenario... scenarios) {
return scenarioDao.insert(scenarios[0]);
}
//protected void onPostExecute(Void aVoid) {
@Override
protected void onPostExecute(Long insertId) {
id.postValue(insertId);
super.onPostExecute(insertId);
}
@Override
protected void onCancelled() {
id.postValue(new Long(0));
super.onCancelled();
}
}
}
and in the fragment:
scenarioDetailViewModel = ViewModelProviders.of(this).get(ScenarioDetailViewModel.class);
final Observer<Long> scenarioIdObserver = new Observer<Long>() {
@Override
public void onChanged(Long aLong) {
Log.d(TAG, "onChanged, scenarioId is " + aLong);
}
};
scenarioDetailViewModel.getCurrentScenarioId().observe(getActivity(),scenarioIdObserver);
And this in the ViewModel:
public class ScenarioDetailViewModel extends AndroidViewModel {
private static final String TAG="ScenarioDetailViewModel";
private ScenarioRepository repository;
private MutableLiveData<Long> currentId;
public ScenarioDetailViewModel(@NonNull Application application) {
super(application);
currentId = new MutableLiveData<Long>();
repository = new ScenarioRepository(application);
}
public MutableLiveData<Long> getCurrentId() {
return currentId;
}
public void insert(Scenario scenario) {
Log.d(TAG,"inserting scenario " + scenario.getName());
repository.insert(currentId,scenario);
}
public void update(Scenario scenario) {
repository.update(scenario);
}
public void delete(Scenario scenario) {
repository.delete(scenario);
}
}
Solution
You can use a LiveData
inside AsyncTask
ScenarioRepository would look like this:
public MutableLiveData<Long> insert (Scenario scenario) {
MutableLiveData<Long> liveData = new MutableLiveData()
new InsertScenarioAsyncTask(liveData, scenarioDao).execute(scenario);
return liveData ;
}
ِYour InsertScenarioAsyncTask:
private static class InsertScenarioAsyncTask extends AsyncTask<Scenario, Void, Long> {
private ScenarioDao scenarioDao;
private MutableLiveData<Long> liveData;
private InsertScenarioAsyncTask (MutableLiveData<Long> liveData, ScenarioDao scenarioDao) {
this.liveData = liveData;
this.scenarioDao = scenarioDao;
}
//protected Void doInBackground(Scenario... scenarios) {
@Override
protected Long doInBackground(Scenario... scenarios) {
return scenarioDao.insert(scenarios[0]);
}
//protected void onPostExecute(Void aVoid) {
@Override
protected void onPostExecute(Long insertId) {
liveData.postValue(insertId);
super.onPostExecute(insertId);
//onScenarioInserted(insertId, scenario);
}
@Override
protected void onCancelled() {
liveData.postValue(0);
super.onCancelled();
}
}
in your fragment:
liveData.observe(this, insertedId -> {
// insertedId is the id of the inserted scenario
// if value is 0 then the task was cancelled
if(insertedId == 0){
}else{
}
});
Answered By - RaminBoodaghi
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.