Performseguewithidentifier Very Slow When Segue Is Modal

performSegueWithIdentifier very slow when segue is modal

Trust me and try this. I have run into this problem a few times.

In Swift 2:

dispatch_async(dispatch_get_main_queue(),{
self.performSegue(withIdentifier:mysegueIdentifier,sender: self)
})

or for Swift 3:

DispatchQueue.main.async {
self.performSegue(withIdentifier: mysegueIdentifier,sender: self)
}

As discussed here and here.

performSegueWithIdentifier:method called from didSelectRowAtIIndexPath slow to show view

you can segue directly from cell in storyboard. There is another delegate method which can use for control segue action

- (BOOL)shouldPerformSegueWithIdentifier:(NSString *)identifier sender:(id)sender {

[super shouldPerformSegueWithIdentifier:identifier sender:sender];

if ([identifier isEqualToString:@"SegueIdentifier"]) {

NSIndexPath *indexPath = [self.tableView indexPathForCell:sender];

if (0 == indexPath.row) {
if (0 == indexPath.row) {
return YES;
}
}
}
return NO;
}

(Xcode 6 beta / Swift) performSegueWithIdentifier has delay before segue

I believe you are not on the main thread when calling

self.delegate?.didReceiveAPIResults(jsonResult)

If you ever are curious whether you are on the main thread or not, as an exercise, you can do NSThread.isMainThread() returns a bool.

Anyway, if it turns out that you are not on the main thread, you must be! Why? Because background threads are not prioritized and will wait a very long time before you see results, unlike the mainthread, which is high priority for the system. Here is what to do... in getInfoFromAPI replace

self.delegate?.didReceiveAPIResults(jsonResult)

with

dispatch_sync(dispatch_get_main_queue())
{
self.delegate?.didReceiveAPIResults(jsonResult)
}

Here you are using GCD to get the main queue and perform the UI update within the block on the main thread.

But be wear, for if you are already on the main thread, calling dispatch_sync(dispatch_get_main_queue()) will wait FOREVER (aka, freezing your app)... so be aware of that.

How to Perform Segue With Delay

You can use GCD's dispatch_after to execute your segue code 2 seconds after the view appears, e.x:

- (void)viewDidAppear:(BOOL)animated 
{
[super viewDidAppear:animated];

double delayInSeconds = 2.0;
dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delayInSeconds * NSEC_PER_SEC));
dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
[self performSegueWithIdentifier:@"splashScreenSegue" sender:self];
});
}

Additionally, please make sure that you remember to call the super implementation when overriding UIViewController's life cycle methods.



Related Topics



Leave a reply



Submit