Android,Realm,Kotlin检查RealmObject是否具有字段匹配变量,将RealmObject键设置为变量

时间:2019-01-23 15:16:32

标签: android kotlin realm

所以我在Android的SharedPreferences中有布尔值。

我想在onPreferenceChanged侦听器中有条件地更新我的Realm实例中的布尔值。一些布尔值在Realm对象中匹配,而有些则不匹配。

我希望能够查看RealmObject是否具有与Preference Key匹配的键,如果是,则使用Preference值更新RealmObject。

这是代码

private val booleanBindPreferenceSummaryToValueListener = Preference.OnPreferenceChangeListener { preference, value ->

    //log the preference change
    Log.d(TAG, "onBooleanPreferenceChanged: ${preference.key}: $value")
    //open the instance of Realm so we can update changes to the user object and have the information on the server
    val realm = Realm.getDefaultInstance()
    //get the userID from globals
    val userID = Globals.read("UserID", "")
    //make sure that the User ID is not null or blank
    if (!userID.isNullOrBlank()){
       //query the Realm database to get our instance of the user
       val user = realm.where<User>().equalTo("platformUserID", userID).findFirst()!!
       //find out if this particular boolean value is saved to the Realm object    

       //TO DO

    }
    realm.close()
    true

}

在JavaScript中,我只列出对象键并过滤数组以查找任何匹配项,然后我就可以编写类似的内容

  

如果(matches.some === true)用户[preference.key] =偏好值

我是Kotlin的新手,但我认为这应该很容易。此刻正在躲避我。

0 个答案:

没有答案