2016-05-11 2 views
1

私がしていることは、ボタンをクリックするとAFNetworkingポッドを使用してデータを取得していることです。私が望むのは、データを取得した後にNSLog(@"/n /nAfter fetching");と表示したいが、それはjsonデータの前に来る。JSONを取得中にiOSでspinKitを使用しているスピナーを表示

ここに私がButtonの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はAsynchronous操作を行います。あなたは方法の下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

感謝を助ける

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]; }); 

完了してデータを取得しますが、私はフェッチしていたとき、私は、スピナーを表示したいですjson。私は上記のコードを使用して達成することはできますか?私はデータを同期的に取得することは良いプログラミングではないと思うからです。 –

+0

[self startSpinner]; [self stopSpinner]; 私の編集中 – Rajesh

関連する問題