2013-03-05 40 views
1

我有一个问题,将JSON数据存储到单个对象的数组中。看起来问题在于执行处理JSON请求的dispatch_asynch。当我在方法之前创建一个断点并且通过应用程序时,它似乎只是落入发送到dispatch_async的块。dispatch_async未被调用

dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0); 
dispatch_async(queue, ^{ 
    NSError *error = nil; 
    NSURL *url = [NSURL URLWithString:@"http://sleepy-dusk-3603.herokuapp.com/companies.json"]; 
    NSString *json = [NSString stringWithContentsOfURL:url 
               encoding:NSASCIIStringEncoding 
               error:&error]; 
    NSLog(@"\nJSON: %@ \n Error: %@", json, error); 

    if(!error) { 
     NSData *jsonData = [json dataUsingEncoding:NSASCIIStringEncoding]; 
     NSDictionary *jsonDict = [NSJSONSerialization JSONObjectWithData:jsonData 
                   options:kNilOptions 
                    error:&error]; 
     NSArray *tempArray = [NSArray arrayWithObjects:@"Call Support Desk", @"Call Genius Bar", nil]; 

     for (NSString *name in [jsonDict valueForKeyPath:@"name"]) { 
      NSString *tempString = [[NSString alloc] initWithString:name]; 
      Company *company = [[Company alloc] initWithName:tempString available_actions:tempArray]; 
      [self addCompany:company]; 

我真的很感谢大家对这个问题的帮助和支持。

+0

你把断点块里面?或者'dispatch_async'调用? – 2013-03-05 15:57:57

+0

当我把断点放在块内时,一切似乎都正常(除了我的UITableView没有被某些原因送入数据)。当我将断点放在调用generate_default_data - >的原始方法上,该方法又调用dispatch_async调用时,事情就不起作用。 – Julian25 2013-03-05 16:07:40

+0

基本上我想弄清楚为什么在dispatch_async块内数组的长度是2,这是假设......以及为什么它在块外面是0。 – Julian25 2013-03-05 16:12:02

回答

1
if(!error) { 
... 
     } 
else 
{ 
    NSLog(@"%@",[error localizedDescription]); 
} 

日志中的错误,并找到发生了什么

此代码我尝试和运行良好

dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0); 
dispatch_async(queue, ^{ 
    NSError *error = nil; 
    NSURL *url = [NSURL URLWithString:@"http://sleepy-dusk-3603.herokuapp.com/companies.json"]; 
    NSString *json = [NSString stringWithContentsOfURL:url 
               encoding:NSASCIIStringEncoding 
               error:&error]; 
    NSLog(@"\nJSON: %@ \n Error: %@", json, error); 

    if(!error) { 
     NSData *jsonData = [json dataUsingEncoding:NSASCIIStringEncoding]; 
     NSDictionary *jsonDict = [NSJSONSerialization JSONObjectWithData:jsonData 
                   options:kNilOptions 
                    error:&error]; 
     NSArray *tempArray = [NSArray arrayWithObjects:@"Call Support Desk", @"Call Genius Bar", nil]; 

     for (NSString *name in [jsonDict valueForKeyPath:@"name"]) { 
      NSString *tempString = [[NSString alloc] initWithString:name]; 
      NSLog(@"%@",tempString); 
     } 
    } 
}); 

响应

2013-03-05 22:02:44.312 newTrial[4711:12303] 
JSON: [{"created_at":"2013-03-04T00:09:06Z","id":1,"name":"Apple","updated_at":"2013-03-04T00:09:06Z","actions":[{"created_at":"2013-03-04T00:09:07Z","id":1,"name":"Call Support Desk","updated_at":"2013-03-04T00:09:07Z"},{"created_at":"2013-03-04T00:09:07Z","id":2,"name":"Call Genius Bar","updated_at":"2013-03-04T00:09:07Z"}]},{"created_at":"2013-03-04T02:01:49Z","id":2,"name":"Comcast","updated_at":"2013-03-04T02:01:49Z","actions":[{"created_at":"2013-03-04T02:01:49Z","id":3,"name":"Account Services","updated_at":"2013-03-04T02:01:49Z"}]}] 
Error: (null) 
2013-03-05 22:02:51.766 newTrial[4711:12303] Apple 

所以我对这个问题的前提是在存储数值的位置。检查它是否已正确初始化 问题可能在这里

Company *company = [[Company alloc] initWithName:tempString available_actions:tempArray]; 
[self addCompany:company]; 
+0

它似乎永远不会执行(!error)块,因为它跳过发送到dispatch_queue的整个块 – Julian25 2013-03-05 16:30:44

+1

Julian25 - 我想你误解了GCD在调试器中的工作方式。如果您跨过或进入dispatch_async调用,它将不会立即调用该块。该块将不会被调用,直到你继续 – 2013-03-05 16:44:17

+0

这实际上很有帮助。我想我误解了GCD在调试器中的工作原理。关于提供的其他答案...我遇到的根本问题在这里讨论: http://stackoverflow.com/questions/15214830/object-array-not-persistent-throughout-ios-program#comment21445661_15214830 – Julian25 2013-03-05 16:58:33

0

工作对我来说就像一个魅力:

dispatch_queue_t _queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0); 
dispatch_async(_queue, ^{ 
    NSError *_error = nil; 
    NSURL *_url = [NSURL URLWithString:@"http://sleepy-dusk-3603.herokuapp.com/companies.json"]; 
    NSData *_json = [NSData dataWithContentsOfURL:_url]; 
    if (_json) { 
     id _response = [NSJSONSerialization JSONObjectWithData:_json options:kNilOptions error:&_error]; 
     if (!_error) { 
      // do whatever you want to do here... 
     } else { 
      NSLog(@"%@", _response); 
     } 
    } 
}); 

的回应是:

(
     { 
     actions =   (
         { 
       "created_at" = "2013-03-04T00:09:07Z"; 
       id = 1; 
       name = "Call Support Desk"; 
       "updated_at" = "2013-03-04T00:09:07Z"; 
      }, 
         { 
       "created_at" = "2013-03-04T00:09:07Z"; 
       id = 2; 
       name = "Call Genius Bar"; 
       "updated_at" = "2013-03-04T00:09:07Z"; 
      } 
     ); 
     "created_at" = "2013-03-04T00:09:06Z"; 
     id = 1; 
     name = Apple; 
     "updated_at" = "2013-03-04T00:09:06Z"; 
    }, 
     { 
     actions =   (
         { 
       "created_at" = "2013-03-04T02:01:49Z"; 
       id = 3; 
       name = "Account Services"; 
       "updated_at" = "2013-03-04T02:01:49Z"; 
      } 
     ); 
     "created_at" = "2013-03-04T02:01:49Z"; 
     id = 2; 
     name = Comcast; 
     "updated_at" = "2013-03-04T02:01:49Z"; 
    } 
)