Soheil Novinfard
Soheil Novinfard

Reputation: 1444

recursive function get "Variable is not assignable (missing __block type specifier) error"

I want to send AFNetworking requests consequently in a queue. I create a recursive function as below for this aim:

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];

     NSLog(@"Start ...");

    [self sentTest:0];
}

- (void) sentTest:(int)i{

    if(i >= 10)
    {
        NSLog(@"Finished");
        return;
    }

    NSLog(@"sending message %d ...", i);

    NSMutableDictionary *params = [@{@"param1":     @"value1",
                                     @"param2":     @"value2",
                                     } mutableCopy];
    NSString *webServiceUrl = @"MY_REST_SERVICE_URL";

    AFHTTPSessionManager *manager = [[AFHTTPSessionManager alloc] init];
    manager.responseSerializer = [AFJSONResponseSerializer serializer];


    [manager POST:webServiceUrl parameters:params success:^(NSURLSessionDataTask *task, id responseObject) {
        NSLog(@"message sent successful %d", i);

        // Now call the method again
        [self sentTest:i++];

         return;
         } failure:^(NSURLSessionDataTask *task, NSError *error) {
             NSLog(@"message sent failure %d", i);
             return;
    }];

}

I get this error:

Variable is not assignable (missing __block type specifier)  error"

I know that I need to define block type, but I don't know how to use it in this recursive function.

Upvotes: 1

Views: 194

Answers (1)

trojanfoe
trojanfoe

Reputation: 122391

I had some concerns that manager was being destroyed when the method ends, however it's being retained by the block.

I am not certain your code will work, however the actual error message relates to updating i in the block (and it would need to have the __block attribute applied), however there is no reason to increment i at all, simply pass in i + 1 to the recursed method:

[self sentTest:i + 1];

(you had a missing semicolon in your code, so I am not convinced that is the real code or not).

Upvotes: 1

Related Questions