在调用下一个方法之前代码未完成 [英] Code Not Completing Before Next Method Is Called

查看:110
本文介绍了在调用下一个方法之前代码未完成的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的iOS应用中,我使用forecast.io API获取3个特定日期的天气预报。一旦我从所有3中获取数组,我想创建一个NSMutableArray并将所有这些对象添加到它。我得到的问题是它在检索预测数据之前尝试创建NSMutableArray。以下是我到目前为止:

In my iOS app, I am using the forecast.io API to get a weather forecast for 3 specific days. Once I get the array from all 3, I want to create an NSMutableArray and add all of those objects to it. The problem I am getting is that it is trying to create the NSMutableArray before the forecast data is retrieved. Here is what I have so far:

typedef void(^myCompletion)(BOOL);
-(void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:YES];
    [self myMethod:^(BOOL finished) {
        if(finished){
            NSMutableArray *allOfIt = [[NSMutableArray alloc] initWithObjects:self.weatherSaturday, self.weatherSunday, self.weatherMonday, nil];
            NSLog(@"%@", allOfIt);
        }
    }];

}
-(void) myMethod:(myCompletion) compblock{
    //do stuff
    ForecastKit *forecast = [[ForecastKit alloc] initWithAPIKey:@"MY-API-KEY"];
    // Request the forecast for a location at a specified time
    [forecast getDailyForcastForLatitude:37.438905 longitude:-106.886051 time:1467475200 success:^(NSArray *saturday) {

    //    NSLog(@"%@", saturday);
        self.weatherSaturday = saturday;


    } failure:^(NSError *error){

        NSLog(@"Daily w/ time %@", error.description);

    }];

    [forecast getDailyForcastForLatitude:37.438905 longitude:-106.886051 time:1467561600 success:^(NSArray *sunday) {

        //  NSLog(@"%@", sunday);
        self.weatherSunday = sunday;

    } failure:^(NSError *error){

        NSLog(@"Daily w/ time %@", error.description);

    }];

    [forecast getDailyForcastForLatitude:37.438905 longitude:-106.886051 time:1467648000 success:^(NSArray *monday) {

        // NSLog(@"%@", monday);
        self.weatherMonday = monday;

    } failure:^(NSError *error){

        NSLog(@"Daily w/ time %@", error.description);

    }];

    compblock(YES);
}

当代码运行时,它会激活NSLog for allOfIt,显示为null,在获取任何预测数据之前。我缺少什么?

When the code is ran, it fires the NSLog for allOfIt, which shows as null, before it gets any of the forecast data. What am I missing?

推荐答案


我遇到的问题是它正在尝试创建检索预测数据之前的NSMutableArray

The problem I am getting is that it is trying to create the NSMutableArray before the forecast data is retrieved

是的,确切地说。问题只是你不明白异步是什么意思。 网络需要时间,这一切都发生在后台。同时,您的主要代码不会暂停;这一切都是立即执行的。

Yup, exactly. The problem is simply that you don't understand what "asynchronous" means. Networking takes time, and it all happens in the background. Meanwhile, your main code does not pause; it is all executed instantly.

因此,事情不会按照编写代码的顺序发生。所有三个 getDailyForcastForLatitude 立即触发,整个方法结束。然后,慢慢地,一个接一个,没有特定的顺序,服务器回调,并调用三个完成处理程序(大括号中的东西)。

Things, therefore, do not happen in the order in which your code is written. All three getDailyForcastForLatitude calls fire off immediately and the whole method ends. Then, slowly, one by one, in no particular order, the server calls back and the three completion handlers (the stuff in curly braces) are called.

如果你想要要按顺序调用完成处理程序,你需要每个 getDailyForcastForLatitude 调用 getDailyForcastForLatitude的完成处理程序在它之前调用。或者,编写代码的方式使得完成处理程序何时以及以何种顺序返回给您无关紧要。

If you want the completion handlers to be called in order, you need each getDailyForcastForLatitude call to be made in the completion handler of the getDailyForcastForLatitude call that precedes it. Or, write your code in such a way that it doesn't matter when and in what order the completion handlers come back to you.

这篇关于在调用下一个方法之前代码未完成的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆