Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Delay in presenting a modal view controller

I have a tab bar based app. There are navigation controllers in all 5 tabs with instances of custom view controller setup properly as root view controllers. This loads just fine. A couple of these view controllers contain table views. I want to show a modal view controller to the user when they select a row in the table view. The (relevant part of) didSelectRowAtIndexPath delegate method looks as follows:

SampleSelectorViewController *sampleVC = [[SampleSelectorViewController alloc] init]; [self presentViewController:sampleVC animated:YES completion:NULL]; 

The modal view controller appears BUT it appears after a very noticeable delay. At times it even requires the user to tap the row a second time. A few things that I have already verified are:

  • Table view's didSelectRowAtIndexPath method is called when the user taps the row
  • The didSelectRowAtIndexPath method does not contain any blocking calls. There are no network operations being performed and the modal view controller's setup does not involve any processing intensive task. The data it displays is static.
  • If I push the new view controller onto the navigation stack (everything else remaining exactly same), it behaves perfectly without any delays. It is only when presented modally that the delay is encountered.

Any ideas/suggestions?

like image 374
Numan Tariq Avatar asked Oct 20 '14 15:10

Numan Tariq


2 Answers

It seems calling presentViewController:animated:completion from within tableView:didSelectRowAtIndexPath: is problematic. It's difficult to find anything that stands out when using the Time Profiler in Instruments, also. Sometimes my modal view comes up in less than a second and other times it takes 4s or even 9s.

I think it's related to the underlying UIPresentationController and layout, which is one of the few things I see when selecting the region of time between tapping on a row and seeing the modal presentation in the Time Profiler.

A Radar exists describing this issue: http://openradar.appspot.com/19563577

The workaround is simple but unsatisfying: delay the presentation slightly to avoid whatever contentious behavior is causing the slowdown.

dispatch_async(dispatch_get_main_queue(), ^{    [self presentViewController:nav animated:YES completion:nil]; }); 
like image 194
azsromej Avatar answered Sep 30 '22 14:09

azsromej


If you call present(:animated:completion:) in tableView(:didSelectRowAt:), selectionStyle == .none for selected tableview cell and you’ve got this strange behavior then try to call tableView.deselectRow(at:animated:) before any operations in tableView(_:didSelectRowAt:).

Did it help?

like image 22
Artem Kirillov Avatar answered Sep 30 '22 14:09

Artem Kirillov