2013-10-18 27 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