Helper Objects: Why won't it download a second time?


#1

I just got through the Helper Objects subchapter (is it called that?) and am now experiencing the interesting issue that the .txt file won’t download again when I re-run the code I just wrote.

The output I get now is:

2012-08-08 19:29:31.123 Callbacks[3681:303] received 665331 bytes 2012-08-08 19:29:31.124 Callbacks[3681:303] Got it all! 2012-08-08 19:29:31.124 Callbacks[3681:303] string has 665331 characters

The first time it of course worked like a charm.
It seems to me as if the downloaded file is still cached inside the incomingData object, although I don’t quite understand how that can be possible. Especially since the connectionDidFinishLoading method of Logger sets it to nil after “Got it all!” has been written to the log. And how could an object live on when the problem is completely stopped?

I’ve double checked my code, and it’s now exactly as it is in the book and seems completely right to me. Nevertheless I have the contents of main.m, Logger.h and Logger.m posted below.

main.m

[code]//
// main.m
// Callbacks
//
// Created by Kilian Költzsch on 08.08.12.
// Copyright © 2012 Kilian Koeltzsch. All rights reserved.
//

#import <Foundation/Foundation.h>
#import “Logger.h”

int main(int argc, const char * argv[])
{

@autoreleasepool {
    
    Logger *logger = [[Logger alloc] init];
    
    NSURL *url = [NSURL URLWithString:@"http://www.gutenberg.org/cache/epub/205/pg205.txt"];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    
    
    __unused NSURLConnection *fetchConn = [[NSURLConnection alloc] initWithRequest:request
                                                                          delegate:logger
                                                                  startImmediately:YES];
    
    __unused NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:2.0
                                                      target:logger
                                                    selector:@selector(sayOuch:)
                                                    userInfo:nil
                                                     repeats:YES];
    [[NSRunLoop currentRunLoop] run];
    
}
return 0;

}
[/code]

Logger.h

[code]//
// Logger.h
// Callbacks
//
// Created by Kilian Költzsch on 08.08.12.
// Copyright © 2012 Kilian Koeltzsch. All rights reserved.
//

#import <Foundation/Foundation.h>

@interface Logger : NSObject {
NSMutableData *incomingData;
}

  • (void)sayOuch:(NSTimer *)t;

@end
[/code]

Logger.m

[code]//
// Logger.m
// Callbacks
//
// Created by Kilian Költzsch on 08.08.12.
// Copyright © 2012 Kilian Koeltzsch. All rights reserved.
//

#import “Logger.h”

@implementation Logger

  • (void)sayOuch:(NSTimer *)t
    {
    NSLog(@“Ouch!”);
    }

//Called each time a chunk of data arrives

  • (void)connection:(NSURLConnection *)connection
    didReceiveData:(NSData *)data
    {
    NSLog(@“received %lu bytes”, [data length]);

    //Create a mutable data if it doesn’t already exist
    if (!incomingData) {
    incomingData = [[NSMutableData alloc] init];
    }

    [incomingData appendData:data];
    }

//Called when the last chunk has been processed

  • (void)connectionDidFinishLoading:(NSURLConnection *)connection
    {
    NSLog(@“Got it all!”);

    NSString *string = [[NSString alloc] initWithData:incomingData
    encoding:NSUTF8StringEncoding];
    incomingData = nil;
    NSLog(@“string has %lu characters”, [string length]);

    //Uncomment the next line to see the entire fetched file
    // NSLog(@“The whole string is %@”, string);
    }

//Called if the fetch fails

  • (void)connection:(NSURLConnection *)connection
    didFailWithError:(NSError *)error
    {
    NSLog(@“connection failed: %@”, [error localizedDescription]);
    incomingData = nil;
    }

@end
[/code]

I’d love to be able to completely reset this so I can try some stuff… Thanks in advance for any help and insights!