這是爲什麼抱怨失誤型? map
應該transfor可觀察到其他的價值,但它希望我從地圖在Kotlin中使用RXJava地圖?
override fun fetchExercises(): Observable<List<Exercise>> {
return FirebaseDatabase.getInstance()
.getReference("exercises")
.observeSingleEvent()
.map { snapshot: DataSnapshot? -> object: List<Exercise>
// Error here
return listOf<Exercise>()
}
}
fun DatabaseReference.observeSingleEvent(): Observable<DataSnapshot> {
return Observable.create(object : Observable.OnSubscribe<DataSnapshot> {
override fun call(subscriber: Subscriber<in DataSnapshot>) {
val valueEventListener = object: ValueEventListener {
override fun onDataChange(snapshot: DataSnapshot?) {
subscriber.onNext(snapshot)
subscriber.onCompleted()
}
override fun onCancelled(error: DatabaseError?) {
subscriber.onError(FirebaseDatabaseThrowable(error))
}
}
addListenerForSingleValueEvent(valueEventListener)
}
})
}
這很有道理,謝謝。在寫Kotlin時需要讓Swift擺脫我的頭腦:) – aryaxt