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がnilではないが、デリゲートメソッドが実行されないことです。誰か助けてもらえますか?

よろしくお願いします...

4

1 に答える 1

0

接続のデリゲートは「self」(= 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 に答える