aschuch / StatefulViewController

Placeholder views based on content, loading, error or empty states

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Loading state is not a initial state?

kidsid-Ixigo opened this issue · comments

When I open my view controller I can see my empty view momentarily then it switches back to loading state. How to avoid that? Either you introduce a new "Initial State" or make Loading state by default state.

I always face this issue when showing empty states, and always came to a different workaround.
You can try to set your loading when the VC disappears, when you have an empty state to show:

    override func viewDidDisappear(_ animated: Bool) {
        super.viewDidDisappear(animated)
   
        if !hasContent() {
            startLoading()
        }
    }

It worked for me.

I am unable to reproduce this issue. The example project calls startLoading() from viewWillAppear to make sure the loading state is shown immediately.

I think this may be the same issue I was seeing here (and I am seeing again): #47

Since the view presentation is being pushed to a subsequent run loop with DispatchQueue.main.sync(), perhaps it is undefined as to whether it occurs in time to mask the content view? I personally only see the issue intermittently.

As I suggested before, you could guarantee that this can't happen in the viewWillAppear scenario by doing something like this in transitionToState(...):

if Thread.isMainThread {
      ...
} else {
   DispatchQueue.main.sync() {
      ...
   }
}

However, again I notice that the issue is complicated further by the async queue call, which sounds like it could be the more likely culprit, i.e. we have no idea when this code will be executed in relation to the content view's presentation on the main thread.

Perhaps if animated == false and the queue is empty, the presentation should just happen 'now' on the main thread?