2013-10-18 35 views
1

我需要從JSON中下載數據,並將數據分配給NSOperationQueue之外的NSData。這裏是我的代碼:如何將NSData從NSOperationQueue中分配給NSOperationQueue外的NSData

-(void)parsingInfo { 
    NSURL *url = [NSURL URLWithString:@"http://someJSON.json"]; 
    NSData *data; 

    [NSURLConnection sendAsynchronousRequest:[NSURLRequest requestWithURL:url] queue:downloadQueue completionHandler:^(NSURLResponse* response, NSData* jsonData, NSError* error){ 

     if(error) 
     { 
      // Error Downloading data 
      NSLog(@"Error"); 
     } 
     else 
     { 
      data = jsonData; 
     } 
    }]; 

    if (data) { 
     NSError *error; 
     NSDictionary *JSONDic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:&error]; 

     application = [JSONDic objectForKey:@"Applications"]; 
     NSArray *featured = [JSONDic objectForKey:@"Featured"]; 
     NSDictionary *dict2; 
     dict2 = [featured objectAtIndex:0]; 

    } else { 

     NSLog(@"Error, no data!"); 
    } 
} 
+0

將代碼的'if(data)'部分移到NSOperation隊列中。基本上你的函數會啓動一個新線程來運行下載,然後會立即繼續檢查數據是否存在(可能在下載開始之前)。 – Putz1103

+0

ok,但然後數組應用程序將爲零 –

+0

好了,那麼當異步請求結束時,用一個知道數組「應用程序」的新函數觸發主線程。 – Putz1103

回答

-1

如果您希望在'if(data)'語句達到'data(數據)'語句前等待'data'被填充,那麼要麼...... a)進行synchronousRequest調用。

+ (NSData *)sendSynchronousRequest:(NSURLRequest *)request returningResponse:(NSURLResponse **)response error:(NSError **)error 

OR,

二)用戶信號燈塊/直到你接收數據中斷這個線程。

-(void)parsingInfo 
{ 
      NSURL *url = [NSURL URLWithString:@http://someJSON.json]; 
      __block NSData *data; 

       // Create a semaphore to block this thread (or run loop) until we get response back from server. 
      dispatch_semaphore_t waitSemaphore = dispatch_semaphore_create(0); 

      [NSURLConnection sendAsynchronousRequest:[NSURLRequest requestWithURL:url] queue:downloadQueue completionHandler:^(NSURLResponse* response, NSData* jsonData, NSError* error){ 

       if(error) 
       { 
        // Error Downloading data 
        NSLog(@"Error"); 
       } 
       else 
       { 
        data = jsonData; 
       } 

       // Signal to release the semaphore (i.e. unblock the current thread). 
       dispatch_semaphore_signal(waitSemaphore); 

      }]; 
       // A dispatch semaphore is an efficient implementation of a traditional counting semaphore. Dispatch semaphores call down 
      // to the kernel only when the calling thread needs to be blocked. If the calling semaphore does not need to block, no kernel call is made. 
      while (dispatch_semaphore_wait(waitSemaphore, DISPATCH_TIME_NOW)) 
      { 
       [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate dateWithTimeIntervalSinceNow:10]]; 
      } 

      // Release the semaphore. 
      dispatch_release(waitSemaphore); 

      if (data) { 
       NSError *error; 
       NSDictionary *JSONDic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:&error]; 

       application = [JSONDic objectForKey:@"Applications"]; 
       NSArray *featured = [JSONDic objectForKey:@"Featured"]; 
       NSDictionary *dict2; 
       dict2 = [featured objectAtIndex:0]; 

      } else { 

       NSLog(@"Error, no data!"); 
      } 

}

隨意問任何問題。

+1

謝謝,現在超級快,但你應該替換NSData *數據;與__block NSData *數據; –

+0

謝謝指出。現在糾正它。 – Ashok

+0

爲什麼反對投票?這是一個公認的答案,我在我的一個項目中使用了這個解決方案。 – Ashok

2

將一個塊傳遞到NSOperation中,該操作可以使用NSData對象作爲參數調用。

+0

你的意思是__block NSData *數據? –

+0

不,你只需要,如果你要修改塊中的數據。您只需要操作將數據傳回給塊。 – jsd