异步请求返回nil

时间:2014-03-22 19:35:33

标签: ios iphone objective-c asynchronous

我已经做了以下异步请求,问题是它已经空了我在底部NSLog尝试了它的空的灯具。我已经检查过nsstring home,away,league等返回值并确实如此。为什么这些值没有添加到灯具NSMutableArray

[ProgressHUD show:@"Loading..."];

NSURL *url = [NSURL URLWithString:@"API_URL"];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[NSURLConnection sendAsynchronousRequest:request
                                   queue:[NSOperationQueue mainQueue]
                       completionHandler:^(NSURLResponse *response,
                                           NSData *data, NSError *connectionError)
 {

         jsonResult = [NSJSONSerialization JSONObjectWithData:data
                                                                  options:0
                                                                    error:NULL];



         int subObjects = ((NSArray *)jsonResult[@"match"]).count;
         for (int i = 0; i <= subObjects-1; i++) {

             NSString *date = [NSString stringWithFormat:@"%@ %@",[[[jsonResult valueForKey:@"match"] valueForKey:@"playdate"] objectAtIndex:i], [[[jsonResult valueForKey:@"match"] valueForKey:@"time"] objectAtIndex:i]];
             NSString *identifier = [[NSLocale currentLocale] localeIdentifier];

             NSDateFormatter *df = [[NSDateFormatter alloc] init];
             [df setTimeZone: [NSTimeZone timeZoneWithName:@"US/Arizona"]];
             [df setLocale:[NSLocale localeWithLocaleIdentifier:identifier]];
             [df setDateFormat:@"yyyy-MM-dd HH:mm:ss"];

             NSDate *myDate = [df dateFromString:[NSString stringWithFormat:@"%@", date]];





             NSArray *items = [[NSString stringWithFormat:@"%@", myDate] componentsSeparatedByString:@" "];


             NSString *home = [[[jsonResult valueForKey:@"match"] valueForKey:@"hometeam"] objectAtIndex:i];
             NSString *away = [[[jsonResult valueForKey:@"match"] valueForKey:@"awayteam"] objectAtIndex:i];
             NSString *league = [[[jsonResult valueForKey:@"match"] valueForKey:@"league"] objectAtIndex:i];




             [fixtures addObject:
              [[NSMutableDictionary alloc] initWithObjectsAndKeys:
               items[0], @"date",
               items[1], @"time",
               home, @"home",
               away, @"away",
               league, @"league",
               nil]];

             [sections addObject:
              [[NSMutableDictionary alloc] initWithObjectsAndKeys:
               items[0], @"date",
               nil]];


         }


     }
 ];



[self.theTableView reloadData];

[ProgressHUD dismiss];

NSLog(@"%@", fixtures);

3 个答案:

答案 0 :(得分:1)

问题是请求是异步函数

如果函数是异步的,该函数将创建另一个线程并立即返回以执行调用异步函数的线程之后的下一行。同时新线程将执行一些代码,并最终执行作为参数传递的块,最后线程被终止并且不再存在。

这意味着

NSLog(@"%@", fixtures);

很可能会在sendAsynchronousRequest完成工作之前执行,这就是它返回nil的原因。

处理下载信息所需的一切都应该在completionHandler区块内进行,包括致电[self.theTableView reloadData];

答案 1 :(得分:0)

if(!fixtures) {
    fixtures = [[NSMutableArray alloc] init];
}

[fixtures addObject:@{
    @"date": items[0],
    @"time": items[1],
    @"home": home,
    @"away": away,
    @"league": league
}];

if(!sections) {
    sections = [[NSMutableArray alloc] init];
}

[sections addObject:@{
    @"date": items[0]
}];

[self.theTableView reloadData];

[ProgressHUD dismiss];

NSLog(@"%@", fixtures);

答案 2 :(得分:0)

这是一种非阻塞操作。这意味着通过调用此方法,它会在实际请求在后台某处执行时立即返回,然后在queue参数中指定的队列上调用处理程序块。 您应该从完成处理程序块重新加载tableview。

// 1 before request
NSURLConnection sendAsynchronousRequest:request
                                   queue:[NSOperationQueue mainQueue]
                       completionHandler:^(NSURLResponse *response,
                                           NSData *data, NSError *connectionError)
{
// 3 request completed
// some processing
...
[self.theTableView reloadData];

[ProgressHUD dismiss];
}
// 2 immediate return

<强>更新

虽然您将主队列作为queue参数传递,但在重新加载表并记录值后,将在下一次运行循环迭代时执行处理程序块。

// current run loop iteration
NSURLConnection sendAsynchronousRequest:request
                                   queue:[NSOperationQueue mainQueue]
                       completionHandler:^(NSURLResponse *response,
                                           NSData *data, NSError *connectionError)
{
// next run loop iteration
}
// current run loop iteration