I have a TableViewController
with a button that triggers an [NSURLConnection sendAsynchronousRequest...]
event, and also loads a modal segue through performSegueWithIdentifier:sender: which targets a small view controller. The purpose of this overlay view controller is to show a loading graphic and to prevent user interaction while the data is sent through the NSURLConnection
.
In the completion block of the NSURLConnection
, I call a method which removes the data in the TableViewController
(its just a batch listing), and then calls dismissViewControllerAnimated:completion:
on the overlay view controller.
Everything works except for dismissing the overlay view controller, which throws a warning in the debugger which says: "Warning: Attempt to dismiss from view controller while a presentation or dismiss is in progress!"
I have found various questions and answers about this error, particularly about using the performSelector:object:withDelay
methods, but so far nothing has worked.
This is particularly annoying because I use a similar process in another area of the app, except that the dismissViewController is called from selecting a UITableViewCell
, and this works fine...
The relevant bits of my code are shown below:
#import "ViewBatch.h"
@interface ViewBatch ()
@property (strong, nonatomic) LoadingOverlayViewController *loadingOverlay;
@end
@implementation ViewBatch
@synthesize loadingOverlay;
....
- (IBAction)exportBatch:(id)sender
{
if ([productArray count] > 0) {
[self performSegueWithIdentifier:@"loadingSegue" sender:self];
[self processData];
}
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([segue.identifier isEqualToString:@"loadingSegue"]) {
loadingOverlay = segue.destinationViewController;
}
}
- (void)processData
{
// Code to create a file and NSURLRequest...
// ....
NSOperationQueue *queue = [[NSOperationQueue alloc] init];
[NSURLConnection sendAsynchronousRequest:urlRequest
queue:queue
completionHandler:^(NSURLResponse *response, NSData *responseData, NSError *error) {
if ([responseData length] > 0 && error == nil)
{
// Not used for this request yet...
}
else if ([responseData length] == 0 && error == nil)
{
// Success...
[self didSendData];
}
else if (error != nil)
{
// Connection error...
NSLog(@"Error: %@", error);
}
}];
}
- (void)didSendData
{
// Reset the batch...
[productArray removeAllObjects];
[self.tableView reloadData];
[loadingOverlay dismissViewControllerAnimated:YES completion:NULL];
}
And the loading view controller is just:
#import <UIKit/UIKit.h>
@interface LoadingOverlayViewController : UIViewController
@property (weak, nonatomic) IBOutlet UILabel *statusLabel;
@property (weak, nonatomic) IBOutlet UIActivityIndicatorView *activityIndicator;
@end
....
....
#import "LoadingOverlayViewController.h"
@interface LoadingOverlayViewController ()
@end
@implementation LoadingOverlayViewController
- (void)viewDidLoad
{
[super viewDidLoad];
[self.activityIndicator startAnimating];
}
@end