I have a Simple DAO including CRUD function
FeedEntryDAO.java
@Dao
public interface FeedEntryDAO {
@Query("SELECT * FROM feedEntrys")
LiveData<List<FeedEntry>> getAll();
@Query("SELECT * FROM feedEntrys WHERE uid = :uid LIMIT 1")
LiveData<FeedEntry> findByUid(int uid);
@Insert
void insertAll(FeedEntry... feedEntries);
@Delete
void delete(FeedEntry feedEntry);
@Update
int update(FeedEntry feedEntry);
}
For the select
, it is okay to return the LiveData type.
Inside the Activity the code is pretty for the selection
viewModel.getFeedEntrys().observe(this,entries -> {...});
However, when I try to insert, update , delete the data. The code seems a little bit ugly and also create a asynctask every time.
new AsyncTask<FeedEntry, Void, Void>() {
@Override
protected Void doInBackground(FeedEntry... feedEntries) {
viewModel.update(feedEntries[0]);
return null;
}
}.execute(feedEntry);
I have 2 question about it:
- Can I use LiveData to wrap the delete, insert , update function ?
- Better way to maintain such asynctask class for delete, insert , update?
Appreciate any suggestions and advices. Thank you.