5

我正在编写一个从 http 连接读取数据并存储在字节数组中的代码。我已经编写了自己的 NSOperation 类。代码的参考是

并发操作揭秘

我的 HttpWorker 类声明就像

@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

我的 HttpWorker.m 类就像

#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

问题是,当我运行此代码并将 httpworker 对象添加到 NSOperationQueue 时,代码运行成功并且 _connection 不为零,但没有执行任何委托方法。有人可以帮忙吗?

谢谢和最好的问候...

4

1 回答 1

0

您的连接代表是“自我”(=您的 NSOperation 对象)。我假设当连接想要向委托发送消息时这个对象已经消失了。

您的 NSOperation 没有“主要”实现。因此,线程启动后什么都不会发生。它将(异步!)触发 NSOperation 并退出。

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


底线:我强烈推荐 ASIHTTPRequest 用于此类任务。

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

于 2010-08-23T12:24:52.370 回答