Search code examples
iosobjective-cios6ios7segue

How to perform a segue at the correct place/time within a method


I am looking to create an async method correctly. I am using a View Controller that loads a UIImagePickerController. When the user either takes a photo/or selects one I collect the resulting image in the following delegate method.

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{

Within this method, I scale down the image size if needed with the following:

// Check if the image size is too large from the selected images imageData
    if ((imageData.length/1024) >= 1024) {

If the image does need scaling I run another method. If it is ok in size, I do not do anything and proceed.

Once the above is complete I segue to another controller:

[self performSegueWithIdentifier:@"photoArea" sender:self];

Question
How can I make this method asynchronous? What I need to do is only call the performSegue method once the image scaling is complete... if it was needed. I need to make sure that this is only called at the correct point, rather than running through the method.

At present, the segue can be called whilst the image sizing is still occurring. I know about blocks, but is this the only way to complete asynchronous methods, for something simple like this?


Solution

  • You could create a NSOperationQueue with concurrency of one and submit the scaling operation to it with the segue call as a separate operation following the scaling. If scaling is not needed then the segue would happen immediately.