GreenDao与Rx的完美搭配
转载请注明出处:http://www.cnblogs.com/cnwutianhao/p/6719380.html
作为Android开发者,一定不会对 GreenDao 和 ReactiveX 陌生。
GreenDao 号称Android最快的关系型数据库

ReactiveX Rx是一个编程模型,目标是提供一致的编程接口,帮助开发者更方便的处理异步数据流。

下面我们就通过一个实例,来讲解有无Rx支持的时候GreenDao应该怎么用,实现增删操作。

首先导入需要的库(本文针对的 GreenDao 是 3.x 版本, Rx 是 1.x 版本)
GreenDao导入需求库的说明: https://github.com/greenrobot/greenDAO/
在 build.gradle(Project:Xxx) 下,添加:
buildscript {
repositories {
...
mavenCentral()
...
}
dependencies {
...
classpath 'org.greenrobot:greendao-gradle-plugin:3.2.2'
...
}
}
在 build.gradle(Module:Xxx) 下,添加:
...
apply plugin: 'org.greenrobot.greendao'
...
dependencies {
...
compile 'org.greenrobot:greendao:3.2.2'
...
}
Rx导入需求库的说明: https://github.com/ReactiveX/RxJava/tree/1.x
在 build.gradle(Module:Xxx) 下,添加:
dependencies {
...
compile 'io.reactivex:rxjava:1.2.9'
compile 'io.reactivex:rxandroid:1.2.1'
...
}
需求库添加完之后就可以进入正题了
1.参考GreenDao官方文档,添加必要的类 Note 、 NotesAdapter 、 NoteType 、 NoteTypeConverter
Note :
/**
* Entity mapped to table "NOTE".
*/
@Entity(indexes = {
@Index(value = "text, date DESC", unique = true)
})
public class Note { @Id
private Long id; @NotNull
private String text;
private String comment;
private java.util.Date date; @Convert(converter = NoteTypeConverter.class, columnType = String.class)
private NoteType type; @Generated(hash = 1272611929)
public Note() {
} public Note(Long id) {
this.id = id;
} @Generated(hash = 1686394253)
public Note(Long id, @NotNull String text, String comment, java.util.Date date, NoteType type) {
this.id = id;
this.text = text;
this.comment = comment;
this.date = date;
this.type = type;
} public Long getId() {
return id;
} public void setId(Long id) {
this.id = id;
} @NotNull
public String getText() {
return text;
} /**
* Not-null value; ensure this value is available before it is saved to the database.
*/
public void setText(@NotNull String text) {
this.text = text;
} public String getComment() {
return comment;
} public void setComment(String comment) {
this.comment = comment;
} public java.util.Date getDate() {
return date;
} public void setDate(java.util.Date date) {
this.date = date;
} public NoteType getType() {
return type;
} public void setType(NoteType type) {
this.type = type;
} }
NotesAdapter :
public class NotesAdapter extends RecyclerView.Adapter<NotesAdapter.NoteViewHolder> {
private NoteClickListener clickListener;
private List<Note> dataset;
public interface NoteClickListener {
void onNoteClick(int position);
}
static class NoteViewHolder extends RecyclerView.ViewHolder {
public TextView text;
public TextView comment;
public NoteViewHolder(View itemView, final NoteClickListener clickListener) {
super(itemView);
text = (TextView) itemView.findViewById(R.id.textViewNoteText);
comment = (TextView) itemView.findViewById(R.id.textViewNoteComment);
itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (clickListener != null) {
clickListener.onNoteClick(getAdapterPosition());
}
}
});
}
}
public NotesAdapter(NoteClickListener clickListener) {
this.clickListener = clickListener;
this.dataset = new ArrayList<Note>();
}
public void setNotes(@NonNull List<Note> notes) {
dataset = notes;
notifyDataSetChanged();
}
public Note getNote(int position) {
return dataset.get(position);
}
@Override
public NotesAdapter.NoteViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.item_note, parent, false);
return new NoteViewHolder(view, clickListener);
}
@Override
public void onBindViewHolder(NotesAdapter.NoteViewHolder holder, int position) {
Note note = dataset.get(position);
holder.text.setText(note.getText());
holder.comment.setText(note.getComment());
}
@Override
public int getItemCount() {
return dataset.size();
}
}
NoteType :
public enum NoteType {
TEXT, LIST, PICTURE
}
NoteTypeConverter :
public class NoteTypeConverter implements PropertyConverter<NoteType, String> {
@Override
public NoteType convertToEntityProperty(String databaseValue) {
return NoteType.valueOf(databaseValue);
}
@Override
public String convertToDatabaseValue(NoteType entityProperty) {
return entityProperty.name();
}
}
必要的类添加之后,接下来就是重头戏:
用代码说话,横向比较 GreenDao 在有无 Rx 的支持下应该如何书写
1.初始化类
无 Rx 写法
private NoteDao noteDao;
private Query<Note> notesQuery;
有 Rx 写法
private RxDao<Note, Long> noteDao;
private RxQuery<Note> notesQuery;
2.将记录保存到DAO里
无 Rx 写法
DaoSession daoSession = ((BaseApplication) getApplication()).getDaoSession();
noteDao = daoSession.getNoteDao();
有 Rx 写法
DaoSession daoSession = ((BaseApplication) getApplication()).getDaoSession();
noteDao = daoSession.getNoteDao().rx();
3.查询所有记录,按A-Z分类
无 Rx 写法
notesQuery = noteDao.queryBuilder().orderAsc(NoteDao.Properties.Text).build();
有 Rx 写法
notesQuery = daoSession.getNoteDao().queryBuilder().orderAsc(NoteDao.Properties.Text).rx();
4.初始化View
无 Rx 写法
protected void setUpViews() {
RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recyclerViewNotes);
//noinspection ConstantConditions
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
notesAdapter = new NotesAdapter(noteClickListener);
recyclerView.setAdapter(notesAdapter);
addNoteButton = findViewById(R.id.buttonAdd);
//noinspection ConstantConditions
addNoteButton.setEnabled(false);
editText = (EditText) findViewById(R.id.editTextNote);
editText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if (actionId == EditorInfo.IME_ACTION_DONE) {
addNote();
return true;
}
return false;
}
});
editText.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
boolean enable = s.length() != 0;
addNoteButton.setEnabled(enable);
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void afterTextChanged(Editable s) {
}
});
}
有 Rx 写法
protected void setUpViews() {
RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recyclerViewNotes);
//noinspection ConstantConditions
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
notesAdapter = new NotesAdapter(noteClickListener);
recyclerView.setAdapter(notesAdapter);
addNoteButton = findViewById(R.id.buttonAdd);
editText = (EditText) findViewById(R.id.editTextNote);
//noinspection ConstantConditions
RxTextView.editorActions(editText).observeOn(AndroidSchedulers.mainThread())
.subscribe(new Action1<Integer>() {
@Override
public void call(Integer actionId) {
if (actionId == EditorInfo.IME_ACTION_DONE) {
addNote();
}
}
});
RxTextView.afterTextChangeEvents(editText).observeOn(AndroidSchedulers.mainThread())
.subscribe(new Action1<TextViewAfterTextChangeEvent>() {
@Override
public void call(TextViewAfterTextChangeEvent textViewAfterTextChangeEvent) {
boolean enable = textViewAfterTextChangeEvent.editable().length() > 0;
addNoteButton.setEnabled(enable);
}
});
}
5.更新记录
无 Rx 写法
private void updateNotes() {
List<Note> notes = notesQuery.list();
notesAdapter.setNotes(notes);
}
有 Rx 写法
private void updateNotes() {
notesQuery.list()
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Action1<List<Note>>() {
@Override
public void call(List<Note> notes) {
notesAdapter.setNotes(notes);
}
});
}
6.添加记录
无 Rx 写法
private void addNote() {
String noteText = editText.getText().toString();
editText.setText("");
final DateFormat df = DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.MEDIUM);
String comment = "Added on " + df.format(new Date());
Note note = new Note();
note.setText(noteText);
note.setComment(comment);
note.setDate(new Date());
note.setType(NoteType.TEXT);
noteDao.insert(note);
Log.d("DaoExample", "Inserted new note, ID: " + note.getId());
updateNotes();
}
有 Rx 写法
private void addNote() {
String noteText = editText.getText().toString();
editText.setText("");
final DateFormat df = DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.MEDIUM);
String comment = "Added on " + df.format(new Date());
Note note = new Note(null, noteText, comment, new Date(), NoteType.TEXT);
noteDao.insert(note)
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Action1<Note>() {
@Override
public void call(Note note) {
Log.d("DaoExample", "Inserted new note, ID: " + note.getId());
updateNotes();
}
});
}
7.删除记录
无 Rx 写法
NotesAdapter.NoteClickListener noteClickListener = new NotesAdapter.NoteClickListener() {
@Override
public void onNoteClick(int position) {
Note note = notesAdapter.getNote(position);
Long noteId = note.getId();
noteDao.deleteByKey(noteId);
Log.d("DaoExample", "Deleted note, ID: " + noteId);
updateNotes();
}
};
有 Rx 写法
NotesAdapter.NoteClickListener noteClickListener = new NotesAdapter.NoteClickListener() {
@Override
public void onNoteClick(int position) {
Note note = notesAdapter.getNote(position);
final Long noteId = note.getId();
noteDao.deleteByKey(noteId)
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Action1<Void>() {
@Override
public void call(Void aVoid) {
Log.d("DaoExample", "Deleted note, ID: " + noteId);
updateNotes();
}
});
}
};
最后别忘了新建一个Application类,并添加到Manifest中
public class BaseApplication extends Application {
/**
* A flag to show how easily you can switch from standard SQLite to the encrypted SQLCipher.
*/
public static final boolean ENCRYPTED = true;
private DaoSession daoSession;
@Override
public void onCreate() {
super.onCreate();
DaoMaster.DevOpenHelper helper = new DaoMaster.DevOpenHelper(
this, ENCRYPTED ? "notes-db-encrypted" : "notes-db");
Database db = ENCRYPTED ? helper.getEncryptedWritableDb("super-secret") : helper.getWritableDb();
daoSession = new DaoMaster(db).newSession();
}
public DaoSession getDaoSession() {
return daoSession;
}
}
文中额外可能会用到的库
compile 'com.jakewharton.rxbinding:rxbinding:1.0.1'
compile 'net.zetetic:android-database-sqlcipher:3.5.6'
关注我的新浪微博,获取更多Android开发资讯!
关注科技评论家,领略科技、创新、教育以及最大化人类智慧与想象力!
GreenDao与Rx的完美搭配的更多相关文章
- GreenDao与ReactiveX的完美搭配
转载请注明出处:http://www.cnblogs.com/cnwutianhao/p/6719380.html 作为Android开发者,一定不会对 GreenDao 和 ReactiveX 陌生 ...
- LESS-Middleware:Node.js 和 LESS 的完美搭配
LESS 是一个编写 CSS 的很好的方式 ,让你可以使用变量,嵌套规则,混入以及其它许多有用的功能,它可以帮助您更好地组织你的 CSS 代码. 最近我一直在研究 Node.js ,并想用 less- ...
- HDU4685 Prince and Princess 完美搭配+良好的沟通
意甲冠军:今天,有n王子,m公主.现在给他们配对,与王子会嫁给一个男人,他喜欢.公主无法做出选择. 这标题去咬硬,还有一类似的题目poj1904.那个题目也是给王子与公主配对,但那个是王子公主各n个, ...
- asp.net mvc 加三层架构 完美搭配
http://www.hysql.org/aspnet/20180630/5712.html 先来一张项目的层级结构图: Model:模型层,主要是各种类型.枚举以及ORM框架,框架完成数据库和实体类 ...
- WebClient+Fiddler2完美搭配下载远程页面信息
WebClient可以下载远程页面信息,这个大家应该都知道,核心代码如下: WebClient web = new WebClient(); string url = String.Format(&q ...
- 出错处理完美搭配之perror&exit
对于库函数出错处理有两个十分有用的函数perror和exit: 一.错误报告 perror函数用一种简单统一的方式报告错误.ANSI C中的许多库函数,尤其是I/O函数,会调用操作系统去执行一些工作. ...
- flask和pymongo的完美搭配
1.如何进行mongo数据库的链接 import pymongo client = pymongo.MongoClient(host='192.168.*.*', port=27017,) db_au ...
- EasyNVR完美搭配腾讯云CDN/阿里云CDN进行RTMP、HLS直播加速的使用说明
1.相关资料入口 腾讯云LVB EasyNVR.com 2.加速说明 2.1. 腾讯LVB加速 2.1.1. 开通服务 腾讯云视频LVB开通入口 2.1.2. 登录进入控制台 腾讯云直播控制台 2.1 ...
- 为革命保护视力 --- 给 Visual Studio 换颜色
“为革命,保护视力,预防近视,眼保健操开始......” 这个应该是最老版本的眼保健操了,你听过? 一堆废话 且不说上面这个眼保健操到底有木有用,让眼睛放松下还是很有必要的,尤其是现在天天对着不是手机 ...
随机推荐
- 2017 3-4/5 两天的学习的REVIEW
明天就要去面试啦,去感受一下,估计又是一顿虐,蓝瘦-- 3月4日:计算机安全基础技术与原理方面的学习 密码体制(密码)由五个部分组成: 消息空间(m),密文空间(c),密钥空间(k),加密算法(E), ...
- perl 正则表达式之漫游
废话不多说了,直奔主题~ 一.简单模式进行匹配 将模式写在一对//中即可,匹配对象是$_ 元字符:圆括号() 进行模式分组 点号 . 能匹配任意一个字符,除换行符(\n)以 ...
- Extjs中grid前端分页使用PagingMemoryProxy【二】
在项目中遇到Grid前端分页,本人也是刚接触extjs没多久,为了实现效果,一直找了很久才实现出来,对于代码中的一些也不能详细的说明出来, 不知道能不能帮助到遇到同样问题的朋友,所以将例子代码 ...
- Nginx的Rewrite规则与实例
通过Rewrite规则可以实现规范的URL.根据变量来做URL转向及选择配置,用好Rewrite有时起到事半功倍的效果. 语法 Nginx的Rewrite相比Apache的要好理解很多,主要使用指令有 ...
- leetcode HouseRobber Dp Code
#include <malloc.h> int MAX(int x,int y){ return x>y?x:y;} int rob(int* nums, int numsSize) ...
- 2017,科学使用strace神器(附代码,举栗子)
我感到惊讶,都2017年了,几乎没有人知道他们可以使用strace的了解所有事情.它总是我拔出的第一个调试工具之一,因为它通常在我运行的Linux系统上可用,并且它可以用于解决各种各样的问题. 什么是 ...
- python 使用内置函数sorted对各种数据类型进行排序
python有两个内置的函数用于实现排序,一个是list.sort()函数,一个是sorted()函数. 区别1:list.sort()函数只能处理list类型数据的排序:sorted()则可以处理多 ...
- Android -- 自定义StepView实现个人信息验证进度条
1,项目中要用到个人信息验证的在网上找了一下,好像有封装好了的StepView,首先感谢一下作者,这是作者的地址,效果图如下: 2,正准备撸起袖子就是一顿复制粘贴的时候,发现效果图成这个样子了(其实这 ...
- 解决在eclipse中写ImageView时有警告的问题
Eclipse中写了一个android程序其中main.xml中ImageView哪行是个黄叹号!不知道为什么? 解决办法: android:contentDescription="@str ...
- 在线上服务器上无管理员权限下升级NodeJS版本
前言 最近发现一个线上机器的问题,是因为node版本过低导致的,线上机器的node版本还是0.x版,遂打算升级node版本. 但是发现常规的npm包的n模块无法使用,提示没有权限创建文件夹,导致nod ...