views:

60

answers:

1

Hi I am writing a code that reads data from a http connection and stores in a byte array. I have written my own NSOperation class. Reference of the code is

Concurrent Operations Demystified

My HttpWorker class declaration is like

@interface HttpWorker : NSOperation{

    NSString *param;
    double requestCode;
    BOOL isLive;
    long initSleep;
    BOOL _isFinished;
    BOOL _isExecuting;
    NSURL *_url;
    NSURLConnection *_connection;
    NSData *_data;


}

@property (nonatomic, retain) NSString *param;
@property (nonatomic) double requestCode;
@property (nonatomic) BOOL isLive;
@property (nonatomic) long initSleep;
@property (readonly) BOOL isFinished;
@property (readonly) BOOL isExecuting;
@property (readonly, copy) NSURL *url;
@property (nonatomic, retain) NSURLConnection *httpCon;
@property (readonly, retain) NSData *data;



-(id)initWithUrl:(NSURL *)_url;
-(void) setRequestParameters:(NSString *)parameters iRequestCode:(double)iRequestCode initialSleep:(long)initialSleep;

@end

And my HttpWorker.m class is like

#import "HttpWorker.h"
#import "Resources.h"


@implementation HttpWorker

@synthesize param;
@synthesize requestCode;
@synthesize isLive;
@synthesize initSleep;
@synthesize isFinished = _isFinished;
@synthesize isExecuting = _isExecuting;
@synthesize url = _url;
@synthesize data = _data;


-(id) initWithUrl: (NSURL *)Url{
    self = [super init];
    if(self == nil){
        return nil;
    }
    _url = [Url copy];
    _isExecuting = NO;
    _isFinished = NO;
    return self;
}

-(BOOL) isConcurrent{
    return YES;
}

-(void) start{
    if(![NSThread isMainThread]){
        [self performSelectorOnMainThread:@selector(start) withObject:nil waitUntilDone:NO];
        return;
    }
    [self willChangeValueForKey:@"isExecuting"];
    _isExecuting = YES;
    [self didChangeValueForKey:@"isExecuting"];
    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:_url];
    NSLog(@"Connecting... %@",_url);
    _connection = [[NSURLConnection alloc] initWithRequest:request delegate:self startImmediately: YES];
    if(_connection == nil){
        NSLog(@"connection is nil");
    }
}


-(void) setRequestParameters:(NSString *)parameters iRequestCode:(double)iRequestCode initialSleep:(long)initialSleep {
    self.param = parameters;
    self.requestCode = iRequestCode;
    self.initSleep = initialSleep;
}

/////////////////////////// delegate methods ///////////////////////////////

-(void) connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
    NSLog(@"receieved response...");
    _data = [[NSData alloc] init];
}


-(void) connection:(NSURLConnection *)connection didReceiveData:(NSData *)incomingData {
    NSLog(@"receieved data...");
}


-(void) connectionDidFinishLoading:(NSURLConnection *) connection {
    NSLog(@"connection did finish loading...");
}


@end

Problem is that when i run this code and add httpworker object to the NSOperationQueue, the code runs successfully and _connection is not nil but none of the delegate methods is executed. Can anyone please help?

Thanks and Best Regards...

A: 

Your delegate for the connection is "self" (= your NSOperation object). I assume this object is already gone when the connection wants to send messages to the delegate.

Your NSOperation does not have a "main" implementation. Consequently nothing will happen after the thread is started. It will (asynchronously!) fire the NSOperation and quit.

See: http://developer.apple.com/mac/library/documentation/Cocoa/Reference/NSOperation_class/Reference/Reference.html#//apple_ref/occ/instm/NSOperation/main


Bottom line: I highly recommend ASIHTTPRequest for this kind of task.

http://allseeing-i.com/ASIHTTPRequest/

Lars Schneider