android realm api,Realm使用简介

配置要求

Android Studio >= 1.5.1

A recent version of the Android SDK.

JDK version >=7.

We support all Android versions since API Level 9(Android 2.3 Gingerbread & above).

1.在Android项目下面的第一级build.gradle文件中添加依赖

classpath "io.realm:realm-gradle-plugin:2.1.1"

示例:

// Top-level build file where you can add configuration options common to all sub-projects/modules. buildscript {

repositories {

jcenter()

}

dependencies {

classpath 'com.android.tools.build:gradle:2.0.0'

/*realm 配置这一行*/

classpath "io.realm:realm-gradle-plugin:2.1.1"

}

}

......

2.在第二级的build.gradle中使用插件

apply plugin: 'realm-android'

示例:

apply plugin: 'com.android.application'

/*realm 配置这一行*/

apply plugin: 'realm-android'

android {

compileSdkVersion 23

buildToolsVersion "23.0.3"

defaultConfig {

minSdkVersion 18

targetSdkVersion 23

versionCode 1

versionName "1.0"

}

buildTypes {

release {

minifyEnabled false

proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'

}

}

}

......

3.完成1、2步骤之后,rebuild或者clean项目(./gradlew clean),然后在Application中进行初始化操作,话可以对realm进行配置

初始化:

public class MyApplication extends Application {

@Override

public void onCreate() {

super.onCreate();

//使用默认的配置

Realm.init(this);

RealmConfiguration config = new RealmConfiguration.Builder().build();

Realm.setDefaultConfiguration(config);

}

}

自定义配置:

// The RealmConfiguration is created using the builder pattern.

// The Realm file will be located in Context.getFilesDir() with name "myrealm.realm"

RealmConfiguration config = new RealmConfiguration.Builder()

.name("myrealm.realm")

.encryptionKey(getKey())

.schemaVersion(42)

.modules(new MySchemaModule())

.migration(new MyMigration())

.build();

// Use the config

Realm realm = Realm.getInstance(config);

Activity中使用:

public class MyActivity extends Activity {

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

Realm realm = Realm.getDefaultInstance();

// ... Do something ...

realm.close();//用完一定记得记得关闭realm

}

}

4.示例代码:

//我们的实体类需要继承RealmObject

public class User extends RealmObject {

//@PrimaryKey :声明主键

@PrimaryKey

private int id;

// @Required:会检查这个字段是否为空;

//只有Boolean, Byte, Short, Integer, Long, Float, Double, String, byte[],Date类型可以使用 @Required来标记;

@Required

private String name;

private int age;

// @Ignore :建表时忽略这个字段,即数据库中不会存在这个属性

@Ignore

private int sessionId;

// Standard getters & setters generated by your IDE…

public String getName() { return name; }

public void setName(String name) { this.name = name; }

public int getAge() { return age; }

public void setAge(int age) { this.age = age; }

public int getSessionId() { return sessionId; }

public void setSessionId(int sessionId) { this.sessionId = sessionId; }

}

5.增删改查

增:

realm.beginTransaction();

User user = realm.createObject(User.class);

user.setName("John");

user.setEmail("john@corporation.com");

realm.commitTransaction();

User user = new User("John");

user.setEmail("john@corporation.com");

realm.beginTransaction();

User realmUser = realm.copyToRealm(user);

realm.commitTransaction();

删:

// obtain the results of a query

final RealmResults results = realm.where(Dog.class).findAll();

// All changes to data must happen in a transaction

realm.executeTransaction(new Realm.Transaction() {

@Override

public void execute(Realm realm) {

// remove single match

results.deleteFirstFromRealm();

results.deleteLastFromRealm();

// remove a single object

Dog dog = results.get(5);

dog.deleteFromRealm();

// Delete all matches

results.deleteAllFromRealm();

}

});

改:

RealmResults r = realm.where(User.class)

.greaterThan("age", 10) //implicit AND

.beginGroup()

.equalTo("name", "Peter")

.or()

.contains("name", "Jo")

.endGroup()

.findAll();

查:

// Build the query looking at all users:

RealmQuery query = realm.where(User.class);

// Add query conditions:

query.equalTo("name", "John");

query.or().equalTo("name", "Peter");

// Execute the query:

RealmResults result1 = query.findAll();

// Or alternatively do the same all at once (the "Fluent interface"):

RealmResults result2 = realm.where(User.class)

.equalTo("name", "John")

.or()

.equalTo("name", "Peter")

.findAll();

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值