ObjectBox 集成指南,2021年大厂Android岗面试必问

ObjectBox 集成指南,2021年大厂Android岗面试必问,第1张

ObjectBox 集成指南,2021年大厂Android岗面试必问

public Date getDate() {
return this.date;
}

public void setDate(Date date) {
this.date = date;
}

}

​ 在 Activity 执行查询(多余的业务代码已经被我省略):

public class NoteActivity extends Activity {

private Box notesBox;
private Query notesQuery;

@Override
public 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();
}


private void updateNotes() {
List 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 中执行查询:


public class ReactiveNoteActivity extends Activity {

private Box notesBox;
private Query notesQuery;
private DataSubscriptionList subscriptions = new DataSubscriptionList();

@Override
public 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() {
@Override
public void onError(Throwable th) {

}
})
// 官方推荐的做法是对 data observers 持有弱引用,防止忘记 cancel subscriptions,
// 但是最好还是记得及时 cancel subscriptions(例如在 onPause、onStop 或者
// onDestroy 方法中)
.weak()
.on(AndroidScheduler.mainThread())
.observer(new DataObserver() {
@Override
public void onData(List notes) {
// 只要数据库里的数据发生了变化,这里的方法就会被回调执行,相当智能。。。
// 业务代码
}
});
}

@Override
protected 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 。

调试

​ 添加权限

​ 在 Application 开启调试

boxStore = MyObjectBox.builder().androidContext(App.this).build();
if (BuildConfig.DEBUG) {
boolean started = new AndroidObjectBrowser(boxStore).start(this);
Log.i(“ObjectBrowser”, "Started: " + started);
}

​ 执行命令

adb forward tcp:8090 tcp:8090

​ 在电脑浏览器中访问

http://localhost:8090/index.html

学习分享

①「Android面试真题解析大全」PDF完整高清版+②「Android面试知识体系」学习思维导图压缩包——————可以点击我的【Github】免费下载,最后觉得有帮助、有需要的朋友可以点个赞

有帮助、有需要的朋友可以点个赞

[外链图片转存中…(img-8ntl00XH-1643960181162)]

[外链图片转存中…(img-EElkQLQr-1643960181164)]

[外链图片转存中…(img-vaxtz4nw-1643960181165)]

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5721652.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-18
下一篇 2022-12-18

发表评论

登录后才能评论

评论列表(0条)

保存