Hongmeng development actual combat series: One of hongmeng development actual combat series: rounded corners
EventBus/RxBus
Network Request (native + Retrofit)
preface
As the project from the demo toys gradually into the enterprise applications, simple data storage already cannot satisfy the desire of the product, so we need to find a suitable Yu Hongmeng system database, the database must be in conformity with light weight, high efficiency, the characteristics of simple, cross-platform, open source, through a circle, find a ObjectBox conform to these characteristics.
ObjectBox is a new non-relational database framework developed by the GreenRobot team, which also developed greenDao,EventBus and other popular frameworks. ObjectBox is a super fast database built specifically for IoT and mobile devices. It is the first high performance ACID-compliant NoSQL database. ObjectBox is less than 1MB, so it is ideal for mobile applications, small IoT devices, and IoT gateways.
Main advantages of ObjectBox: 10 times faster than competing products, cross-platform. It runs on Linux, Windows, Mac and iOS, Android, PI, ARM, embedded or Containerized raspberries, and is small in size. Less than 1MB, is a NoSQL database, API simple and easy to use, all the above are moving brick, specific can visit their official website to learn, ticket in this: ObjectBox official website to learn
Integrate using ObjectBox
This article is based on ObjectBox 2.7.1 version, docking into the Hongmeng 2.0 system for use!
1. Environment configuration
1. Build. Gradle in your project directory
Buildscript {dependencies {classpath "IO. Objectbox :objectbox-gradle-plugin:2.7.1"}}Copy the code
2. Add the following code to build.gradle of module
// At the beginning of the file apply plugin: 'com.huawei.ohos.hap' next line apply plugin: 'IO. Objectbox dependencies {implementation' org. Greenrobot: essentials: 3.0.0 - RC1 '}Copy the code
3, add the corresponding JAR package and so to the module liBS, related files can be found in the demo
2. Initialize the ObjectBox framework
1. Create a Javabean object to store
Public class BoxUser {@id public long Id; public int userId; public String userName; }Copy the code
2. Build directly, MyObjectBox class and other classes will be generated in the corresponding directory of build/generated/source of the module, and relevant files will be copied back to the corresponding directory of the project
Initialize the database
private Box boxCreateDb() { if (boxStore == null) { boxStore = MyObjectBox.builder().androidContext(getApplicationContext()).build(); Hilog.warn (new HiLogLabel(hilog.log_app, 0, "===demo==="), "NoSQl database initialized successfully "); } Box box = boxStore.boxFor(BoxUser.class); return box; }Copy the code
3. Basic operations of ObjectBox
1,
private void boxInsert() { BoxUser boxUser = new BoxUser(); boxUser.userId = 1; boxUser.userName = "name1"; boxCreateDb().put(boxUser); Hilog.warn (new HiLogLabel(hilog.log_app, 0, "===demo==="), "NoSQl insert data successfully "); }Copy the code
2, delete
private void boxDelete() { boxCreateDb().query().equal(BoxUser_.userName,"name1").build().remove(); Hilog.warn (new HiLogLabel(hilog.log_app, 0, "===demo==="), "NoSQl insert data delete successfully "); }Copy the code
3, change
private void boxUpdata() { List<BoxUser> result = boxCreateDb().query().build().find(); for(int i = 0 ; i < result.size() ; i ++){ BoxUser boxUser = result.get(i); boxUser.userName = "usernameUpdata"; } boxCreateDb().put(result); Hilog.warn (new HiLogLabel(hilog.log_app, 0, "===demo==="), "NoSQl update data successfully "); }Copy the code
4,
private void boxQuery() { List<BoxUser> result = boxCreateDb().query().build().find(); Hilog.warn (new HiLogLabel(hilog.log_app, 0, "===demo==="), "NoSQl query data count: "+result.size()); for(int i = 0 ; i < result.size() ; I ++){hilog.warn (new HiLogLabel(hilog.log_app, 0, "===demo==="), "NoSQl " +result.get(i).userId + "====="+i+"====" + result.get(i).userName); }}Copy the code
conclusion
ObjectBox other more advanced database operations, such as model changes, transactions, and so on and Android write the same, you can refer to the Android related writing method! Git: github.com/maolinnan/H…
This is the fourth installment in the series, and we’ll be seeing more of it at…… .
If the article has a little inspiration for you, I hope you can click a like, to a wave of attention.