http要求を行うためにNSUrlConnectionを使用しています。私は、イベントドリブンコードを避けたいので、私は、次のようにNSRunloopを利用しています:イベントドリブンコードを回避するためのNSRunloopの使用
NSURLRequest *request = [[NSURLRequest alloc]
initWithURL: [NSURL URLWithString:_urlString]
cachePolicy: NSURLRequestReloadIgnoringLocalAndRemoteCacheData
timeoutInterval: 10
];
NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
if(!connection)
{
DebugLog(@"Creating a connection has failed");
[self setValidationRequestResult:false];
}
else
{
NSRunLoop* currentRunLoop = [NSRunLoop currentRunLoop];
[self.connection start];
while (self.isConnectionComplete == NO)
{
NSDate* dateLimit = [NSDate dateWithTimeIntervalSinceNow:0.05];
[currentRunLoop runUntilDate:dateLimit];
}
//TODO: check release of NSUrlConnection
}
}
// Now perform remaining tasks ........
それは上に示した方法で、NSRunLoopに大丈夫ですか、私たちは「didFailWithError」と「connectionDidFinishLoading」にして通知を掲示すべきですhttpリクエストが完了した後にロジックを書いてください。
イベントドリブンコードを回避する理由は何ですか? NSURLConnectionは、データが利用可能なときに、自身をrunloopにアタッチし、コールバックをポストするように設計されています。 –
httpリクエストから応答を取得した後、特定のタスクを実行したい。イベントドリブンコードを記述すると、NSUrlConnectionタスクが完了した後でPostNotificationを使用する必要があります。これにより、わかりにくいコードを壊すようになります。 –
より良い質問、なぜあなただけ使用していない '+ [NSURLConnection sendSynchronousRequest:returningResponse:エラー:]'? – MyztikJenz