Grand Central Dispatch mükemmeldir ve kod miktarını azaltır, ancak neden arka plan iş parçacığı üzerinde bir şey çalıştıramıyorum?karşılaştırma GCD vs. performSelectorInBackground: dispatch_async arka planda değil
- (IBAction)performSel {
[self performSelectorInBackground:@selector(doStuff) withObject:nil];
[NSThread sleepForTimeInterval:3];
[[self.view.subviews lastObject] removeFromSuperview];
}
- (IBAction)gcd {
dispatch_async(dispatch_queue_create("myGCDqueue", NULL), ^(void) {
//dispatch_sync(dispatch_queue_create("myGCDqueue", NULL), ^(void) {
//dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^(void) {
//dispatch_sync(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^(void) {
//dispatch_async(dispatch_get_main_queue(), ^(void) {
//dispatch_sync(dispatch_get_main_queue(), ^(void) {
[self doStuff]; // only for readability, will move the code on success
});
[NSThread sleepForTimeInterval:3];
[[self.view.subviews lastObject] removeFromSuperview];
}
- (void)doStuff {
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc]init];
UIView *abortingView = [[UIView alloc]initWithFrame: self.view.bounds];
abortingView.backgroundColor = [UIColor whiteColor];
abortingView.alpha = 0.7;
[self.view insertSubview:abortingView atIndex:10];
[abortingView release];
[pool drain];
}
[NSThread sleepForTimeInterval:3];
varsayılan bir UI işlevlerini taklit etmektir:
Ben (yorumladı işin hiçbiri) ne anlama geldiğini göstermek için örnek bir uygulama yaptık. Örneğin, birisi bir navigasyon görünümünden diğerine geçiyorsa.
Sadece yeni bir görünüm tabanlı uygulamaya kod kopyalayın, iki düğme oluşturun ve bunları bağlayın.
uyumaya veya IBAction yöntemlerine yanıt olarak uzun süren görevleri yapmak her zaman kötü bir fikirdir – Felix