aschuch / StatefulViewController

Placeholder views based on content, loading, error or empty states
MIT License
2.14k stars 149 forks source link

DispatchQueue.main.sync slightly delays showing initial loading view #47

Closed FWJonathan closed 7 years ago

FWJonathan commented 7 years ago

If already on the main thread, the call to DispatchQueue.main.sync in transitionToState(...) will delay a loading view presentation briefly so that the main view is shown before valid data is present.

This could be resolved with something like the following logic:

if Thread.isMainThread {
      ...
} else {
   DispatchQueue.main.sync() {
      ...
   }
}
FWJonathan commented 7 years ago

On further observation I see that this isn't quite so simple as the call happens within an async block in the local queue. The only way I see to workaround this currently is to hide the viewController's view until loading completes, or am I missing something?

aschuch commented 7 years ago

Yes you are right, I am usually calling startLoading() from viewWillAppear as shown in the example project. Do you still see this behaviour when used from viewWillAppear?

FWJonathan commented 7 years ago

Hmm, I was seeing this behaviour when I posted, but I've made several changes to my loading flow since adding a workaround, and after removing the workaround it now seems ok!

It could be that my call to startLoading() itself may have been getting pushed back a cycle, but I can't verify this now. Let's assume that it's ok for now and I'll let you know if it re-occurs. Anyway, thanks for the library, very useful!