简体   繁体   中英

Usage of RXJava map in Kotlin?

Why is this complaining about a miss-type? map supposed to transfor the value of an observable to another, but it's expecting me to return an observable from the map

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)
        }
    })
}

This part of your code

.map { snapshot: DataSnapshot? -> object: List<Exercise>

says that the type of the snapshot parameter that this lambda receives is DataSnapshot? -> object: List<Exercise> DataSnapshot? -> object: List<Exercise> (which actually isn't a valid function type, that would be DataSnapshot? -> List<Exercise> ).

I believe what you actually wanted to do is the following, a snapshot parameter that just has the type DataSnapshot :

.map { snapshot: DataSnapshot? ->
    listOf<Exercise>()
}

The return type of the lambda doesn't have to be specified here, it will just be inferred. You also don't need to use the return keyword in a lambda, because the last expression in it is what's returned.

@zsmb13 has pointed out the correct answer right there. Here, I would like to comment on some mistake made in your lambda expression.

.map { snapshot: DataSnapshot? -> object: List<Exercise>
        // Error here
        return listOf<Exercise>()
}

object: List<Exercise> here is not a valid syntax since the body goes after an -> sign inside the lambda expression. We do not specify the return type inside the body.

Also, the return here means to return from fetchExercises() as described in Kotlin docs :

A return statement without a label always returns from the function declared with the fun keyword. This means that a return inside a lambda expression will return from the enclosing function, whereas a return inside an anonymous function will return from the anonymous function itself.

As what @zsmb13 said, return is not needed in most of the case. If you really want it(for flow control), you can use qualified returns :

//This is answer from @zsmb13
.map { snapshot: DataSnapshot? ->
    return@map listOf<Exercise>()
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM