2013-09-27 20 views
1

因此,我正在將一些數據從服務器下載到我的ios客戶端。數據需要格式化,所以我使用NSNotification通知應用程序何時數據已完全下載,何時完成,我格式化數據,然後將其顯示在屏幕上。 所有這一切都很酷,但由於數據的大小,屏幕凍結。我想我應該使用GCD將數據下載到另一個線程,以便UI仍然響應。當我這樣做時,我似乎沒有下載任何數據。 我有一個方法getTops,它使用NSURLConnection來下載數據。最初,我viewDidLoad我稱這種方法和它的工作很好,但後來我用GCD,像這樣無法使用GCD的NSURLConnection獲取數據

dispatch_queue_t getItemsQ = dispatch_queue_create("get Items", NULL); 
dispatch_async(getItemsQ, ^{ 
    [self getTops]; 
}); 

它停止工作。我知道它到達getTops,因爲我可以看到在日誌中的控制檯,但它從來沒有達到-(void)connectionDidFinishLoading:(NSURLConnection *)connection

這裏是我使用的代碼:

-(void)getTops{ 
    Keychain *keychain = [[Keychain alloc]init]; 
    NSString *auth_token = [keychain getAuthToken]; 
    NSLog(@"at getTops"); 
    topimageArray = [[NSMutableArray alloc]init]; 
    NSURLConnection *connection; 

    webData = [[NSMutableData alloc]init]; 
    NSURL *url = [[NSURL alloc]initWithString:base]; 

    NSMutableURLRequest *request = [[NSMutableURLRequest alloc]init]; 
    [request setURL: url]; 
    [request setValue:auth_token forHTTPHeaderField:@"X-AUTH-TOKEN"]; 
    connection = [NSURLConnection connectionWithRequest:request delegate:self]; 
    // [connection start]; 
    [UIApplication sharedApplication].networkActivityIndicatorVisible = YES; 
    NSLog(@"at getTops conn start"); 
    } 
-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response{ 
    NSLog(@"recieved response"); 
    [webData setLength:0]; 
} 
-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data{ 
    if(data) { 
     NSLog(@"Appending data"); 
     [webData appendData:data]; 
    } 
} 
-(void)connectionDidFinishLoading:(NSURLConnection *)connection{ 
    NSArray *response= [NSJSONSerialization JSONObjectWithData:webData options:0 error:nil]; 
    NSLog(@"Tops full response::: %@",response); 
    theArray = [[NSArray alloc]initWithArray:response]; 
    NSLog(@"DONE"); 
    //Notify that the data is ready to be formated 
    [[NSNotificationCenter defaultCenter]postNotificationName:@"getTops" object:nil]; 
    [UIApplication sharedApplication].networkActivityIndicatorVisible = NO; 
} 

-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error{ 
    NSLog(@"error::::%@", error); 
    NSString *errormsg = error.localizedDescription; 
    UIAlertView *alertview = [[UIAlertView alloc]initWithTitle:@"Error" message:errormsg delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil]; 
    [alertview show]; 
    [UIApplication sharedApplication].networkActivityIndicatorVisible = NO; 
} 

我想,也許我應該刪除[UIApplication sharedApplication].networkActivityIndicatorVisible但沒有幫助

編輯::添加到委託方法的NSLogs。 我得到的日誌是

at getTops conn start 

而那就是它。

+1

你不需要調用'start',這會自動發生。您能否將日誌添加到所有委託方法中,以查看您得到的回覆? –

+0

沒有收到任何迴應 – nupac

+0

如果你運行'sendSynchronousRequest:returningResponse:error:'? –

回答

3

最簡單的方法是使用sendAsynchronousRequest:queue:completionHandler :.沒有委託是必要的,只需將來自connectionDidFinishLoading的代碼放入完成塊即可。

加載URL請求的數據,並在請求完成或失敗時在操作隊列上執行處理程序塊。

[NSURLConnection sendAsynchronousRequest:request 
    queue:[NSOperationQueue mainQueue] 
    completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) { 
     < your code > 
}]; 
+0

工程像魔術。謝謝 – nupac