2013-03-05 88 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"; 
    } 
)