Jeromiin
Jeromiin

Reputation: 71

AFNetworking iOS

I have a problem with a request in my app, I want to receive a json but because of the completion block my "PRINT 2" is print before my "PRINT 1" and of course my "PRINT 2" is null. I want the contrary and my "PRINT 2" to be filled but I can't manage to do it.

-(void) makeConnection {
NSURL *url = [NSURL URLWithString:[@"http://monsite.com/iPhonej/verifPseudo.php?login="stringByAppendingString:[_loginField.text stringByAppendingString:[@"&password=" stringByAppendingString:_passField.text]]]];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
operation.responseSerializer = [AFJSONResponseSerializer serializer];
[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {

    self.response = (NSDictionary *)responseObject;
    NSLog(@"PRINT 1 : %@", self.response[@"la"][@"reponse"][0][@"rep"]);
    [_dataLock lock];

} failure:^(AFHTTPRequestOperation *operation, NSError *error) {

    NSLog(@"Request Failed: %@, %@", error, error.userInfo);

}];
[operation start];
}

- (IBAction)logIn:(id)sender {
[self makeConnection];
NSLog(@"PRINT 2 : %@", self.response[@"la"][@"reponse"][0][@"rep"]);
}

I know that AFNetworking is asynchronous but is there an other way to do the request and receive my json well ?

Thank you

Upvotes: 1

Views: 129

Answers (2)

Zee
Zee

Reputation: 1905

As it is Async request, move your "PRINT 2" code to an other method and call that method from completion block, like this

-(void) makeConnection {
    NSURL *url = [NSURL URLWithString:[@"http://monsite.com/iPhonej/verifPseudo.php?login="stringByAppendingString:[_loginField.text stringByAppendingString:[@"&password=" stringByAppendingString:_passField.text]]]];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
    operation.responseSerializer = [AFJSONResponseSerializer serializer];
    [operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {

        self.response = (NSDictionary *)responseObject;
        NSLog(@"PRINT 1 : %@", self.response[@"la"][@"reponse"][0][@"rep"]);
        [_dataLock lock];
        [self loggedIN];

    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {

        NSLog(@"Request Failed: %@, %@", error, error.userInfo);

    }];
    [operation start];
}

- (IBAction)logIn:(id)sender {
    [self makeConnection];
}

- (void)loggedIN {
    NSLog(@"PRINT 2 : %@", self.response[@"la"][@"reponse"][0][@"rep"]);
}

Upvotes: 1

Sebastian Wramba
Sebastian Wramba

Reputation: 10127

No, that is actually the nature of an asynchronous request. There is no way to execute it the way you intend. I'm afraid you will have to adjust your design to match the asynchronous nature (e.g. put the "PRINT 2" statement into the completion block as well).

Upvotes: 0

Related Questions