I am trying to replace NSURLConnection
with NSURLSession
, but I found that with NSURLSession
I couldn't read the intermediate data chunks like I did in NSURLConnection
with the delegate method.
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
I used to start using the data before completion. I am kind of streaming. How I can access the downloaded data before NSURLSession
complete?
I notice there is a (NSURL *)location
which is temporary saved data location from NSURLSession
before completion, but I can I get this URL before completion?
Thanks
Tried this as suggested from Rob:
NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
NSURLSession *session = [NSURLSession sessionWithConfiguration:configuration delegate:self delegateQueue:[NSOperationQueue mainQueue]];
NSURLSessionTask *task = [session dataTaskWithRequest:request];
[task resume];
However, only didRecieveResponse was called
- (void)URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)dataTask didReceiveResponse:(NSURLResponse *)response completionHandler:(void (^)(NSURLSessionResponseDisposition))completionHandler
but didReceiveData not called.
I tried to change the task to downloadTask
NSURLSessionConfiguration *defaultConfigObject = [NSURLSessionConfiguration defaultSessionConfiguration];
NSURLSession *defaultSession = [NSURLSession sessionWithConfiguration: defaultConfigObject delegate:self delegateQueue: [NSOperationQueue mainQueue]];
NSURLSessionDownloadTask * downloadTask =[defaultSession downloadTaskWithRequest:request];
[downloadTask resume];
It works with the download delegate, but data delegate (didReceiveData) not called.
Anybody can tell me what did I get wrong?