dispatch_async未被调用

时间:2013-03-05 15:54:14

标签: iphone ios cocoa-touch

我在将JSON数据存储到单个对象的数组中时遇到问题。似乎问题在于执行处理JSON请求的dispatch_asynch。当我在方法之前创建一个断点而不是单步执行应用程序时,它似乎只是通过发送到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];

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

2 个答案:

答案 0 :(得分: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];

答案 1 :(得分: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";
    }
)
相关问题