6

別のスレッドでNSURLConnectionを実行していますが(非同期であり、メインスレッドで実行すると機能することを認識しています)、親スレッドをデリゲートとして渡してもデリゲート呼び出しは行われません。誰かがこれを行う方法を知っていますか?

コード:

-(void)startConnectionWithUrlPath:(NSString*)URLpath {

//initiates the download connection - setup
NSURL *myURL = [[NSURL alloc] initWithString:URLpath];

myURLRequest = [NSMutableURLRequest requestWithURL:myURL cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:60];
[myURL release];


//initiates the download connection on a seperate thread
[NSThread detachNewThreadSelector:@selector(startDownloading:) toTarget:self withObject:self];

}


-(void)startDownloading:(id)parentThread {

 NSAutoReleasePool *pool = [[NSAutoReleasePool alloc] init];

 [NSURLConnection connectionWithRequest:myURLRequest delegate:parentThread];
 //The delegate methods are setup in the rest of the class but they are never getting called...

 [pool drain];
}

編集*

別のスレッドでNSURLConnectionを実行する必要がある理由は、iPhoneアプリで何かをダウンロードしていて、ユーザーが画面をロックするとダウンロードがキャンセルされるためです(ユーザーがホームボタンを押すだけでアプリがバックグラウンドに入ると、問題なく続行されます) )。これは、別のスレッドではなく、メインスレッドで非同期に接続を実行しているためだと理解しています。

NSURLConnectionを開始するときに、このコードを(別のスレッドではなく)試しました。

  NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:myURLRequest delegate:self startImmediately:NO];
   [connection scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
   [connection start];
   [connection release];

しかし、画面ロックでダウンロードがキャンセルされるという点で、これと同じ問題があります。

*更新

以下のトーマスの回答に追加するには(スレッドの終了に関してジェームズウェブスターの回答も正しいことに注意してください)、Appleドキュメントは次のように説明しています:「一時停止状態-アプリはバックグラウンドにありますが、コードを実行していません。システムはアプリをに移動しますこの状態は自動的に発生し、通知する前に通知されません。一時停止中、アプリはメモリに残りますが、コードは実行されません。」

ユーザーが画面をロックすると、アプリはバックグラウンド状態になり、すぐに一時停止状態になるため、すべての実行が停止され、ダウンロードが強制終了され、これが発生しようとしているという警告は表示されません...ユーザーが画面をロックしたが、まだ画面が見つからないことを通知する通知。

したがって、アプリがバックグラウンドに移行したときにすべてのダウンロードを一時停止(特定の情報を保存してNSURLConnectionをキャンセル)し、アプリが再びアクティブになったときにHTTPRangeヘッダーで再開します。これは問題のない回避策ですが、ダウンロードがバックグラウンドで行われていないため、ユーザーエクスペリエンスに悪影響を与えるため、理想的ではありません...残念です。

4

2 に答える 2

1

Since your NSURLConnection is asynchronous, the end of your -startDownloading method is reached immediately, and the thread exits.

You should indeed schedule your connection on the main runloop (or use GCD).

デバイスのロックは別の問題です。デバイスがロックされると、バッテリーの寿命を延ばすためにアプリケーションが一時停止されます。ダウンロードを完了するために一時停止するときに、おそらく余分な時間を要求することができます。

于 2012-02-29T13:04:39.687 に答える
0

I think your problem might be that the NSURLConnection has been deallocated as soon as you exit the startDownloading: message (or more accurately when your autorelease pool is drained)

However I think your methodology might be a bit uncouth anyway. NSURLConnection the way you are using it is asynchronous and will appear to be threaded anyway.

Try this and see if it works as you expect it to (i.e. your app doesn't pause while your connection is busy)

-(void)startConnectionWithUrlPath:(NSString*)URLpath {

    //initiates the download connection - setup
    NSURL *myURL = [[NSURL alloc] initWithString:URLpath];

    myURLRequest = [NSMutableURLRequest requestWithURL:myURL cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:60];
    [myURL release];

    [NSURLConnection connectionWithRequest:myURLRequest delegate:self];
}
于 2012-02-29T11:51:16.283 に答える