如何查询另一个领域对象内的领域对象的大小
我有两个领域类Competition
和User
。 User
是领域类,也包含在Competition
中:
open class Competition (@PrimaryKey var competitionId: String?=null,
var user: User? =null): RealmObject()
open class User(@PrimaryKey var userId: String?=null,
var name: String? =null): RealmObject()
在某个时候,我想在竞赛中的特定CompetitionId中获得User的大小,而我对自己认为微不足道的问题却遇到了挑战
这是我尝试过的
var userObj = realm.where<Competition>().equalTo(
"competitionId", competitionId
).findFirst().user.realm.where<User>().findAll().size
这给了我
kotlin.KotlinNullPointerException
Stacktrace:
2019-03-12 16:03:25.112 22028-22028/co.keepnet.bass W/System.err: kotlin.KotlinNullPointerException
2019-03-12 16:03:25.113 22028-22028/co.keepnet.bass W/System.err: at co.keepnet.bass.ui.MainActivity$onClick$1.execute(MainActivity.kt:41)
2019-03-12 16:03:25.113 22028-22028/co.keepnet.bass W/System.err: at io.realm.Realm.executeTransaction(Realm.java:1431)
2019-03-12 16:03:25.113 22028-22028/co.keepnet.bass W/System.err: at co.keepnet.bass.ui.MainActivity.onClick(MainActivity.kt:37)
2019-03-12 16:03:25.113 22028-22028/co.keepnet.bass W/System.err: at co.keepnet.bass.data.adapter.CompetitionsAdapter$onBindViewHolder$1.onClick(CompetitionsAdapter.kt:75)
2019-03-12 16:03:25.113 22028-22028/co.keepnet.bass W/System.err: at android.view.View.performClick(View.java:6897)
2019-03-12 16:03:25.113 22028-22028/co.keepnet.bass W/System.err: at android.view.View$PerformClick.run(View.java:26101)
2019-03-12 16:03:25.113 22028-22028/co.keepnet.bass W/System.err: at android.os.Handler.handleCallback(Handler.java:789)
2019-03-12 16:03:25.113 22028-22028/co.keepnet.bass W/System.err: at android.os.Handler.dispatchMessage(Handler.java:98)
2019-03-12 16:03:25.113 22028-22028/co.keepnet.bass W/System.err: at android.os.Looper.loop(Looper.java:164)
2019-03-12 16:03:25.114 22028-22028/co.keepnet.bass W/System.err: at android.app.ActivityThread.main(ActivityThread.java:6944)
2019-03-12 16:03:25.114 22028-22028/co.keepnet.bass W/System.err: at java.lang.reflect.Method.invoke(Native Method)
2019-03-12 16:03:25.114 22028-22028/co.keepnet.bass W/System.err: at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:327)
2019-03-12 16:03:25.114 22028-22028/co.keepnet.bass W/System.err: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1374)
答案 0 :(得分:1)
我的意思是, 相当琐碎, 如果 ,您可以利用Realm的功能来发挥自己的优势(我认为3.5.0+)。
open class User(): RealmObject() {
@PrimaryKey var userId: String? = null
var name: String? = null
@LinkingObjects("user")
val userOfCompetitions: RealmResults<Competition>? = null
}
然后
val userCount = realm.where<User>()
.equalTo("userOfCompetitions.competitionId", competitionId)
.count()