Introduction to the ObjectBox Database
Objectbox database is a new non-relational database framework developed by the GreenRobot team, which has also developed greenDao,EventBus and other popular frameworks. According to the official introduction, the library has the following features:
- Super fast: Claims to be better than any embedded database ever tested
- Object-oriented API: No Rows, Columns, and SQL, completely zero-based objects (no ORM, no SQLite)
- Reactivity: Simple and powerful responses to data changes, using the Reaction Data Viewer in ObjectBox or integrated with RxJava.
- Multiple platforms: Android and Java already supported,ios and MacOs are being integrated
- Simple threads: Objects returned by the ObjectBox can be run in all threads
- We no longer need to manage the version numbers ourselves for database upgrades. ObjectBox automatically manages them for us (ObjectBox is responsible for adding, deleting, and renaming new object versions of attributes).
configuration
1.Add this to your root build.gradle (project level):
Buildscript {ext.objectboxVersion = '2.2.0' dependencies {classpath "io.objectbox:objectbox-gradle-plugin:$objectboxVersion" } }Copy the code
2.And this to our app’s build.gradle (module level):
apply plugin: 'io.objectbox' // after applying Android plugin
dependencies {
debugImplementation"io.objectbox:objectbox-android-objectbrowser:$objectboxVersion"
releaseImplementation"io.objectbox:objectbox-android:$objectboxVersion"
}
Copy the code
Use 3.
- Creating an entity Class
import io.objectbox.annotation.Entity import io.objectbox.annotation.Id @Entity class UserInfo { /** * id : 1 * nickname: sex: 30 */ @id (assignable = true) var Id: Long = 1 var nickname: String? = null var sex: Int = 0 constructor( id: Long, nickname: String? , sex: Int, ) { this.id = id this.nickname = nickname this.sex = sex } constructor() }Copy the code
- Unified management of database operations
import io.objectbox.Box; import io.objectbox.BoxStore; public class ObjectBoxDataManager { private static ObjectBoxDataManager objectBoxDataManager; public BoxStore boxStore; public Box userInfoBox; @param application */ public void init(application Application) {boxStore = MyObjectBox.builder().androidContext(application).build(); initUserInfoBox(); } public static synchronized ObjectBoxDataManager getInstance() { if (objectBoxDataManager == null) { objectBoxDataManager = new ObjectBoxDataManager(); } return objectBoxDataManager; } private void initUserInfoBox() {// Table class userInfoBox = boxStore.boxfor (userinfo.class); } /** * add * @param userInfo */ public void insertUserInfo(userInfo userInfo) {userInfobox.removeall (); userInfoBox.put(userInfo); } /** * delete */ public void clearUserInfo() {userInfobox.removeall (); } @param userInfo */ public void updateUserInfo(userInfo userInfo) {List< userInfo > List = userInfoBox.query().build().find(); if (list! =null&&! list.isEmpty()){ int pos=-1; UserInfo bean; for (int i = 0; i <list.size() ; i++) { bean=list.get(i); if (bean.getId()==userInfo.getId()){ pos=i; } } if (pos==-1){ insertUserInfo(userInfo); }else { userInfoBox.remove(list.get(pos)); userInfoBox.put(userInfo); Public UserInfo queryUserInfo() {return (UserInfo) {return (UserInfo); userInfoBox.query().build().findUnique(); }}Copy the code
- Database Unified Operation Management class initialization
ObjectBoxDataManager.getInstance().init(this)
Copy the code
- Add and delete
// add var userInfo= userInfo () userinfo.nickname =" "userinfo.sex =30 ObjectBoxDataManager. GetInstance (). InsertUserInfo (the userInfo) / / delete ObjectBoxDataManager. GetInstance () clearUserInfo () / / change ObjectBoxDataManager. GetInstance (). UpdateUserInfo (the userInfo)/check/var queryUserInfo = ObjectBoxDataManager.getInstance().queryUserInfo()Copy the code