0

I'm trying to build a sample app that grabs some json from a url and display it in a table view. I'm having an issue storing the data into an object and inserting these objects into an NSMutableArray.

TableView.m

- (void) getDataFromAPI {
    NSString *endpoint = @"http://www.somesite.com/list.php";

    NSURL *url = [NSURL URLWithString:endpoint];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];


    AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {
        [self createMovies: JSON];
    } failure:^(NSURLRequest *request, NSHTTPURLResponse *response , NSError *error , id JSON){
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Alert" message:@"Uh oh, there's been a problem!" delegate:nil cancelButtonTitle:@"Cancel" otherButtonTitles:@"Retry", nil];
        [alert show];
    }];
    [operation start];
}

- (void) createMovies: (id) json {

    NSMutableArray *list = [[NSMutableArray alloc] init];
    for (id entry in json) {
        Movie *aMovie = [[Movie alloc] init];
        aMovie.movieId = [entry objectForKey:@"id"];
        aMovie.title = [entry objectForKey:@"title"];
        aMovie.director = [entry objectForKey:@"director"];
        aMovie.price = [entry objectForKey:@"price"];

        aMovie = nil;
    }
    NSLog(@"%@", list);
    self.movieList = list;
}

- (void)viewDidLoad
{
    self.movieList = [[NSMutableArray alloc] init];
    [super viewDidLoad];

    [self getDataFromAPI];
    self.title = @"Movies";


    NSLog(@"%@", self.movieList);
}

When I try to inspect the self.movieList in viewDidLoad it has zero objects, but when I inspect the list in createMovies I get six objects.

An example of my json data:

[
  {
    "id": 1,
    "title": "Transformers",
    "price": 29.54,
    "Director": "Michael Bay"
  },
  {
    "id": 2,
    "title": "South Park",
    "price": 34.88,
    "author": "Matt Stone, Trey Parker"
  },
  {
    "id": 3,
    "title": "The Hobbit",
    "price": 20.04,
    "author": "Peter Jackson"
  }
]
1
  • Well, your request is asynchronous... Commented Jan 13, 2013 at 18:30

1 Answer 1

5

AFJSONRequestOperation is asynchronously loading the data. So, when the [incorrectly named -- should just be loadDataFromAPI] getDataFromAPI method has returned, the data hasn't yet actually been loaded.

Your createMovies: method should trigger a UI refresh that causes the movies to be displayed.

Sign up to request clarification or add additional context in comments.

6 Comments

I haven't even got to the point of sticking it into the UI yet so a UI refresh won't help I don't think.
Same problem, though. You are asynchronously loading something and printing out the results before it is loaded.
But in the AFJSONRequestionOperation i call createMovies in the success method, does that not mean that the request has finished loading asynchronously?
@creminsn Yes, you call createMovies once the data is finished loading asynchronously. As stated, the problem is in viewDidLoad. When you log the movies in viewDidLoad, the data hasn't loaded yet since it is in progress in the background.
@creminsn You don't. viewDidLoad shouldn't care when it's done. viewDidLoad simply kicks off the loading. Your createMovies deals with the completion. If you need to reload a table view then do so at the end of createMovies. But make sure you call reloadData on the main thread. Your createMovies may be called on a background thread (see the docs as needed).
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.