I have a tab bar with 5 view controllers. In my third view controller I have a table view and its didSelect method I am changing my TabBar tab programmatically to a first tab using following code. tabBarController?.selectedViewController = tabBarController.childViewControllers[0]
In my first view controller which is childViewControllers[0]
there is viewDidAppear method including my ActivityIndicator and some API calls. When I changed my tab bar programmatically using above code third view controller stays on the screen and as soon as viewDidAppear method finishes it is switching which causes sense of freezing to a user . However, when I press directly to first view controller first view controller appearing then, shows Activity Indicator making API calls. What could be the reason of it and how can I solve it?
Asked
Active
Viewed 953 times
0

atalayasa
- 3,310
- 25
- 42
-
add some minor delay for API call which will fix the issue of freeze – Vinodh Jun 11 '18 at 09:11
-
Can you let us know, how are you changing View Controller ? – Nilomi Shah Jun 11 '18 at 09:19
-
Issue of freeze is not about API call. As I said when I pressed first tab bar from the tab bar it is showing my activity indicator and making API call. Problem is when I switch my tab bar programmatically in my didSelectRowAt function it is waiting to finish API call. @Vinodh – atalayasa Jun 11 '18 at 10:40
-
It is but obvious, during API call, UI freezes, so what you want instead of that? – Nilomi Shah Jun 11 '18 at 10:45
-
1can you comment the API call and try switching tabbar. If UI not freezing then UI Switching and API call works on same thread which cause the freeze . So delay when switching tabs – Vinodh Jun 11 '18 at 11:09
-
@Vinodh yes when I comment API call it is not freezing. How can I delay it when switching tabs? – atalayasa Jun 11 '18 at 12:37
-
@NilomiShah I need to show something like Activity Indicator user should not wait without any reaction when select something from table view. – atalayasa Jun 11 '18 at 12:37
-
1you can use this method to add delay for calling https://stackoverflow.com/a/28821805/1142743. Post notification from third viewcontroller to firstview when didselect is called in listener add API call with delay – Vinodh Jun 11 '18 at 13:05
-
Thanks dude it is now working like a charm. I put my code inside `DispatchQueue.main.async` as you said @Vinodh – atalayasa Jun 11 '18 at 13:07
-
post it as answer so other will know the reason for freeze and what is the fix – Vinodh Jun 11 '18 at 13:10
2 Answers
5
When I changed my tabBarController?.selectedViewController = tabBarController.childViewControllers[0]
code
DispatchQueue.main.async {
tabBarController?.selectedViewController = tabBarController.childViewControllers[0]
}
It works well some API calls in the first view controller are causing to freeze so I put it inside main thread thanks to @Vinodh

atalayasa
- 3,310
- 25
- 42