0

I have followed a handful of examples on NSURLConnection, see latest below, but yet I keep getting null on the returned FinishLoading. I checked didReceiveResponse and its getting the data. What am I not doing right here?

EDITED: Now works as expected.

    #pragma mark NSURLConnection Delegate Methods

    - (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
        _dataDictionary = [NSMutableDictionary new];
        _theReceivedData = [NSMutableData new];

        [_theReceivedData setLength:0];
        // add object
        [_dataDictionary setObject:_theReceivedData forKey:[connection description]];
    }

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

        NSMutableData *imageData = _dataDictionary[ [connection description] ];
        [imageData appendData:data];
        if([connection description]!=nil && imageData!=nil)
        {
            [_dataDictionary setObject:imageData forKey:[connection description]];
        }
    }

    - (NSCachedURLResponse *)connection:(NSURLConnection *)connection
                      willCacheResponse:(NSCachedURLResponse*)cachedResponse {
        // Return nil to indicate not necessary to store a cached response for this connection
        return nil;
    }

    - (void)connectionDidFinishLoading:(NSURLConnection *)connection {
        // The request is complete and data has been received
        // You can parse the stuff in your instance variable now

        NSData *imageData = _dataDictionary[ [connection description] ];

if(imageData!=nil)
    {
        NSLog(@"%@",imageData);
LeviXC
  • 1,075
  • 2
  • 15
  • 32
  • 1
    Are you sure that the data object is initialized? Try logging it in the response method and see if it prints "null". – Chris Loonam Jun 05 '15 at 13:31
  • Thanks for that. I init'ed the NSMutableDictionary and that got it working, so I updated my post, however its not producing an image now. – LeviXC Jun 05 '15 at 21:14

1 Answers1

1

Take NSMutableData *theReceivedData out. Make it a class variable. Your problem is theReceivedData is being deallocated in every method. You are creating a new object in every method. Retain the old one.

NSMutableData *_theReceivedData;

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
            _theReceivedData = [NSMutableData new];
            [_theReceivedData setLength:0];
        }

        - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
            [_theReceivedData appendData:data];
        }

        - (NSCachedURLResponse *)connection:(NSURLConnection *)connection
                          willCacheResponse:(NSCachedURLResponse*)cachedResponse {
            // Return nil to indicate not necessary to store a cached response for this connection
            return nil;
        }

        // LOOK AT THIS http://stackoverflow.com/questions/1064920/iphone-corrupt-jpeg-data-for-image-received-over-http

        - (void)connectionDidFinishLoading:(NSURLConnection *)connection {
            NSLog(@"%@",_theReceivedData);
lead_the_zeppelin
  • 2,017
  • 13
  • 23
  • I copies your example and I still got a null. That was how I started out till I followed someones else example that I had posted above. Do I need to alloc init the dataDictionary in the method class that is calling the NSURLConnection? – LeviXC Jun 05 '15 at 15:53
  • 1
    @LeviXC check out my updated response. There is no need to reallocate _theReceivedData. – lead_the_zeppelin Jun 05 '15 at 16:22
  • 1
    you also need to init it.. otherwise it will stays nil forever :) – Marc-Alexandre Bérubé Jun 05 '15 at 16:45
  • Thanks, I updated my post to reflect what I'm trying to do. Because I have multiple images request happening at once, I have to use a NSMutableDictionary and key each request to the [connection description]. Looks like the data is now getting through as I needed to init the NSMutableDictionary, however the images are still coming out blank... – LeviXC Jun 05 '15 at 21:12
  • Thanks for the help, your coding example helped me sort out my issue. I have updated my post to reflect the working version – LeviXC Jun 05 '15 at 21:25