0

I am using AFNetworking to get a JSON response. I am getting is as a PhotoPXArray (model I created using mantle). The log output is exactly the data I want. My problem is using the data. How do I go about saving the response data as a variable that can be used elsewhere in my program.

Also, I am using Sculptor to help with serializing.

-(NSArray*) getPhotoForWord:(NSString*)word {
    NSArray *results = nil;

    NSString *requestString = BASE_URL;
    requestString = [requestString stringByAppendingString:@"photos/search?term="];
    requestString = [requestString stringByAppendingString:word];
    requestString = [requestString stringByAppendingString:CONSUMER_KEY];



    NSString *encoded = [requestString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    manager.responseSerializer = [SCLMantleResponseSerializer serializerForModelClass:PhotoPXArray.class];
    [manager GET:encoded
      parameters:nil
         //success:^(AFHTTPRequestOperation *operation, id responseObject) {
         success:^(AFHTTPRequestOperation *operation, PhotoPXArray *responseObject) {
             NSLog(@"JSON: %@", responseObject);


         } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
             NSLog(@"Error: %@", error);
         }];


    return results;
}
@end
kinezu
  • 1,212
  • 2
  • 12
  • 23

2 Answers2

0

Read the Apple documentation regarding blocks and variables. Or you can view this question on SO that will probably also answer your question.

From the Apple docs:

__block variables live in storage that is shared between the lexical scope of the variable and all blocks and block copies declared or created within the variable’s lexical scope. Thus, the storage will survive the destruction of the stack frame if any copies of the blocks declared within the frame survive beyond the end of the frame (for example, by being enqueued somewhere for later execution). Multiple blocks in a given lexical scope can simultaneously use a shared variable.

Community
  • 1
  • 1
Michael
  • 6,561
  • 5
  • 38
  • 55
  • Thank you! I tried _block with 1 underscore. I didn't notice the second one until now. – kinezu Mar 14 '15 at 06:23
  • 1
    Even marked as `__block`, this array will be returned immediately and be empty, not having time for the request to complete. This is not the right way. – Jon Shier Mar 14 '15 at 06:30
0

Use a completion block to get your data out:

- (void)getPhotoForWord:(NSString *)word completionHandler:(void ^(PhotoPXArray *photoArray))completionHandler
{
    NSString *requestString = BASE_URL;
    requestString = [requestString stringByAppendingString:@"photos/search?term="];
    requestString = [requestString stringByAppendingString:word];
    requestString = [requestString stringByAppendingString:CONSUMER_KEY];

    NSString *encoded = [requestString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    manager.responseSerializer = [SCLMantleResponseSerializer serializerForModelClass:PhotoPXArray.class];
    [manager GET:encoded
      parameters:nil
         success:^(AFHTTPRequestOperation *operation, PhotoPXArray *responseObject) {
            NSLog(@"JSON: %@", responseObject);
            if (completionHandler) {
                completionHandler(responseObject);
            }
      } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
            NSLog(@"Error: %@", error);
     }];
}

Then call it like this:

[object getPhotoForWord:@"word" completionHandler:^(PhotoPXArray *photoArray) {
    // Do something with photo array.
}];

Note that this call is asynchronous and will complete at some unknown time in the future. Also, you should likely take an NSError argument in the completion block so you can see if you get an error from the request, but I'll leave that to you.

Jon Shier
  • 12,200
  • 3
  • 35
  • 37