NSURLConnection won't call delegate methods

Discussion in 'Mac Programming' started by Samppaa, Jul 5, 2010.

  1. Samppaa macrumors regular

    Joined:
    Mar 26, 2010
    #1
    I got a little problem when I tried to do NSURLConnection example from apple site and add it to my application, it won't somehow call the delegates as it doesn't print anything from them to console.. I am really confused what I am doing wrong, Thanks in advance!

    Code:
    //
    //  redeemHandler.m
    //  Checker
    //
    //  Created by Samuli Lehtonen on 5.7.2010.
    //  Copyright 2010 Test. All rights reserved.
    //
    
    #import "redeemHandler.h"
    
    
    @implementation redeemHandler
    @synthesize intervalValue;
    
    
    -(void)testFunction
    {
    NSLog(@"Running threadFunction!");
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
    while (checking) {
    NSURLRequest *theRequest=[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.apple.com/"]
      cachePolicy:NSURLRequestUseProtocolCachePolicy
      timeoutInterval:60.0];
    NSURLConnection *theConnection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
    
    
    if (theConnection) {
    receivedData = [[NSMutableData data] retain];
    NSLog(@"Succeeded! Data lenght: %d", [receivedData length]);
    }
    else {
    NSLog(@"Connection failed!");
    }
    sleep(1);
    }
    
    
    [pool release];
    
    
    }
    
    
    -(IBAction)startChecking:(id)sender
    {
    if ([sender selectedSegment] == 0) {
    [sender setEnabled:YES forSegment:1];
    [statusText setStringValue:@"Checking in progress..."];
    [sender setEnabled:NO forSegment:0];
    [statusBar startAnimation:sender];
    checking = YES;
    urlThread = [[NSThread alloc] initWithTarget:self selector:@selector(testFunction) object:nil];
    [urlThread start];
    NSLog(@"Started!");
    }
    else {
    [sender setEnabled:YES forSegment:0];
    [sender setEnabled:NO  forSegment:1];
    [statusText setStringValue:@"Checking not in progress..."];
    [statusBar stopAnimation:sender];
    checking = NO;
    [urlThread release];
    urlThread = nil;
    }
    
    
    }
    
    
    
    -(IBAction)openOptions:(id)sender
    {
    [NSApp beginSheet:optionsSheet modalForWindow:mainWindow modalDelegate:self didEndSelector:NULL contextInfo:nil];
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    intervalValue = [defaults integerForKey:@"Interval"];
    
    
    // NSLog(@" is: %@", [defaults valueForKey:@"Interval"]);
    
    
    
    }
    
    -(IBAction)closeOptions:(id)sender
    {
    [optionsSheet orderOut:nil];
    [NSApp endSheet:optionsSheet];
    
    }
    
    -(IBAction)closeAndSaveOptions:(id)sender
    {
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    [defaults setInteger:intervalValue forKey:@"Interval"];
    [defaults synchronize];
    /*NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    NSLog(@" is: %@", [defaults valueForKey:@"Interval"]);*/
    [optionsSheet orderOut:nil];
    [NSApp endSheet:optionsSheet];
    
    
    }
    
    -(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
    {
    [receivedData setLength:0];
    NSLog(@"Received response!");
    }
    
    -(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
    {
    NSLog(@"Received data!");
    [receivedData appendData:data];
    }
    
    -(void)connectionDidFinishLoading:(NSURLConnection *)connection
    {
    NSLog(@"Succeeded! Received %d bytes of data", [receivedData length]);
    [connection release];
    [receivedData release];
    }
    
    -(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
    {
    [connection release];
    [receivedData release];
    
    
    NSLog(@"Connection failed!");
    }
    
    @end
    
    There is the code, I'll post if I get this solved..
     
  2. Luke Redpath macrumors 6502a

    Joined:
    Nov 9, 2007
    Location:
    Colchester, UK
    #2
    Why are you messing around with threads?

    You are using an asynchronous API that depends on the runloop. You should not be doing any threading and this is probably why you are getting undefined behaviour.

    Run the NSUrlConnection code on the main thread, and don't forget to remove that sleep.
     

Share This Page