2

I have implemented both the NSURLConnectionDownloadDelegate, NSURLConnectionDataDelegate delegate methods as given below.

The problem is that after connection:didReceiveResponse: , connectionDidFinishDownloading:destinationURL: is called but not connectionDidFinishLoading: Even connection:didReceiveData: is not called.

When I comment the NSURLConnectionDownloadDelegate methods, the other three are called without any issues.

I have a NSURLConnections which gets JSON from server. The NSURLConnectionDataDownloading delegate methods are used by newsstand to download issues.

How do i manage this?

Here are all the delegate methods than I am implementing

- (void)connection:(NSURLConnection *)connection didWriteData:(long long)bytesWritten totalBytesWritten:(long long)totalBytesWritten expectedTotalBytes:(long long)expectedTotalBytes {

}

- (void)connectionDidFinishDownloading:(NSURLConnection *)connection destinationURL:(NSURL *)destinationURL {

}

- (void) connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {

}

- (void) connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {

}

- (void) connectionDidFinishLoading:(NSURLConnection *)connection {

}

Here is my .h file

@interface FirstTopViewController : UIViewController <NSURLConnectionDownloadDelegate, NSURLConnectionDataDelegate, NSURLConnectionDelegate, UITableViewDataSource, UITableViewDelegate>

This is how I am connecting to server to get JSON

[[NSURLConnection alloc] initWithRequest:req delegate:self startImmediately:YES];

This is the code for downloading an issue if needed

NSURLRequest *urlReq = [NSURLRequest requestWithURL:myURL];
NKAssetDownload *asset = [currentIssue addAssetWithRequest:urlReq];
[asset downloadWithDelegate:self];

The problem is with the call to get JSON from server. Issue downloading works fine.

Dhawal
  • 185
  • 2
  • 11

2 Answers2

2

NSURLConnectionDataDelegate define delegate methods used for loading data to memory. NSURLConnectionDownloadDelegate: delegate methods used to perform resource downloads directly to a disk file.

Then if you implemented connectionDidFinishDownloading:destinationURL: in your delegate. That will inform NSURLConnection you want to download the data to a disk file other than to memory as NSData. The NSURLConnectionDataDelegate method won't get called. If you eliminate connectionDidFinishDownloading:destinationURL: from your delegate class implementation, connection:DidReceiveData: will get called instead.

For your case, implement two helper delegates for different usage.

hrchen
  • 1,223
  • 13
  • 17
1

When you want to get your JSON data in -connection:didReceiveData:, you need to set the delegate to an object which implements NSURLConnectionDataDelegate; when you want to download an issue to a file, the delegate needs to be an object that implements NSURLConnectionDownloadDelegate. A single class can't do both at once.

This is not explained very well in the NSURLConnection docs, but the comments in NSURLConnection.h make it a little more explicit:

An NSURLConnection may be used for loading of resource data directly to memory, in which case an NSURLConnectionDataDelegate should be supplied, or for downloading of resource data directly to a file, in which case an NSURLConnectionDownloadDelegate is used. The delegate is retained by the NSURLConnection until a terminal condition is encountered. These two delegates are logically subclasses of the base protocol, NSURLConnectionDelegate.

jatoben
  • 3,079
  • 15
  • 12