Realm (Java) database using the documentation (Getting Started)

1.1 Basic requirements

  • Android Studio 1.5.1+
  • JDK 7.0+
  • The latest version of the Android SDK
  • The latest Realm 7.0 version: Android API 16+ (Android 4.1+).

Description: Realm Java other than Android does not support. Eclipse does not support the development, migrate to Android Studio.

1.2 Installation

Gradle plugin configuration.

Step 1: In the project-level build.gradleadd-dependent path to the file.

Using the test version 7.0.0-beta:

buildscript {
    repositories {
        jcenter()
        google()
        maven {
            url 'http://oss.jfrog.org/artifactory/oss-snapshot-local'
        }
    }
    dependencies {
        classpath "io.realm:realm-gradle-plugin:7.0.0-beta-SNAPSHOT"
    }
}

allprojects {
    repositories {
        jcenter()
        google()
        maven {
            url 'http://oss.jfrog.org/artifactory/oss-snapshot-local'
        }
    }
}

Using the latest stable version:

buildscript {
    repositories {
        jcenter()
    }
    dependencies {
        classpath "io.realm:realm-gradle-plugin:6.0.2"
    }
}

Continue to ensure the project is "Projects" view. Click the top left corner of Studio Android drop-down menu select "Projects":

image

In the "Projects" view, select the item level build.gradle file:

image

Step 2: In the application-level build.gradlereferences realm-androidplug.

apply plugin: 'realm-android'

In the "Projects" view, find the application-level build.gradlefile:

image

After the completion of these two configurations, dependencies can only sync gradle. If the Realm upgrading from versions prior to v0.88, you also need to clean up gradle project (./gradlew clean).

The following are two modified build.gradleexamples of file:

Whether you want to use Realm synchronize all database?
All documents related to the synchronization has been moved to our platform document .

Other build system

It does not support the Maven and Ant build system. Interested can look at GitHub:

ProGuard configuration library provided as part of the Realm. This means that you do not need to add any specific rules in the Realm of the ProGuard configuration.

1.2.1 Sample Code

View more on GitHub

Realm allows you to secure, lasting and effective way to quickly write the model layer of App. as follows:

// 创建model类并继承RealmObject
public class Dog extends RealmObject {
    private String name;
    private int age;

    // ... getters()和setters()方法 ...
}

public class Person extends RealmObject {
    @PrimaryKey
    private long id;
    private String name;
    private RealmList<Dog> dogs; // 声明一对多关系

    // ... getters()和setters()方法 ...
}

// 像普通的Java对象一样使用它们
Dog dog = new Dog();
dog.setName("Rex");
dog.setAge(1);

// 初始化Realm(每个App仅做一次)
Realm.init(context);

// 获取此线程的Realm实例对象
Realm realm = Realm.getDefaultInstance();

// 查询所有2岁以下的狗
final RealmResults<Dog> puppies = realm.where(Dog.class).lessThan("age", 2).findAll();
puppies.size(); // => 0,因为尚未将任何狗添加到Realm

// 在事务中保留您的数据
realm.beginTransaction();
final Dog managedDog = realm.copyToRealm(dog); // 取得非托管对象
Person person = realm.createObject(Person.class); // 直接创建托管对象
person.getDogs().add(managedDog);
realm.commitTransaction();

// 数据更改时将通知监听器
puppies.addChangeListener(new OrderedRealmCollectionChangeListener<RealmResults<Dog>>() {

    @Override
    public void onChange(RealmResults<Dog> results, OrderedCollectionChangeSet changeSet) {
        // 查询结果通过细粒度的通知实时更新。
        changeSet.getInsertions(); // => [0] is added.
    }
});

// 异步更新后台线程上的对象
realm.executeTransactionAsync(new Realm.Transaction() {

    @Override
    public void execute(Realm bgRealm) {
        Dog dog = bgRealm.where(Dog.class).equalTo("age", 1).findFirst();
        dog.setAge(3);
    }
}, new Realm.Transaction.OnSuccess() {

    @Override
    public void onSuccess() {
        // 原始查询和Realm对象将自动更新。
        puppies.size(); // => 0,因为没有幼犬年龄小于2岁
        managedDog.getAge();   // => 3,狗的年龄被更新
    }
});

1.3 View Realm database

If you need help using the Realm, please see StackOverflow answer for detail.

1.3.1 Realm Studio

Realm Studio is our premier development tool that allows you to easily manage Realm Realm databases and platforms. Use Realm Studio, you can open and edit local and synchronized Realms, and manage any Realm Object Serverinstance. It supports Mac, Windows and Linux.

image

1.3.2 Stetho Realm

You can also Stetho-Realm plugin configuration to Stetho, this is Android's Chrome browser debugging tools Facebook created.

Stetho-Realm Realm is not a formal maintenance.

1.4 Realm initialization

You must be initialized before you can use Realm in the application, this needs to be performed once.

Realm.init(context);

You must provide an Android Context object. Initialization Realm best onCreate on MyApplication in:

public class MyApplication extends Application {

  @Override
  public void onCreate() {
    super.onCreate();
    Realm.init(this);
  }
}

If you create your own application, you have to be registered to the AndroidManifest.xml file:

<application
  android:name=".MyApplication"
  ...
/>
Published 59 original articles · won praise 88 · views 190 000 +

Guess you like

Origin blog.csdn.net/geofferysun/article/details/104977104