栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

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

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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)]

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/726312.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号