0

NSURLConnection cancelコールバックを使用してキャッチすることは可能ですか?

このコードを使用している場合

-(void) pleaseStopDownload {
cancelled = YES;
    [conn cancel];
    conn = nil;
    [self myUpdateUImessage];
}

from time tomyUpdateUImessageは、このコールバックの前に呼び出されます

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
    NSLog(@"didReceiveData");
if (!cancelled) {
//this code inside brackets suddenly is calling:(
//not always but from time to time
    summ += data.length;
    if (_progressHandler != nil)
        _progressHandler(data, summ, max);
} else {
return;
}
}

そのため、ユーザー インターフェイスが適切に更新されません。つまり、進行中の UI よりも最終的な UI が表示されます。

問題があった編集

NSOperationQueue *tempQueue = [[NSOperationQueue alloc] init];
[conn setDelegateQueue:tempQueue];

正しいNSQueueです NSOperationQueue *tempQueue = [NSOperationQueue mainQueue];

4

1 に答える 1

2

コールバックを使用して NSURLConnection キャンセルをキャッチすることは可能ですか?

いいえ。

ここの公式ドキュメントから:

このメソッドが呼び出された後、接続はそれ以上デリゲート メソッドの呼び出しを行いません。

これは、呼び出されたらすぐに UI のクリーンアップを処理し、変数cancelに依存しないようにする必要があることを意味します。_cancelled- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data

私がアドバイスするのは、キャンセル コードからクリーンアップ メソッドを呼び出すことです。

-(void) pleaseStopDownload {
    [conn cancel];
    conn = nil;
    [self handleCancelledDownload];
}
于 2016-04-25T15:16:54.427 に答える