ObjectBox 集成指南,建议细读
? 实体类格式(最简单的只要加两个注解就够了,更详细的用法可以参考官方文档):
package io.objectbox.example;
import java.util.Date;
import io.objectbox.annotation.Entity;import io.objectbox.annotation.Generated;import io.objectbox.annotation.Id;import io.objectbox.annotation.apihint.Internal;
@Entitypublic class Note {
// 注意这里的 @Id 注解是必须的,和 GreenDao 不同,GreenDao 可以省略,但是如果你的业务字段已经有了 一个名字为 id 的字段,可以取一个别的名字啊~@Idlong boxId;
String text;String comment;Date
date;
public Note(long id, String text, String comment, Date date) {this.boxId = id;this.text = text;this.comment = comment;this.date = date;}
public Note() {}
public long getId() {return this.boxId;}
public void setId(long id) {this.boxId = id;}
public String getText() {return this.text;}
public void setText(String text) {this.text = text;}
public String getComment() {return this.comment;}
public void setComment(String comment) {this.comment = comment;}
public Date getDate() {return this.date;}
public void setDate(Date date) {this.date = date;}
}
? 在 Activity 执行查询(多余的业务代码已经被我省略):
public class NoteActivity extends Activity {
private Box<Note> notesBox;private Query<Note> notesQuery;
@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.main);
BoxStore boxStore = ((App) getApplication()).getBoxStore();notesBox = boxStore.boxFor(Note.class);
// query all notes, sorted a-z by their text(http://greenrobot.org/objectbox/documentation/queries/)notesQuery = notesBox.query().order(Note_.text).build();updateNotes();}
/** Manual trigger to re-query and update the UI. For a reactive alternative check {@link ReactiveNoteActivity}. */private void updateNotes() {List<Note> notes = notesQuery.find();}
private void addNote() {Note note = new Note();note.setText(noteText);note.setComment(comment);note.setDate(new Date());notesBox.put(note);Log.d(App.TAG, "Inserted new note, ID: " + note.getId());}
}
ObjectBox 的 Reactive 用法
? 同样在 Activity 中执行查询:
/** An alternative to {@link NoteActivity} using a reactive query (without RxJava, just plain ObjectBox API). */public class ReactiveNoteActivity extends Activity {
private Box<Note> notesBox;private Query<Note> notesQuery;private DataSubscriptionList subscriptions = new DataSubscriptionList();
@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.main);
notesBox = ((App) getApplication()).getBoxStore().boxFor(Note.class);
// query all notes, sorted a-z by their text// (http://greenrobot.org/objectbox/documentation/queries/)notesQuery = notesBox.query().order(Note_.text).build();
// Reactive query (http://greenrobot.org/objectbox/documentation/data-observers- reactive-extensions/)notesQuery.subscribe().onError(new ErrorObserver() {@Overridepublic void onError(Throwable th) {
}})// 官方推荐的做法是对 data observers 持有弱引用,防止忘记 cancel subscriptions,// 但是最好还是记得及时 cancel subscriptions(例如在 onPause、onStop 或者// onDestroy 方法中).weak().on(AndroidScheduler.mainThread()).observer(new DataObserver<List<Note>>() {@Overridepublic void onData(List<Note> notes) {// 只要数据库里的数据发生了变化,这里的方法就会被回调执行,相当智能。。。// 业务代码}});}
@Overrideprotected void onDestroy() {subscriptions.cancel();super.onDestroy();}
private void addNote() {Note note = new Note();note.setText(noteText);note.setComment(comment);note.setDate(new Date());notesBox.put(note);Log.d(App.TAG, "Inserted new note, ID: " + note.getId());}}
上面的用法看上去就像傻瓜版的 RxJava,上手容易,概念理解也简单,但是并没有 RxJava 那么强大的功能,所以如果在应对更复杂的业务逻辑的时候,还是需要引入 RxJava ,示例如下:
Query query = box.query().build();RxQuery.observable(query).subscribe(this);
RxQuery 可以使用 Flowable、Observable、Single 来订阅查询结果,目前 ObjectBox 只支持 RxJava 2 。
评论