2009-03-16 8 views
3

HTTP接続のための独立したクラスを作成しました。すべての接続が正常に動作します。問題は、メソッド 'didReceiveData'が接続を呼び出すメソッドの後に呼び出されることがわかります。 「HTTP接続のための 'didReceiveData'メソッドについての議論


- (IBAction)accept:(id)sender { 
    [self connect:url]; 
    //labelStr = ReturnStr; Cannot be written here. 
} 

-(void)connect:(NSString *)strURL 
{ 
    NSURLRequest *theRequest=[NSURLRequest requestWithURL:[NSURL URLWithString:strURL] 
               cachePolicy:NSURLRequestUseProtocolCachePolicy 
              timeoutInterval:60.0]; 

    NSURLConnection *theConnection=[[NSURLConnection alloc] initWithRequest:theRequest delegate:self]; 
    if (theConnection) 
    { 
     // 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 
{ 
    // append the new data to the receivedData 
    [receivedData appendData:data]; 
    ReturnStr = [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding]; 
} 

は、これは私が受け取った文字列にラベルのテキストを変更したい場合は、コードはIBActionで書くことができないという問題が発生します(メソッド「didReceiveDataは」IBAction後に「受け入れる」と呼ばれます)受け入れる」しかし、この方法で記述される必要があります 『didReceiveData:『をdidReceiveData』は、このよう


    MainViewController *mainView = [[MainViewController alloc] initWithNibName:@"MainView" bundle:nil]; 
    AMEAppDelegate *delegate = [[UIApplication sharedApplication] delegate]; 
    [delegate.navController pushViewController:mainView animated:YES]; 
    mainView.labelStr.text = ReturnStr; 

さらなる問題は、私はMAINVIEWを初期化する場合MAINVIEW上のデータが上書きされることです』。 MainViewを初期化せずにlabelStrのテキストを変更することは可能ですか?

答えて

1

データが入るまでアプリを待機させたい場合は、NSURLConnectionのsendSynchronousRequest:returningResponse:error:メソッドを使用します。ただし、このメソッドが実行されている間、残りのアプリケーションはフリーズされます。もちろん、ユーザーが奇妙な接続をしていると、メソッドに時間がかかることがあります。

+0

ロードが完了したら接続をシャットダウンする方法はありますか? didFinishLoadingメソッドのようなものはありますか? –

+0

sendSynchronousRequest:returningResponse:error:では、これはすべてそのメソッドによって管理されます。メソッドが戻るまでに、接続が開かれ、要求が送信され、応答が受信され、接続が再び閉じられました。しかし、すべてには数秒かかるかもしれません。 –

2

The problem is that I find method 'didReceiveData' will be called AFTER the method who call the connection. (method 'didReceiveData' will be called after IBAction 'accept')

あなたはそれを作成し、接続する前に、あなたにconnection:didReceiveData:を送信するための接続を期待していますか?

This will cause a problem that if I want to change the text of a label to the received string, the code cannot be written in IBAction 'accept' but have to be written in method 'didReceiveData' …

右の音。あなたがそれを受け取るまであなたが受け取ったもので働くことはできません。あなたのconnection:didReceiveData:方法では、メインビューコントローラとアプリデリゲートを作成

A further problem is that the data on MainView will be overwritten if I initialize MainView in 'didReceiveData'. Is it possible for me to change the text of labelStr without initialize MainView?

はそれを行うことが本当に遅れているようです。これらのことを早くしてからconnection:didReceiveData:に何もしないでください。labelStr.textと設定してください。

ところで、connection:didReceiveData:の実装では、リークがReturnStrと表示されます。あなたが奪い取ったものを解放したり、オートリリースすることを忘れないでください。

0

NSURL接続および他の同様のクラスは、非同期で使用するように設計されています。

initWithRequest:デリゲート:すぐに戻り、デリゲートメソッドをそのデリゲートに送信するまで接続スタジオに迷惑をかけません。

0

NSDataの代わりにNSMutableDataを使用します。

+0

こんにちはnew-soul、receivedDataは既にNSMutableDataとして宣言されています。 –

関連する問題