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

ObjectBox 集成指南,超全Android中高级面试复习大纲

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

ObjectBox 集成指南,超全Android中高级面试复习大纲

dependencies {
classpath ‘com.android.tools.build:gradle:3.1.1’
classpath “io.objectbox:objectbox-gradle-plugin:$objectboxVersion”
}
}

​ 然后,在 Module 级别的 build.gradle 文件里添加如下脚本:

dependencies {
// 这一句是添加 RxJava 扩展
compile ‘io.objectbox:objectbox-rxjava:0.9.8’
// 下面这两句是 ObjectBox 很骚气的一个功能——DataBrowser, 通过浏览器来调试和浏览数据库的数据
debugImplementation “io.objectbox:objectbox-android-objectbrowser: o b j e c t b o x V e r s i o n " r e l e a s e I m p l e m e n t a t i o n " i o . o b j e c t b o x : o b j e c t b o x − a n d r o i d : objectboxVersion" releaseImplementation "io.objectbox:objectbox-android: objectboxVersion"releaseImplementation"io.objectbox:objectbox−android:objectboxVersion”
}

// ObjectBox browser dependencies must be set before applying the plugin so it does not add objectbox-android
// (would result in two conflicting versions, e.g. “Duplicate files copied in APK lib/armeabi-v7a/libobjectbox.so”).
apply plugin: ‘io.objectbox’

注意这里的 apply plugin: ‘io.objectbox’ 一定要添加到 dependencies 模块后面(已经踩过这个坑了,直接按照官网的 Get Started 的集成方式有点问题)。

ObjectBox 简单用法

​ 在 Application 中初始化:

public static final String TAG = “ObjectBoxExample”;
public static final boolean EXTERNAL_DIR = false;

private BoxStore boxStore;

@Override
public void onCreate() {
super.onCreate();
boxStore = MyObjectBox.builder().androidContext(App.this).build();
if (BuildConfig.DEBUG) {
new AndroidObjectBrowser(boxStore).start(this);
}

Log.d(“App”, “Using ObjectBox " + BoxStore.getVersion() + " (” +
BoxStore.getVersionNative() + “)”);
}

public BoxStore getBoxStore() {
return boxStore;
}

​ 实体类格式(最简单的只要加两个注解就够了,更详细的用法可以参考官方文档):

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;

@Entity
public class Note {

// 注意这里的 @Id 注解是必须的,和 GreenDao 不同,GreenDao 可以省略,但是如果你的业务字段已经有了 一个名字为 id 的字段,可以取一个别的名字啊~
@Id
long 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 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/)

notesBox = ((App) getApplication()).getBoxStore().boxFor(Note.class);

// query all notes, sorted a-z by their text
// (http://greenrobot.org/objectbox/documentation/queries/)

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

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

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