我在Android应用中使用Realm DB。数据已成功保存/更新,但是问题是我添加/更新数据时未调用addChangeListener。 这是我的代码(科特琳):
在MainActivity中:
Realm.init(this)
// Get a Realm instance for this thread
val config = RealmConfiguration.Builder().name("coinbundle.realm")
.schemaVersion(1) // Must be bumped when the schema changes
.build()
val realm = Realm.getInstance(config)
val bundles = realm.where(BundleDB::class.java).findAll()
bundles.addChangeListener(RealmChangeListener<RealmResults<BundleDB>> {
Log.d("myanswer","lisnter worked")
})
在BackgroundService中,我这样保存数据:
val config = RealmConfiguration.Builder().name("coinbundle.realm")
.schemaVersion(1) // Must be bumped when the schema changes
.build()
val realm = Realm.getInstance(config)
realm.beginTransaction()
BundleObj = realm.createObject(BundleDB::class.java, UUID.randomUUID().toString())
BundleObj!!.name = "MY_BUNDLE_NAME"
realm.commitTransaction()