0

I am trying to use an AFNetworking class to retrieve data from a database. Long story short, the data received from the parameter responseObject is filled with items. Here is my problem however. I am trying to copy the results in responseObject into an NSDictionary called results. I used the following code to get there:

__block NSDictionary *results;

[manager GET:@"http://daneolog.altervista.org/app/getData.php"
  parameters:nil
     success:^(AFHTTPRequestOperation *operation, id responseObject) { results = responseObject;
     NSLog(@"Inside: %@", results); }
     failure:^(AFHTTPRequestOperation *operation, NSError *error) { NSLog(@"%@", error); }];

NSLog(@"Outside: %@", results);

I tried NSLoging the results dictionary INSIDE the success braces, and everything is a-okay there.

I tried NSLoging the results dictionary OUTSIDE the GET function, and it comes up as (null).

These are my results:

2015-11-12 14:34:34.875 TestApp[4864:258743] Outside: (null)
2015-11-12 14:34:35.242 TestApp[4864:258743] Inside: (
        {
        address = "Sample Address";
    }
)

Now notice the peculiar thing: the outside NSLog is being executed first. I don't know why this is so. Can anyone help me on this? Thanks a bundle.

Jack Thomas
  • 308
  • 1
  • 3
  • 15

1 Answers1

2

Your code outside the success block is executing before the success block completes that is why results come up as null. results is on the main thread, and you are not blocking (as is correct with an HTTP request).

You can pass a weak reference to an object, and have that updated. Or if you absolutely need to wait for the result (login for instance) then you should do it on the main thread.

Here is an example of a weak object:

//results is an object created earlier

__weak NSDictionary *weakResults = results;

[manager GET:@"http://daneolog.altervista.org/app/getData.php"
     parameters:nil
     success:^(AFHTTPRequestOperation *operation, id responseObject) {     
             weakResults = responseObject;
             NSLog(@"Inside: %@", results); 
     }
     failure:^(AFHTTPRequestOperation *operation, NSError *error) {  
             NSLog(@"%@", error); 
     }];


//when the success block finishes the results object will be populated

If you wanted to block i.e. do it on the main thread you could use:

NSData *urlData=[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];

However, if the response doesn't come back, your app hangs.

dstudeba
  • 8,878
  • 3
  • 32
  • 41
  • Mmm... I knew something along those lines were happening. How should I "pass a weak reference to an object, and have that updated"? I don't really know much Objective-C (which is why I'm using AFNetworking in the first place :)) – Jack Thomas Nov 12 '15 at 21:53
  • Oook. Here's a funny problem. Whenever I try to access weakResults, it complains at me saying it has to have a __block type specifier. :'( – Jack Thomas Nov 12 '15 at 22:20
  • Here is some information on __block and __weak: http://stackoverflow.com/questions/19227982/using-block-and-weak – dstudeba Nov 12 '15 at 22:31
  • Umm... yeah. So how would I do an AFNetworking GET function on the main thread? – Jack Thomas Nov 12 '15 at 22:51