2014-04-04 17 views
0

您好新多線程....下面是我寫的代碼在單獨的線程中下載視頻的代碼,但委託方法沒有引發任何人請幫助我解決這個.. 。在此先感謝..委託方法不在iOS中的多線程程序中觸發

- (void)downLoad { 
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ 
_videoData = [[NSMutableData alloc] init]; 
    [NSURLConnection connectionWithRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://km.support.apple.com/library/APPLE/APPLECARE_ALLGEOS/HT1211/sample_iTunes.mov"]] delegate:self]; 
     //saving is done on main thread 
}); 
} 
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response { 
    NSLog(@"Downloading Started"); 
    _length = [response expectedContentLength]; 
    NSLog(@"Size:%0.2f",_length); 
} 
-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data { 
    [_videoData appendData:data]; 
    float progress = (float)[_videoData length]/(float)_length; 
    NSLog(@"Progress:%0.2f",progress); 
    _timeLabel.text = [NSString stringWithFormat:@"%0.2F%%",progress*100]; 
    [_progressView setProgress:progress animated:YES]; 
} 
-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error { 
    NSLog(@"didFailWithError"); 
} 
-(void)connectionDidFinishLoading:(NSURLConnection *)connection { 
    dispatch_async(dispatch_get_main_queue(), ^{ 
     [self saveLocally:_videoData]; 
     NSLog(@"File Saved !"); 
    }); 
} 

回答

0

NSURLConnection的排隊,在一個運行循環(NSRunLoop),如果你有一個(像主線程),一個線程創建一個NSURLConnection的這是很好的委託電話。

但是,您正在使用創建線程並根據喜好銷燬/停用它們的大中央調度(GCD)。我真的不認爲需要將下載邏輯移動到單獨的線程,因爲只有主線程會調用回調(如果要執行UI工作,這非常有用),但是NSURlConnection將在其上執行其下載魔術後臺線程。

如果您想在下載時在後臺下載數據處理,您可能需要查看NSURLSession(iOS 7),因爲它支持GCD隊列。

相關問題