非同期で実行できるサーバーリクエストがたくさんありますが、続行する前に、それらがすべて完了するのを待つ必要があります。
dispatch_group_asyncとそれはかなり合理的なようですが、私はそれを動作させることができません。永久にブロックするか、まったくブロックしません。私の最近の試みは次のようになります。
dispatch_group_t group;
- (void)cleanRoom {
NSAssert(![NSThread isMainThread], @"not on main thread.");
group = dispatch_group_create();
for (Junk *thing in myRoom) {
// take it off the current thread
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
// register code against group
dispatch_group_enter(attachmentGroup);
NSURLConnectionWrapper *wrapper = [[NSURLConnectionWrapper alloc] init];
wrapper.delegate = self;
[wrapper sendRequestFor:thing];
}];
}
// wait till the stuff is the group is done
dispatch_group_wait(attachmentGroup, DISPATCH_TIME_FOREVER);
NSLog(@"waiting complete!!!!");
// process the results now that I have them all
}
- (void)wrapperConnectionDone {
// do a bit more junk
dispatch_group_leave(group);
}
これによりNSURLConnectionDelegate
、NSURLConnectionDataDelegate
メソッドが呼び出されることがないため、永久にブロックされます。どういうわけか彼らのスレッドをブロックしたと思いますが、使用すると、が私のメソッドとは異なるスレッドにあるNSLog
ことを確認できます。NSURLConnection
cleanRoom
コールバックを作成するための実行ループがない他のスレッドについて少し読んだので、次のようなことを試しましたが、目立った効果はconnection setDelegateQueue:[NSOperationQueue mainQueue]]
あり[[NSRunLoop currentRunLoop] runUntilDate:[NSDate distantFuture]]
ませんでした。
+ sendAsynchronousRequest:queue:completionHandler:
私にはうまくいきません、私はいくつかの醜い認証を持っています。私はそれでいくつかの良い例を見てきましたが、私は適応することに失敗しました。
私は明らかにいくつかの基本的なビットが欠けていますが、それを見つけることができません。