8

に4つの要素がありますNSMutableArrayUITextView私は、ファイルをダウンロードし、テスト目的でファイルのデータを表示するためのこのきちんとしたコードを持っています。forループがなければ、すべてが正常です。私に問題を与えるコードはこの関数にあります:

- (void)complexDownload {
    int i;
    for (i=0; i < downloadArray.count; i++) {
        if (isBusy == NO) {
            [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES];
            downloadURL = [downloadArray objectAtIndex:i];
            NSLog(@"URL is %@", downloadURL);
            NSLog(@"Downloading object at index %i", i);
            NSURL *url = downloadURL;
            NSURLRequest *theRequest=[NSURLRequest requestWithURL:url
                                                      cachePolicy:NSURLRequestUseProtocolCachePolicy
                                                  timeoutInterval:60.0];

            NSURLConnection *theConnection = [NSURLConnection connectionWithRequest:theRequest delegate:self];

                if (theConnection) {
                    self.downloadData = [NSMutableData data];
                    isBusy = YES;
                    NSLog(@"Busy value in download cycle equals %i, downloading", isBusy);
                } else {
                    [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];
                    NSLog(@"Connection failed");
                    isBusy = NO;
                }
        }
    }
}

私は最初、問題はにあるのではないかと思いましたがisBusy BOOL、条件がなくてもifアプリがクラッシュします。コンパイラはエラーを出しませんが、これは次のとおり です。大きなスクリーンショットのリンクは次のとおりです。

残りの機能は次のとおりです。

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
    [downloadData appendData:data];
}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
    NSString *dataString = [[NSString alloc] initWithData:downloadData encoding:NSASCIIStringEncoding];
    self.dataTextView.text = dataString;
    [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];
    NSLog(@"Download finished!");
    isBusy = NO;
}

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
    NSLog(@"%@", error);
    [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];
}

すべてのNSLogged値は問題なく、アレイにはリンクがあり、すべてのリンクは正しいです。

4

4 に答える 4

6

私の推測では、ある時点downloadArray[i]で破損しているか、NSUrlではないかのどちらかです。CFURLCopyAbsoluteURL()から呼び出されたときにコードがクラッシュし[NSURLRequest requestWithURL...]ます。

于 2012-12-19T15:05:56.113 に答える
1

非同期APIinitWithRequest:delegate:を取得し、 isBusyフラグを使用して同期させようとします。このアプローチはそもそも非常に間違っています。NSURLConnectionクラスは十分に賢いので、適切に使用すれば任意のフラグを使用する必要はありません。NSOperationsまたはGCDの使用を真剣に再検討する必要があります。より複雑な接続プログラミングを計画している場合は、RestKitなどのサードパーティフレームワークの使用を検討する必要があります。

于 2012-12-19T15:13:23.553 に答える
1

forループとisBusyインジケーターを取り除きます。complexDownload常に最初または最後の(より適切な方の)オブジェクトのみを処理してから、それを配列から削除します。再度connectionDidFinish呼び出します。complexDownloadそのために使用performSelectorします。待機時間は0.0fでもかまいません。そうすることで、あなたdownloadArrayはある種のキューとして機能します。

于 2012-12-19T15:23:36.083 に答える
1

[NSURLRequest requestWithURL:urlのurl-objectに問題があると思います。追加のオブジェクトにURLをコピーする必要はありません。これを試して:

NSLog(@"URL is %@", [downloadArray objectAtIndex:i]);
NSLog(@"Downloading object at index %i", i);
NSURL *url = [downloadArray objectAtIndex:i];

(またはdownloadURLの前に自分自身を追加します)

downloadArrayにNSStringが含まれている場合:

NSURL *url = [NSURL URLWithString:[downloadArray objectAtIndex:i]];

[ASIHTTPRequest](http://allseeing-i.com/ASIHTTPRequest/のような外部フレームワークを使用することをお勧めします

于 2012-12-19T15:24:55.907 に答える