NSURLConnection failing

  • I am trying to use NSURLConnection to fetch a few small images (like
    10k) from various websites.  The connections are established and then
    the connection:willSendRequest:redirectResponse: delegate method is
    called and it freezes.  It never times out that I know of, at least
    the connection:didFailWithError: delegate is never called.  So now for
    the crazy part... sometimes the code works fine and all of the images
    are retrieved.  I have tested this with several different sites and it
    seems to be fairly random.  I have tried different caching policies as
    well with no change.  If it fails, it always seems to fail after the
    redirect is received.  I am running this on Leopard with GC enabled.
    Below is the code I am using, I removed all the log messages I was
    using for debugging.  If anyone has any ideas they would be greatly
    appreciated.

    NSURLRequest *request=[NSURLRequest requestWithURL:[NSURL
    URLWithString:imageURL]

    cachePolicy:NSURLRequestReloadIgnoringLocalCacheData

    timeoutInterval:30.0];

    NSURLConnection *imageConnection = [[NSURLConnection alloc]
    initWithRequest:request

                                        delegate:self];

    if (!imageConnection) {
        NSLog(@"A problem was encountered while trying to download an
    image from %@", imageURL);
    }

    ----------------delegate methods-----------------

    - (void)connection:(NSURLConnection *)connection
    didReceiveResponse:(NSURLResponse *)response
    {
        [receivedData setLength:0];
    }

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

    - (NSURLRequest *)connection:(NSURLConnection *)connection
                        willSendRequest:(NSURLRequest *)request
                        redirectResponse:(NSURLResponse *)redirectResponse
    {
        return request;
    }

    - (void)connection:(NSURLConnection *)connection
    didFailWithError:(NSError *)error
    {
        NSLog(@"Error: %@", [error localizedDescription]);
    }

    - (void)connectionDidFinishLoading:(NSURLConnection *)connection
    {
        image = [[NSImage alloc] initWithData:receivedData];
        if (!image || ![image isValid]) {
            image = [NSImage imageNamed:@"missingImage.tiff"];
        }
    }

    -Doug
  • Just an update on this... I changed the code to use NSURLDownload with
    moderate success.  The images are downloaded much more consistently,
    but every 4th or 5th time it hangs after the
    connection:willSendRequest:redirectResponse: delegate method is
    called.  When it crashes it dumps me into gcb and the next to the last
    two items in the thread are objc_msgSend and
    _NSURLDownloadDidRecevieData.  I am just baffled at why this is
    failing so intermittently.  Thanks.

    Doug

    On Thu, Feb 21, 2008 at 9:23 PM, Doug Penny <doug.penny...> wrote:
    > I am trying to use NSURLConnection to fetch a few small images (like
    > 10k) from various websites.  The connections are established and then
    > the connection:willSendRequest:redirectResponse: delegate method is
    > called and it freezes.  It never times out that I know of, at least
    > the connection:didFailWithError: delegate is never called.  So now for
    > the crazy part... sometimes the code works fine and all of the images
    > are retrieved.  I have tested this with several different sites and it
    > seems to be fairly random.  I have tried different caching policies as
    > well with no change.  If it fails, it always seems to fail after the
    > redirect is received.  I am running this on Leopard with GC enabled.
    > Below is the code I am using, I removed all the log messages I was
    > using for debugging.  If anyone has any ideas they would be greatly
    > appreciated.
    >
    >
    > NSURLRequest *request=[NSURLRequest requestWithURL:[NSURL
    > URLWithString:imageURL]
    >
    > cachePolicy:NSURLRequestReloadIgnoringLocalCacheData
    >
    > timeoutInterval:30.0];
    >
    > NSURLConnection *imageConnection = [[NSURLConnection alloc]
    > initWithRequest:request
    >
    > delegate:self];
    >
    > if (!imageConnection) {
    > NSLog(@"A problem was encountered while trying to download an
    > image from %@", imageURL);
    > }
    >
    >
    > ----------------delegate methods-----------------
    >
    > - (void)connection:(NSURLConnection *)connection
    > didReceiveResponse:(NSURLResponse *)response
    > {
    > [receivedData setLength:0];
    > }
    >
    > - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
    > {
    > [receivedData appendData:data];
    > }
    >
    > - (NSURLRequest *)connection:(NSURLConnection *)connection
    > willSendRequest:(NSURLRequest *)request
    > redirectResponse:(NSURLResponse *)redirectResponse
    > {
    > return request;
    > }
    >
    > - (void)connection:(NSURLConnection *)connection
    > didFailWithError:(NSError *)error
    > {
    > NSLog(@"Error: %@", [error localizedDescription]);
    > }
    >
    > - (void)connectionDidFinishLoading:(NSURLConnection *)connection
    > {
    > image = [[NSImage alloc] initWithData:receivedData];
    > if (!image || ![image isValid]) {
    > image = [NSImage imageNamed:@"missingImage.tiff"];
    > }
    > }
    >
    >
    >
    > -Doug
    >
previous month february 2008 next month
MTWTFSS
        1 2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28 29    
Go to today