Atlas Device SDK 已弃用。 有关详细信息,请参阅弃用页面。
版本 10.6.0 中的新增内容。
您可以使用 RealmDictionary数据类型来管理由唯一的 String键值对组成的集合。 RealmDictionary实现了 Java 的Map接口,因此它的工作方式与内置HashMap类相同,不同之处在于托管RealmDictionary实例将其内容持久保存到域中。 包含Realm对象的RealmDictionary实例会存储对这些对象的引用。 当您从域中删除Realm 对象时, RealmDictionary中对该对象的任何引用都会变为null值。
使用
要创建RealmDictionary类型的字段,请定义RealmDictionary<T>类型的对象属性,其中T定义要存储在RealmDictionary中的值。 目前, RealmDictionary实例只能使用String类型的键。
下表显示了可以使用哪些方法与RealmDictionary一起完成常见collection任务:
任务 | 方法 |
|---|---|
将对象添加到 | put() (或 Kotlin 中的 |
将多个对象添加到 | |
检查字典是否包含特定键 | |
检查字典是否包含特定值 |
import io.realm.RealmDictionary; import io.realm.RealmObject; public class Frog extends RealmObject { String name; RealmDictionary<Frog> nicknamesToFriends; // realm-required empty constructor public Frog() {} public String getName() { return name; } public void setName(String name) { this.name = name; } public RealmDictionary<Frog> getNicknamesToFriends() { return nicknamesToFriends; } public void setNicknamesToFriends(RealmDictionary<Frog> nicknamesToFriends) { this.nicknamesToFriends = nicknamesToFriends; } }
Frog frog = realm.createObject(Frog.class); frog.setName("George Washington"); // get the RealmDictionary field from the object we just created RealmDictionary<Frog> dictionary = frog.getNicknamesToFriends(); // add key/value to the dictionary Frog wirt = realm.createObject(Frog.class); wirt.setName("Wirt"); dictionary.put("tall frog", wirt); // add multiple keys/values to the dictionary Frog greg = realm.createObject(Frog.class); greg.setName("Greg"); Frog beatrice = realm.createObject(Frog.class); beatrice.setName("Beatrice"); dictionary.putAll(Map.of("small frog", greg, "feathered frog", beatrice)); // check for the presence of a key Assert.assertTrue(dictionary.containsKey("small frog")); // check for the presence of a value Assert.assertTrue(dictionary.containsValue(greg)); // remove a key dictionary.remove("feathered frog"); Assert.assertFalse(dictionary.containsKey("feathered frog")); // deleting a Realm object does NOT remove it from the dictionary int sizeOfDictionaryBeforeDelete = dictionary.size(); greg.deleteFromRealm(); // deleting greg object did not reduce the size of the dictionary Assert.assertEquals(sizeOfDictionaryBeforeDelete, dictionary.size()); // but greg object IS now null: Assert.assertEquals(dictionary.get("small frog"), null);
import io.realm.RealmDictionary import io.realm.RealmObject open class Frog : RealmObject() { var name: String? = null var nicknamesToFriends: RealmDictionary<Frog> = RealmDictionary<Frog>() }
val frog = realm.createObject(Frog::class.java) frog.name = "George Washington" // get the RealmDictionary field from the object we just created val dictionary = frog.nicknamesToFriends // add key/value to the dictionary val wirt = realm.createObject(Frog::class.java) wirt.name = "Wirt" dictionary["tall frog"] = wirt // add multiple keys/values to the dictionary val greg = realm.createObject(Frog::class.java) greg.name = "Greg" val beatrice = realm.createObject(Frog::class.java) beatrice.name = "Beatrice" dictionary.putAll(mapOf<String, Frog>( Pair("small frog", greg), Pair("feathered frog", beatrice))) // check for the presence of a key Assert.assertTrue(dictionary.containsKey("small frog")) // check for the presence of a value Assert.assertTrue(dictionary.containsValue(greg)) // remove a key dictionary.remove("feathered frog") Assert.assertFalse(dictionary.containsKey("feathered frog")) // deleting a Realm object does NOT remove it from the dictionary val sizeOfDictionaryBeforeDelete = dictionary.size greg.deleteFromRealm() // deleting greg object did not reduce the size of the dictionary Assert.assertEquals( sizeOfDictionaryBeforeDelete.toLong(), dictionary.size.toLong() ) // but greg object IS now null: Assert.assertEquals(dictionary["small frog"], null)
通知
要订阅对RealmDictionary的更改,请将 MapChangeListener实施传递给RealmSet.addChangeListener 方法。 您的MapChangeListener实施必须定义一个onChange()方法,该方法接受对已更改RealmDictionary的引用和一设立更改作为参数。 您可以通过MapChangeSet参数访问权限添加到字典中的键以及从字典中删除的键。
AtomicReference<Frog> frog = new AtomicReference<Frog>(); realm.executeTransaction(r -> { frog.set(realm.createObject(Frog.class)); frog.get().setName("Jonathan Livingston Applesauce"); }); MapChangeListener<String, Frog> mapChangeListener = new MapChangeListener<String, Frog>() { public void onChange(RealmMap<String, Frog> map, MapChangeSet<String> changes) { for (String insertion : changes.getInsertions()) { Log.v("EXAMPLE", "Inserted key: " + insertion + ", Inserted value: " + map.get(insertion).getName()); } } }; frog.get().getNicknamesToFriends().addChangeListener(mapChangeListener); realm.executeTransaction(r -> { // get the RealmDictionary field from the object we just created RealmDictionary<Frog> dictionary = frog.get().getNicknamesToFriends(); // add key/value to the dictionary Frog wirt = realm.createObject(Frog.class); wirt.setName("Wirt"); dictionary.put("tall frog", wirt); // add multiple keys/values to the dictionary Frog greg = realm.createObject(Frog.class); greg.setName("Greg"); Frog beatrice = realm.createObject(Frog.class); beatrice.setName("Beatrice"); dictionary.putAll(Map.of("small frog", greg, "feathered frog", beatrice)); });
var frog: Frog? = null realm.executeTransaction { r: Realm? -> frog = realm.createObject(Frog::class.java) frog?.name = "Jonathan Livingston Applesauce" } val mapChangeListener: MapChangeListener<String, Frog> = MapChangeListener<String, Frog> { map, changes -> for (insertion in changes.insertions) { Log.v("EXAMPLE", "Inserted key: $insertion, Inserted value: ${map[insertion]!!.name}") } } frog?.nicknamesToFriends?.addChangeListener(mapChangeListener) realm.executeTransaction { r: Realm? -> // get the RealmDictionary field from the object we just created val dictionary = frog!!.nicknamesToFriends // add key/value to the dictionary val wirt = realm.createObject(Frog::class.java) wirt.name = "Wirt" dictionary["tall frog"] = wirt // add multiple keys/values to the dictionary val greg = realm.createObject(Frog::class.java) greg.name = "Greg" val beatrice = realm.createObject(Frog::class.java) beatrice.name = "Beatrice" dictionary.putAll(mapOf<String, Frog>( Pair("small frog", greg), Pair("feathered frog", beatrice))) }