Skip to content

Should the new async boolean in main thread scheduler help recyclerview preserve scroll state on configChange? #444

@ursusursus

Description

@ursusursus

Hi,
I am trying the new update, and was hoping it would solve the issue when emit already comes from main thread, so observeOn main thread doesnt wait for next frame.

Where I notice this the most is recylcerview. If stream of adapter data is kept synchnronous, on rotation - scroll state is kept / restored. If stream is async - scroll state is reset.

When using the new api, Id expect for it to be able to restore the scroll state on replayed, startWith, etc . emits. However this is not the case, even on a minimal example.

class FooActivity : AppCompatActivity() {
    private lateinit var disposable: Disposable

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.controller_workflow_jobs)

        val adapter = SimpleAdapter(...)

        val recyclerView = findViewById<RecyclerView>(R.id.recyclerView).apply {
            setHasFixedSize(true)
            this.layoutManager = LinearLayoutManager(this@FooActivity)
            this.adapter = adapter
        }

        disposable = Observable.fromCallable {
            listOf(
                    JobEntity("1", "Foo1", ItemState.REGULAR, "1"),
                    JobEntity("1", "Foo2", ItemState.REGULAR, "1"),
                    JobEntity("1", "Foo3", ItemState.REGULAR, "1"),
                    JobEntity("1", "Foo4", ItemState.REGULAR, "1"),
                    JobEntity("1", "Foo5", ItemState.REGULAR, "1"),
                    JobEntity("1", "Foo6", ItemState.REGULAR, "1"),
                    JobEntity("1", "Foo7", ItemState.REGULAR, "1"),
                    JobEntity("1", "Foo8", ItemState.REGULAR, "1"),
                    JobEntity("1", "Foo9", ItemState.REGULAR, "1"),
                    JobEntity("1", "Foo10", ItemState.REGULAR, "1"),
                    JobEntity("1", "Foo11", ItemState.REGULAR, "1"),
                    JobEntity("1", "Foo12", ItemState.REGULAR, "1"),
                    JobEntity("1", "Foo13", ItemState.REGULAR, "1"),
                    JobEntity("1", "Foo14", ItemState.REGULAR, "1"),
                    JobEntity("1", "Foo15", ItemState.REGULAR, "1")
            )
        }
 
                .observeOn(AndroidSchedulers.mainThread())
                .throwingSubscribe {
                    adapter.setItems(it)
                }
    }

    override fun onDestroy() {
        super.onDestroy()
        disposable.dispose()
    }
}

Shouldnt this work? Why there would be any delay? I.e. I get its async anyways but I am pretty sure it should not take more than 16ms to leak into next frame, or can it? (Nexus 5, Pixel 2) (If I comment out the observeOn it works)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions