Note that there are some explanatory texts on larger screens.

plurals
  1. PORetrieve NSURL Data when loaded
    primarykey
    data
    text
    <p>I have a the following retrieveData method in my SendData class. I call this class from my MovieViewController. The method checks if the data fetched from the server is correct, if it is, it returns TRUE. If it is not, it returns FALSE. It works just fine except the method returns to my MovieViewController before the data has time to "download". Hence the method always returns false because it is always null because control is returned to the original view controller.</p> <p>How do I return (AKA exit with the method with the returned value) only when the data has finished loading and not before?</p> <p>Thanks!</p> <p><strong>SendData.m</strong></p> <pre><code>-(BOOL)retrieveData:(NSString *) postID:(NSString *) message:(NSString *)token { __block BOOL value=FALSE; NSURLSessionConfiguration *sessionConfiguration = [NSURLSessionConfiguration defaultSessionConfiguration]; sessionConfiguration.HTTPAdditionalHeaders = @{ @"message" : message, @"access_token": token }; postID=[NSString stringWithFormat:@"https://graph.facebook.com/%@/comments?message=%@&amp;access_token=%@",postID,message,token]; NSURLSession *session = [NSURLSession sessionWithConfiguration:sessionConfiguration]; NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:postID]]; request.HTTPMethod = @"POST"; NSURLSessionDataTask *postDataTask = [session dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) { //THIS NEEDS TO BE EXECUTED BEFORE THE METHOD IS RETURNED BOOL valid; NSString* myString; myString = [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding]; NSLog(@"MY string is %@", myString); if (myString != NULL || [myString length] != 0) { valid = TRUE; } else { valid = FALSE; } value=valid; }]; return value; } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload