Earlier today I asked the following question: iOS block being stoped when view pushed
The operation I mentioned (OP1) is actually a \"http get\" to my server, using NSUR
I'm not sure I understand the work flow that goes on in the first controller -- specifically, what the user does to initiate the download, and what else he does before the next controller gets presented (and when that controller gets instantiated). When I've made apps in the past that required doing downloads from multiple classes, I've created a download class that creates the NSURLConnection, and implements all the call backs. It has one delegate protocol method to send back the data (either raw data or error object) to its delegate.
I made a simple test case simulating what I think your work flow is, using two buttons. One instantiates a Downloader class instance, creates the next controller, sets it as the delegate of the downloader, and starts the download. The second button does the push to that second controller. This works, no matter when the push happens, but I don't know if it's relevant to your situation (I test using the Network Link Conditioner to simulate a slow connection).
The first Controller:
#import "ViewController.h"
#import "ReceivingViewController.h"
#import "Downloader.h"
@interface ViewController ()
@property (strong,nonatomic) ReceivingViewController *receiver;
@end
@implementation ViewController
-(IBAction)buttonClicked:(id)sender {
Downloader *loader = [Downloader new];
self.receiver = [self.storyboard instantiateViewControllerWithIdentifier:@"Receiver"];
loader.delegate = self.receiver;
[loader startLoad];
}
-(IBAction)goToReceiver:(id)sender {
[self.navigationController pushViewController:self.receiver animated:YES];
}
The Download class .h:
@protocol DownloadCompleted
-(void)downloadedFinished:(id) dataOrError;
@end
@interface Downloader : NSObject
@property (strong,nonatomic) NSMutableData *receivedData;
@property (weak,nonatomic) id delegate;
-(void)startLoad;
Downloader .m:
-(void)startLoad {
NSLog(@"start");
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.google.com"] cachePolicy:NSURLRequestReloadIgnoringLocalAndRemoteCacheData timeoutInterval:10];
NSURLConnection *connection = [NSURLConnection connectionWithRequest:request delegate:self];
if (connection) self.receivedData = [NSMutableData new];
}
-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
self.receivedData.length = 0;
}
-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
[self.receivedData appendData:data];
}
-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
[self.delegate downloadedFinished:error];
}
-(void)connectionDidFinishLoading:(NSURLConnection *)connection {
[self.delegate downloadedFinished:self.receivedData];
}
-(void)dealloc {
NSLog(@"In Downloader dealloc. loader is: %@",self);
}
The second controller:
@interface ReceivingViewController ()
@property (strong,nonatomic) NSData *theData;
@end
@implementation ReceivingViewController
-(void)downloadedFinished:(id)dataOrError {
self.theData = (NSData *)dataOrError;
NSLog(@"%@",self.theData);
}
-(void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
NSLog(@"%@",self.theData);
}