简体   繁体   English

iOS Objective-C AFNetworking dataTaskWithRequest:completionHandler:无法从完成块内部检索数据

[英]iOS Objective-C AFNetworking dataTaskWithRequest:completionHandler: can't retrieve data from inside completition block

Here is my code: 这是我的代码:

 //create array with all the teams
NSMutableArray *leagueTeams = [[NSMutableArray alloc] init];
//send request

[[sessionManager dataTaskWithRequest:request completionHandler:^(NSURLResponse *response, id responseObject, NSError *error) {
    NSDictionary *responseFromJSONDictionary = (NSDictionary *) responseObject;

    //copy the team's attributes into temp variables
    NSString *tempTeamIDCode = [responseFromJSONDictionary objectForKey:@"teamCode"];
    NSString *tempTeamName = [responseFromJSONDictionary objectForKey:@"teamName"];
    NSInteger tempTeamPoints = [(NSNumber *) [responseFromJSONDictionary objectForKey:@"teamPoints"] integerValue];

    //use temp variables to create a temporary team
    Team *aTeam = [[Team alloc] initWithTeamIdCode:tempTeamIDCode andTeamName:tempTeamName andLeaguePoints:tempTeamPoints];

    //add team to array
    [leagueTeams addObject:[aTeam copy]];

}]resume];

I am trying to make an app that retrieves JSON data from a server. 我正在尝试制作一个从服务器检索JSON数据的应用程序。 for now I'm using a static JSON to retrieve entries. 现在,我正在使用静态JSON来检索条目。 I used breakpoints to follow the variable values. 我使用断点来跟随变量值。 The app successfully retrieves the JSON data, it successfully creates the 3 temp variables and successfully creates the team object AND successfully adds the object to the leageTeams mutablearray, while inside the success code block. 该应用程序成功检索了JSON数据,成功创建了3个临时变量,并成功创建了team对象,并将该对象成功添加到leageTeams mutablearray中,同时位于成功代码块内。

BUT, the moment the application leaves the success block, the leagueTeams array disappears. 但是,当应用程序离开成功块时,LeagueTeams数组就会消失。 it doesn't exist in memory even as an empty array, like it did before the execution of the success block. 它像空数组一样不存在于内存中,就像执行成功块之前一样。

I'm probably doing something very wrong with trying to pass data to an outside variable inside the code block, but all other similar questions had the trouble of not getting the data from the server in time, but in my case the data request is always successful and the JSON response and turning it into an NSDICtionary all work fine....so anyone can help please? 我试图将数据传递到代码块内的外部变量时可能做错了很多,但是所有其他类似的问题都存在无法及时从服务器获取数据的麻烦,但是在我的情况下,数据请求始终成功,并且将JSON响应并将其转换为NSDICtionary都可以正常工作。...所以有人可以帮助吗? Thanks 谢谢

Okay what's happening is the "dataTaskWithRequest:completionHandler:" method is asynchronous! 好的,发生的是“ dataTaskWithRequest:completionHandler:”方法是异步的! Once it starts the program execution doesn't wait for it to return value it continues to next line outside of it. 一旦启动程序,程序将不等待其返回值,而是继续执行其下一行。 So probably the code which you have written below this method is executing first than the code inside the completion handler. 因此,您在此方法下编写的代码可能比完成处理程序中的代码先执行。 So what you can do is trigger a Notification or call some delegate method to run any code after the completion Handler returns the required value. 因此,您可以做的是在完成处理程序返回所需值之后,触发Notification或调用某些委托方法以运行任何代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM