在调用下一个方法之前代码未完成
Code Not Completing Before Next Method Is Called
在我的 iOS 应用程序中,我使用 forecast.io API 获取 3 个特定日期的天气预报。从所有 3 个数组中获取数组后,我想创建一个 NSMutableArray 并将所有这些对象添加到其中。我遇到的问题是它试图在检索预测数据之前创建 NSMutableArray。这是我目前所拥有的:
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);
}
当代码为 运行 时,它会在获取任何预测数据之前触发 allOfIt 的 NSLog,它显示为 null。我错过了什么?
The problem I am getting is that it is trying to create the NSMutableArray before the forecast data is retrieved
是的,没错。问题很简单,你不明白 "asynchronous" 是什么意思。 联网需要时间,而且这一切都在后台进行。同时,您的主代码 不会暂停 ;全部立即执行。
因此,事情不会按照您编写代码的顺序发生。所有三个 getDailyForcastForLatitude
调用立即触发,整个方法结束。然后,慢慢地,一个接一个,不按特定顺序,服务器回调并调用三个完成处理程序(花括号中的东西)。
如果您希望按顺序调用完成处理程序,则需要在 getDailyForcastForLatitude
的完成处理程序 中进行每个 getDailyForcastForLatitude
调用 在它之前调用。或者,以这样一种方式编写您的代码,即完成处理程序何时以何种顺序返回给您并不重要。
在我的 iOS 应用程序中,我使用 forecast.io API 获取 3 个特定日期的天气预报。从所有 3 个数组中获取数组后,我想创建一个 NSMutableArray 并将所有这些对象添加到其中。我遇到的问题是它试图在检索预测数据之前创建 NSMutableArray。这是我目前所拥有的:
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);
}
当代码为 运行 时,它会在获取任何预测数据之前触发 allOfIt 的 NSLog,它显示为 null。我错过了什么?
The problem I am getting is that it is trying to create the NSMutableArray before the forecast data is retrieved
是的,没错。问题很简单,你不明白 "asynchronous" 是什么意思。 联网需要时间,而且这一切都在后台进行。同时,您的主代码 不会暂停 ;全部立即执行。
因此,事情不会按照您编写代码的顺序发生。所有三个 getDailyForcastForLatitude
调用立即触发,整个方法结束。然后,慢慢地,一个接一个,不按特定顺序,服务器回调并调用三个完成处理程序(花括号中的东西)。
如果您希望按顺序调用完成处理程序,则需要在 getDailyForcastForLatitude
的完成处理程序 中进行每个 getDailyForcastForLatitude
调用 在它之前调用。或者,以这样一种方式编写您的代码,即完成处理程序何时以何种顺序返回给您并不重要。