Android--> Realm (Database ORM) usage experience, lambda expression

Source: Internet
Author: User
Tags jcenter

Realm, born for mobile devices. Instead of SQLite and Core Data.

Very fortunate that the official help document is in Chinese: https://realm.io/cn/docs/java/latest/
Although the latest version number 0.88.3, even the 1.0 version number is not announced ... But it does not affect her strength at all;

API Document: https://realm.io/docs/java/latest/api/

Because of the official help documents, there are Chinese ... Everyone seems very easy to understand, I just based on personal experience, write experience.

References to 1:realm

Project-level Build.gradle files:

buildscript {    repositories {        jcenter()    }    dependencies {        ‘com.android.tools.build:gradle:2.1.0-beta3‘        ‘me.tatarka:gradle-retrolambda:3.3.0-beta4‘//lambda表达式配置步骤1        "io.realm:realm-gradle-plugin:0.88.3"//注意此行    }}allprojects {    repositories {        jcenter()    }}

Module-level Build.gradle

‘com.android.application‘‘me.tatarka.retrolambda‘ //lambda表达式配置步骤2‘realm-android‘ //注意此行android {    ...    //lambda表达式配置步骤3,结束.    compileOptions {        "UTF-8"        targetCompatibility JavaVersion.VERSION_1_8        sourceCompatibility JavaVersion.VERSION_1_8    }}dependencies {    ...}

(Egg: Above, includes configuration steps for lambda expressions ...) You know)

2: The operation of adding and deleting changes

//Encapsulated operation class Public  class rrealm {  /** * The method must be called */   Public Static void Init(context context, String name,BooleanClean) {Realmconfiguration configuration =NewRealmconfiguration.builder (context). Name. Build ();if(Clean)      {realm.deleterealm (configuration);  } realm.setdefaultconfiguration (configuration); } Public StaticRealmRealm() {returnRealm.getdefaultinstance (); }/** * is usually able to direct this method * /   Public Static void operate(Onoperate operate) {if(Operate! =NULL) {Realm realm = Realm ();          Realm.begintransaction ();          Operate.on (Realm);          Realm.committransaction ();      Realm.close (); }  } Public  interface onoperate {      voidOn (Realm realm); }}
Private void Add() {rrealm.operate (realm) { for(inti =0; i < Num;            i++) {Testrealmobject realmobject = Realm.createobject (Testrealmobject.class);            Realmobject.setage (i); Realmobject.setname ("Name"+ i); Realmobject.settest ("Test"+ i);        Realmobject.settime (System.currenttimemillis ()); }    });}Private void Delete() {rrealm.operate (realm) {realmresults<testrealmobject> all = realm.where(Testrealmobject.class). FindAll ();    All.clear (); });}Private void Update() {rrealm.operate (realm) {realmresults<testrealmobject> all = realm.where(Testrealmobject.class). FindAll (); for(inti =0; I < all.size (); i++) {TestrealmobjectObject= All.Get(i);Object. SetName (Object. GetName () +"New");Object. Settest (Object. Gettest () +"New"); }    });}Private void Query() {rrealm.operate (realm) {realmquery<testrealmobject>where= Realm.where(Testrealmobject.class); realmresults<testrealmobject> all =where. FindAll (); for(TestrealmobjectObject: All) {e (Object. toString ()); }    });}

3: Performance evaluation

//測试类publicclass TestRealmObject extends RealmObject {    public String name;    publicint age;    publiclong time;    public String test;}//插入1000条数据: 149 毫秒 148  158  118//查询1000条数据: 146 毫秒 183  178  192//删除1000条数据: 39  毫秒  44   37   38//更新1000条数据: 212 毫秒 195  185  216//数据仅供參考

Remind again: The official has the Chinese document, everybody can read concretely, will be better;

At this point: The article is over, if there is doubt: QQ Group: 274306954 welcome your increase.

Android--&gt; Realm (Database ORM) usage experience, lambda expression

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.