0

正しく動作させるためにサウンド ファイルをダウンロードする必要があるアプリがあります。

サイズが 20Mb を超えるファイルをNSURLConnection非同期でダウンロードしています。ダウンロードの割合を追跡するために を配置し、Apple が提案するprogressBarViewデリゲート メソッドを使用しています。NSUrlConnection

NSURLRequest *theRequest=[NSURLRequest requestWithURL:soundFileURL cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:20.0];
// create the connection with the request
// and start loading the data
NSURLConnection *theConnection;

theConnection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self];

//[theConnection cancel];
//[theConnection start];
if (theConnection) {
    // Create the NSMutableData that will hold
    // the received data
    // receivedData is declared as a method instance elsewhere
    receivedData=[[NSMutableData data] retain];
} else {
    // inform the user that the download could not be made
} 

およびデリゲート メソッド

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

など ...

ダウンロードを開始すると、インターフェースが時々ハングアップし、progressViewハングアップします。

1 つの注目すべき点と、おそらく別の質問: ユーザー インターフェイスを無効にしたため、ユーザーはダウンロードが完了するまで待たなければなりません。もちろん、そのことを伝えるメッセージを彼に渡します。Apple はそのために私のアプリを拒否しますか? 私はそれについてひどく心配しています

私の質問を読んでくれてありがとう:)

4

2 に答える 2

1

NSUrlConnectionのイベントをNSURLConnectionDelegateデフォルトでメイン スレッドに送信します。この接続用に新しいプールと実行ループを作成し、それがバックグラウンドで処理されることを確認してください。バックグラウンドで画像をダウンロードする例を次に示します。変更された NSOperationQueue と NSOperation を使用していますが、ファイルをダウンロードするために簡単に変更できます。developer.apple.com の LinkedImageFetcher

于 2012-09-12T11:36:36.220 に答える