Android JetPack架构——结合记事本Demo一篇打通对Sqlite的增删改查结合常用jetpack架构应用

}

2.编写数据仓库(Repository、AsyncTask)


如果此处不引入Repository类会造成什么影响呢?为什么要引入Repository?

对数据库的操作(调用Dao)逻辑将放于ViewModel中(步骤5)。使得ViewModel中代码变得杂乱

引入仓库类,用于对数据库操作并对ViewModel暴露方法。让ViewModel专注于数据处理而非对数据库的调用,对ViewModel和Dao进一步解耦。

什么是AsyncTask?为什么需要引入AsyncTask?

  1. 一个Android 已封装好的轻量级异步类,用于实现多线程、异步通信、消息传递

  2. 数据库的操作很重,一次读写操作花费 10~20ms 是很常见的,这样的耗时很容易造成界面的卡顿。所以通常情况下,条件允许情况下要避免在主线程中处理数据库。

public class NoteRepository {

private NoteDao noteDao;

private LiveData<List> allNoteLive;

public NoteRepository(Context context){

NoteDatabase database = NoteDatabase.getINSTANCE(context);

noteDao = database.getNoteDao();

allNoteLive = noteDao.queryAllNotes();

}

public LiveData<List> getAllWordLive() {

return allNoteLive;

}

public LiveData<List> queryNotesWithPattern(String pattern){

//模糊匹配注意百分号

return noteDao.queryNotesWithPattern("%"+pattern+"%");

}

public void insertNotes(Note… notes){

new InsertAsyncTask(noteDao).execute(notes);

}

public void updateNotes(Note… notes){

new UpdateAsyncTask(noteDao).execute(notes);

}

public void deleteNotes(Note… notes){

new DeleteAsyncTask(noteDao).execute(notes);

}

public void deleteAllNotes(){

new DeleteAllAsyncTask(noteDao).execute();

}

//创建副线程类,继承AsyncTask实现

static class InsertAsyncTask extends AsyncTask<Note, Void, Void>{

private NoteDao noteDao;

InsertAsyncTask(NoteDao noteDao) {

this.noteDao = noteDao;

}

@Override

protected Void doInBackground(Note… notes) {

noteDao.insertNotes(notes);

return null;

}

}

static class UpdateAsyncTask extends AsyncTask<Note, Void, Void>{

private NoteDao noteDao;

UpdateAsyncTask(NoteDao noteDao) {

this.noteDao = noteDao;

}

@Override

protected Void doInBackground(Note… notes) {

noteDao.updateNotes(notes);

return null;

}

}

static class DeleteAllAsyncTask extends AsyncTask<Note, Void, Void>{

private NoteDao noteDao;

DeleteAllAsyncTask(NoteDao noteDao) {

this.noteDao = noteDao;

}

@Override

protected Void doInBackground(Note… notes) {

noteDao.deleteAllNotes();

return null;

}

}

static class DeleteAsyncTask extends AsyncTask<Note, Void, Void>{

private NoteDao noteDao;

DeleteAsyncTask(NoteDao noteDao) {

this.noteDao = noteDao;

}

@Override

protected Void doInBackground(Note… notes) {

noteDao.deleteNotes(notes);

return null;

}

}

}

3.编写ViewModel+LiveData


为什么要使用ViewModel?

ViewModel 是数据与 UI 分离的中间层,提供了一个将数据转换为 UI 友好型数据的场所。其次,它也提供了多 Fragment 复用相同 ViewModel 的机制。

为什么要使用LiveData?

LiveData 是一个可以感知 Activity 、Fragment生命周期的数据容器。当 LiveData 所持有的数据改变时,它会通知相应的界面代码进行更新。

此处为了方便,数据和界面的交互放在了Activity中,读者有需要可以使用Databinding对Activity进行进一步解耦.

public class NoteViewModel extends AndroidViewModel {

/**

  • 使用数据仓库处理好的数据库交互逻辑

*/

private NoteRepository repository;

public NoteViewModel(@NonNull Application application) {

super(application);

repository = new NoteRepository(application);

}

public LiveData<List> getAllNoteLive() {

return repository.getAllWordLive();

}

public LiveData<List> queryNotesWithPattern(String pattern){

return repository.queryNotesWithPattern(pattern);

}

public void insertNotes(Note… notes){

repository.insertNotes(notes);

}

public void updateNotes(Note… notes){

repository.updateNotes(notes);

}

public void deleteNotes(Note… notes){

repository.deleteNotes(notes);

}

public void deleteAllNotes(){

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值