Skip to content

Correct usage of "using"? #6293

@ursusursus

Description

@ursusursus

Hi, I have a database observable which emits android Cursor. I am mapping this cursor to a POJO. Since Cursor is a native resource it needs to be closed. Therefore i am using using

val queryObservable: Observable<Cursor>
val contactObservable: Observable<Optional<Contact> = queryObservable
                .flatMap { cursor ->
                    Observable.using(
                            { cursor },
                            { Observable.fromCallable { optionalOf(mapContact(it)) } },
                            { it.close() })
                }

As far as I know, this could break if stream is disposed exactly after queryObservable emits (producing the cursor) and before the flatmap, hence leaking the Cursor, right?

Is it possible to even have a Observable< Cursor > then?

Only way I can see this being bulletproof is if the cursor is being created inside the first lambda in using, right? But what If it need to be Observable, i.e. many cursors over time (when database changed while that observable is subscribed)?

All examples of using I have seen were about Socket, where exactly as I said, Socket instance was created in that lambda

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions