2016-05-11 39 views
1

我想要做的是,點擊一個按鈕,我使用AFNetworking窗格獲取數據。我想要的是在獲取我想要顯示的數據之後NSLog(@"/n /nAfter fetching");,但它在json數據之前。顯示使用spinKit在iOS中獲取JSON時的微調器

這是我在IBAction中編寫的代碼。

dispatch_queue_t queue = dispatch_get_global_queue(
                  DISPATCH_QUEUE_PRIORITY_DEFAULT, 0); 
     dispatch_sync(queue, ^{ 
      //Load the json on another thread 

      NSURL *url = [NSURL URLWithString:finalURL]; 

      NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration]; 
      AFURLSessionManager *manager = [[AFURLSessionManager alloc]initWithSessionConfiguration:configuration]; 

      NSURLRequest *request = [NSURLRequest requestWithURL:url]; 

      NSURLSessionDataTask *dataTask = [manager dataTaskWithRequest:request completionHandler:^(NSURLResponse *response,id responseObject, NSError *error) { 

       if (error) { 
        NSLog(@"\n Error --> %@",error); 
       } 
       else{ 
        jsonDictionary = (NSDictionary*) responseObject; 
        NSLog(@"/n Data :: %@",jsonDictionary); 
       } 
      }]; 
      [dataTask resume]; 

     }); 

     NSLog(@"/n /nAfter fetching"); 

請提供一個很好的方法來做到這一點,並糾正我如果我錯了它。謝謝。

回答

1

由於AFURLSessionManager進行異步操作。你會得到的數據完成

dispatch_queue_t queue = dispatch_get_global_queue(
                  DISPATCH_QUEUE_PRIORITY_DEFAULT, 0); 
     dispatch_sync(queue, ^{ 
      //Load the json on another thread 
[self startSpinner]; 
      NSURL *url = [NSURL URLWithString:finalURL]; 

      NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration]; 
      AFURLSessionManager *manager = [[AFURLSessionManager alloc]initWithSessionConfiguration:configuration]; 

      NSURLRequest *request = [NSURLRequest requestWithURL:url]; 

      NSURLSessionDataTask *dataTask = [manager dataTaskWithRequest:request completionHandler:^(NSURLResponse *response,id responseObject, NSError *error) { 
    [self stopSpinner]; 

       if (error) { 
        NSLog(@"\n Error --> %@",error); 
       } 
       else{ 

     NSLog(@"/n /nAfter fetching"); //this is where you receive data 
        jsonDictionary = (NSDictionary*) responseObject; 
        NSLog(@"/n Data :: %@",jsonDictionary); 
       } 
      }]; 
      [dataTask resume]; 

     }); 

,如果你想使用下面的方法URLSession將幫助您

+ (NSData *)sendSynchronousRequest:(NSURLRequest *)request 
    returningResponse:(__autoreleasing NSURLResponse **)responsePtr 
    error:(__autoreleasing NSError **)errorPtr { 
    dispatch_semaphore_t sem; 
    __block NSData *  result; 

    result = nil; 

    sem = dispatch_semaphore_create(0); 

    [[[NSURLSession sharedSession] dataTaskWithRequest:request 
     completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) { 
     if (errorPtr != NULL) { 
      *errorPtr = error; 
     } 
     if (responsePtr != NULL) { 
      *responsePtr = response; 
     } 
     if (error == nil) { 
      result = data; 
     } 
     dispatch_semaphore_signal(sem); 
    }] resume]; 

    dispatch_semaphore_wait(sem, DISPATCH_TIME_FOREVER); 

    return result; 
} 
+0

感謝替代方法,使同步操作,但我想說明微調當我取JSON。我可以實現使用上述代碼?因爲我認爲同步讀取數據不是很好的編程。 –

+0

[self startSpinner]; [self stopSpinner]; 在我的編輯 – Rajesh