Skip to content

realm1.1.1学习及应用

cheyiliu edited this page Aug 10, 2016 · 3 revisions

realm1.1.1用法学习

     -基本用法
          -extends RealmObject
          -RealmList<? extends RealmObject>
          -@Required @Ignore @Index @PrimaryKey
          -An alternative to extending the RealmObject base class is implementing the RealmModel interface and adding the @RealmClass annotation.
          -Relationships
          -All write operations (adding, modifying, and removing objects) must be wrapped in write transactions. A write transaction can either be committed or cancelled.
          -Because RealmObjects are strongly tied to a Realm, they should be instantiated through the Realm directly.
          -Instead of manually keeping track of realm.beginTransaction(), realm.commitTransaction(), and realm.cancelTransaction() you can use the realm.executeTransaction()   / realm.executeTransactionAsync()
          -All fetches (including queries) are lazy in Realm, and the data is never copied.
          -增删查改
          -It is important to note that Realm instances are thread singletons.
          -In-Memory Realm - Define an instance for an un-persisted in-memory Realm.
          -A DynamicRealm is a variant of the conventional Realm that makes it possible to work with Realm data without using RealmObject subclasses. Instead all access are done using Strings instead of Classes.
          -Realm instances are reference counted, which means that if you call getInstance() twice in a thread, you will also have to call close() twice as well.
          -Auto-Refresh - If a Realm instance has been obtained from a thread that is associated with a Looper (the UI thread is by default) then the Realm instance comes with an auto-refresh feature.
          -Schemas
          -Encryption
          -Working With Android(Adapters Intents AsyncTask IntentService)
          -Working With Other Libraries, Gson, Retrofit

     -约束
          -Currently there’s no support for final, transient and volatile fields.
          -Realm supports the following field types: boolean, byte, short, int, long, float, double, String, Date and byte[].
          -Realm model classes are not allowed to extend any other object than RealmObject.
          -Realm files cannot be accessed by concurrent processes.(文档明确指出。测试,两进程里分别两线程循环读写,没见明显异常,但数据完整性不确定?)
          -线程,The only rule to using Realm across threads is to remember that Realm, RealmObject or RealmResults instances cannot be passed across threads.(违反了直接崩溃,java.lang.IllegalStateException: Realm access from incorrect thread. Realm objects can only be accessed on the thread they were created.)
          -The upper limit of class names is 57 characters. Realm for Android prepend class_ to all names, and the browser will show it as part of the name.
          -The length of field names has a upper limit of 63 character.
          -Nested transactions are not supported, and an exception is thrown if they are detected.
          -Strings and byte arrays (byte[]) cannot be larger than 16 MB.
          -RealmObject.hashCode(), the value is not stable, and should be neither used as a key in HashMap nor saved in HashSet

     -升级
          -migration, 更新版本号,更新schema,若model类变化太频繁,这里工作量巨大(只要更新model,realm就要migration)
          -deleteRealmIfMigrationNeeded()

     -其他点
          -与Gson配合
               -Serializing Realm objects to JSON does not work with GSON’s default behavior as GSON will use field values instead of getters and setters.
               -Some JSON APIs will return arrays of primitive types like integers or Strings, which Realm doesn’t support yet.

     -小结: 本质是数据库这点别忘了,一言不合就分表;表是与realmObject对应;表的列的类型有限;

     -参考资料
          -https://realm.io/docs/java/latest/
          -https://realm.io/docs/java/latest/api/overview-summary.html
          -工具Realm Browser
Clone this wiki locally