2013-04-17 18 views
6

Belirli bir işlevde eşzamansız URL isteklerini uygulamaya çalışıyorum, tüm bu isteklerin tamamlanmasını istiyorum ve sonra belirli bir eylemi istiyorum ancak eylem isteklerin önündedir, yani isteklerin tamamlanmasından önce çağrılıyor . Şimdi updateUIFunction myAsyncMultipleURLRequestFunction önce çağrılandispatch_async içinde eşzamansız url istekleri

dispatch_queue_t fetchQ = dispatch_queue_create("Featured Doc Downloader", NULL); 
     dispatch_async(fetchQ, ^{ 
      [self myAsyncMultipleURLRequestFunction]; 
      dispatch_sync(dispatch_get_main_queue(), ^{ 
       [self updateUIFunction]; 
      }); 
     }); 

-(void)myAsyncMultipleURLRequestFunction 
    { 
    for (int i=0; i<count; i++) 
    { 
    NSURLConnection *loginConnection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self];   
    } 
    } 

tüm istekleri tamamlar. Ayrıca bunu NSOperaitonQueue ile denedim ama gerçekten istediğimi yapamadım.

[_operationQ addOperationWithBlock:^{ 
    for (int i=0; i<count; i++) 
     { 
    NSURLConnection *loginConnection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self];   
     } 
    } 

[[NSOperationQueue mainQueue] addOperationWithBlock:^{ 
     // updating UI 
     [self updateUIFunction]; 
    }]; 
}]; 

Bunun çok basit olduğunu biliyorum ama zaman aşımı yaşıyorum, herhangi bir yardım takdir ediliyor.

cevap

10

@tkanzakic doğru yolda olduğunu deneyin. Kullanılacak doğru yapı dispatch_group_t. Ancak uygulama geliştirilebilir. Bir semafor kullanarak tüm indirmelerinizi senkronize olmayan bir şekilde başlatabilir ve eş zamanlı olarak çok fazla koşmadığınızdan emin olabilirsiniz. Burada, dispatch_group_t dosyasını nasıl kullanmanız gerektiğini gösteren tüm bir kod örneği var:

dispatch_queue_t fetchQ = dispatch_queue_create("Featured Doc Downloader", NULL); 
dispatch_group_t fetchGroup = dispatch_group_create(); 

// This will allow up to 8 parallel downloads. 
dispatch_semaphore_t downloadSema = dispatch_semaphore_create(8); 

// We start ALL our downloads in parallel throttled by the above semaphore. 
for (int i=0; i<count; i++) { 
    dispatch_group_async(fetchGroup, fetchQ, ^(void) { 
     dispatch_semaphore_wait(downloadSema, DISPATCH_TIME_FOREVER); 
     NSURLConnection *loginConnection = [[NSURLConnection alloc] initWithRequest:requestArray[i] delegate:self]; 
     dispatch_semaphore_signal(downloadSema); 
    }); 
} 

// Now we wait until ALL our dispatch_group_async are finished. 
dispatch_group_wait(fetchGroup, DISPATCH_TIME_FOREVER); 

// Update your UI 
dispatch_sync(dispatch_get_main_queue(), ^{ 
    [self updateUIFunction]; 
}); 

// Release resources 
dispatch_release(fetchGroup); 
dispatch_release(downloadSema); 
dispatch_release(fetchQ); 
+0

Bunu deneyeceğim, umut verici görünüyor. – satheeshwaran

+0

Bu sizin için çalışıyor mu? – aLevelOfIndirection

3

Bir dispatch_group_t oluşturmak ve sonra updateUIFunction yürütmek için dispatch_group_notify kullanabilirsiniz zaman örneğin grup finiş çalışan önceki bloğu,:

dispatch_queue_t fetchQ = dispatch_queue_create("Featured Doc Downloader", NULL); 
dispatch_async(fetchQ, ^{ 
    dispatch_group_t group = dispatch_group_create(); 
    dispatch_group_async(group, dispatch_get_global_queue(0, 0), ^{ 
     [self myAsyncMultipleURLRequestFunction]; 
    }); 
    dispatch_group_notify(group, dispatch_get_global_queue(0, 0), ^{ 
     dispatch_async(dispatch_get_main_queue(), ^{ 
      [self updateUIFunction]; 
     }); 
    }); 
}); 
+0

Bunu deneyecek ve size bildireceğiz! – satheeshwaran

+0

Hey @tkanzakic çalışmıyor, eskisi gibi. myUsyncMultipleURLRequestFunction işlevinin tamamlanmasından önce updateUIFUnction çağrılır. – satheeshwaran

+0

'dispatch_group_notify' bloğunun küçük bir değiştirmesini yaptım, bu yolu uygulaman gerektiğini hatırlıyorum, bu şekilde bir proje, – tkanzakic

1

İlk Yapılandırma çalıştırmak döngü.

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ 
    NSURLRequest* request = [NSURLRequest requestWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:10]; 
    [NSURLConnection connectionWithRequest:request delegate:self]; 
    while(!self.finished) { 
     [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]]; 
    } 
}); 

bu

+0

burada self.finished tarafından ne demek istiyorsun ?? Bu bir BOOL mu? – satheeshwaran

İlgili konular