wigging
wigging

Reputation: 9170

Parsing JSON with AFHTTPClient

I'm using the AFNetworking library to parse json using the AFHTTPClient. I can verify that the json is being parsed within the client block and send that data to my json model. However, when I try to access the json model from outside the block I get no data. How can I pass the parsed json data to the json model then access that model data elsewhere in the app?

the AFHTTPClient subclass / singleton:

#import <Foundation/Foundation.h>
#import "AFHTTPClient.h"

@interface JsonClient : AFHTTPClient

+ (JsonClient *)sharedClient;

@end

#import "JsonClient.h"
#import "AFJSONRequestOperation.h"

static NSString *const kJsonBaseURLString = @"https://alpha-api.app.net/";

@implementation JsonClient

+ (JsonClient *)sharedClient {
    static JsonClient *_sharedClient = nil;

    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        _sharedClient = [[JsonClient alloc] initWithBaseURL:[NSURL URLWithString:kJsonBaseURLString]];
    });

    return _sharedClient;
}

- (id)initWithBaseURL:(NSURL *)url {
    self = [super initWithBaseURL:url];

    if (!self) {
        return nil;
    }

    [self registerHTTPOperationClass:[AFJSONRequestOperation class]];
    [self setDefaultHeader:@"Accept" value:@"application/json"];

    return self;
}

@end

the JSON model data:

#import <Foundation/Foundation.h>

@interface TheJson : NSObject

@property (nonatomic, copy) NSString *createdAt;
@property (nonatomic, copy) NSString *userText;

- (id)initWithDictionary:(NSDictionary *)dict;

@end

#import "TheJson.h"

@implementation TheJson

- (id)initWithDictionary:(NSDictionary *)dict {
    self = [super init];

    if (self) {
        self.createdAt = [dict objectForKey:@"created_at"];
        self.userText = [dict objectForKey:@"text"];
    }

    return self;
}

@end

the ViewController to update the user interface:

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController

@end

#import "ViewController.h"
#import "JsonClient.h"
#import "TheJson.h"

@interface ViewController ()

@property (weak) IBOutlet UILabel *createdLabel;
@property (weak) IBOutlet UILabel *textLabel;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

- (IBAction)fetchJsonData:(id)sender {

    [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES];

    [[JsonClient sharedClient] getPath:@"stream/0/posts/stream/global" parameters:nil
                               success:^(AFHTTPRequestOperation *operation, id JSON) {
                                   NSArray *postsFromResponse = [JSON valueForKeyPath:@"data"];
                                   NSDictionary *dictFromArray = postsFromResponse[0];

                                   TheJson *jsonObject = [[TheJson alloc] initWithDictionary:dictFromArray];
                                   NSLog(@"createdAt is %@", jsonObject.createdAt);
                                   NSLog(@"text from user is %@", jsonObject.userText);

                                   [self updateInterface];
                                   [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];

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

- (void)updateInterface {
    TheJson *thejson;
    [_createdLabel setText:thejson.createdAt];
    [_textLabel setText:thejson.userText];
}

@end

Upvotes: 0

Views: 603

Answers (1)

Firoze Lafeer
Firoze Lafeer

Reputation: 17143

You haven't passed the new jsonObject out of the block nor stored it anywhere. A short term answer is to declare updateInterface to take the jsonObject as a parameter.

So your updateInterface becomes updateInterface: something like this:

- (void)updateInterface:(TheJson*)thejson {
    [_createdLabel setText:thejson.createdAt];
    [_textLabel setText:thejson.userText];
}

...and then within your block, you call this method like this:

[self updateInterface:jsonObject];

Longer term, if your app has many of these objects and/or needs to hold onto them for any amount of time, you probably want to think about how you will store and organize these as you download them.

Upvotes: 2

Related Questions