开发者

Objective C Preserve Changes Made by A Block To A Property

For some reason this code does not work:

[request开发者_运维百科 setCompletionBlock:^{
    NSString *response = [request responseString];
    NSDictionary *data = [response JSONValue];
    NSArray *events = (NSArray *)[data objectForKey:@"objects"];

    for (NSMutableDictionary *event in events){

        /* experimental code*/
        NSString *urlString = [NSString 
                               stringWithFormat:
                               @"http://localhost:8000%@?format=json",
                               [event objectForKey:@"tournament"]];

        NSURL *url2 = [NSURL URLWithString:urlString];
        ASIHTTPRequest *request2 = [ASIHTTPRequest requestWithURL:url2];
        [request2 setCompletionBlock:^{
            NSString *responseString = [request2 responseString];
            NSDictionary *tournamentDict = [responseString JSONValue];
            self.tournamentString = [tournamentDict objectForKey:@"tournament"]; 
        }];
        [request2 startAsynchronous];

        /* end experimental code */
        NSLog(@"%@", self.tournamentString);
        [mutableArray addObject:event];
    }
    self.eventsArray = mutableArray;
    [MBProgressHUD hideHUDForView:self.view animated:YES];

    [self.tableView reloadData];
}];

so here are 2 asynchronous requests, in which I fire one after another. I want to change the value of the property tournamentText after the second request executes.

Inside the completion block for the second request, when I NSLog self.tournamentText, it displays the text I want to retrieve.

Outside the block, an NSLog produces a nil.

What can I do to preserve the changes to self.tournamentText? Thanks in advanced! Please do tell me if I missed an Apple documentation on this.


You should probably apply the __block storage type modifier to the variable (outside of the block).

__block NSDictionary *tournamentDict;

See Apple's documentation on interaction between blocks and variables (in Blocks Programming Topics) for more info.

By the way, you do realize that you have a block within a block, rather than two separate blocks after another? To preserve the changes to the variable outside the second block, try this:

[request setCompletionBlock:^{
    NSString *response = [request responseString];
    NSDictionary *data = [response JSONValue];
    NSArray *events = (NSArray *)[data objectForKey:@"objects"];

    for (NSMutableDictionary *event in events){

        /* experimental code*/
        NSString *urlString = [NSString 
                               stringWithFormat:
                               @"http://localhost:8000%@?format=json",
                               [event objectForKey:@"tournament"]];

        NSURL *url2 = [NSURL URLWithString:urlString];
        ASIHTTPRequest *request2 = [ASIHTTPRequest requestWithURL:url2];
        __block NSDictionary *tournamentDict;
        [request2 setCompletionBlock:^{
            NSString *responseString = [request2 responseString];
            tournamentDict = [responseString JSONValue];
            self.tournamentString = [tournamentDict objectForKey:@"tournament"]; 
        }];
        [request2 startAsynchronous];

        /* end experimental code */
        NSLog(@"%@", self.tournamentString);
        [mutableArray addObject:event];
    }
    self.eventsArray = mutableArray;
    [MBProgressHUD hideHUDForView:self.view animated:YES];

    [self.tableView reloadData];
}];
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜