1

I'm building my app with TheMovieDB API but my JSONObject always is nil.I don't know what I'm doing wrong.This is my code:

@implementation SKTMovieLibrary

-(id) init{

   if (self = [super init]) {


    NSURL *url = [NSURL URLWithString:@"https://api.themoviedb.org/3/movie/now_playing?api_key=myApiKey"];

    NSURLSession *session = [NSURLSession sharedSession];
    NSURLSessionDataTask *dataTask = [session dataTaskWithURL:url completionHandler:^(NSData *data, NSURLResponse *response, NSError *err){


        NSDictionary * movieDict = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil];


        NSArray *JSONArray =  [movieDict objectForKey:@"results"];

        dispatch_async(dispatch_get_main_queue(), ^{


            _JSONObject = JSONArray;

        });


    }];
    [dataTask resume];

Thanks.

Skurt
  • 11
  • 7
  • did you look at your data argument in the debugger? does your dataTask return any results? – Eugene Gordin Mar 04 '16 at 00:28
  • No,nothing and I have set a breakpoint in the completion block and it is working correctly but when it finish the JSONObject is nil again. – Skurt Mar 04 '16 at 00:33
  • not sure I understood you. So in your completion handler for task, the data is nil or not? If it is not nil, your jsonArray has content, right? and then when you do dispatch jsonObject is nil? – Eugene Gordin Mar 04 '16 at 00:37
  • also... _JSONObject looks like a strong property, shouldn't it have self. JSONObject since it's inside a block, and maybe use weak, to make sure you don't have retain cycle ? – Eugene Gordin Mar 04 '16 at 00:40
  • In my completion block, the data is not nil and my jsonArray has content but when i do dispatch jsonObject is nil and i need it for do a for in and create my model for each NSDictionary in jsonObject – Skurt Mar 04 '16 at 00:41
  • @property (weak,nonatomic) NSArray *jsonObject; – Skurt Mar 04 '16 at 00:47
  • JSONArray inside of the dispatch block, is it nil? Have you tried to allocate array ? NSArray *JSONArray = [NSArray arrayWithArray:[movieDict objectForKey:@"results"]]; ? – Eugene Gordin Mar 04 '16 at 00:47
  • No JSONArray isnt nil inside of the dispatch block – Skurt Mar 04 '16 at 00:56
  • I just ran your code, and it works, BUT...for me it's not in the init method...can this be an issue? – Eugene Gordin Mar 04 '16 at 00:58
  • In what method did u do it?I'm going to try it – Skurt Mar 04 '16 at 01:00
  • for me it's a viewController, so I placed it in viewWillAppear – Eugene Gordin Mar 04 '16 at 01:03
  • @EugeneZhenyaGordin yes but in my case i have it in a model of a list of movie object for a collection view controller so i must do in my model – Skurt Mar 04 '16 at 01:06
  • hm...you're trying to get the UI items from the cloud, right? like images and stuff....you should do this in the viewController...where your collectionView is...on viewWillAppear do this request...give a spinner...then load your view, I don't think you should do the data fetch in the model! – Eugene Gordin Mar 04 '16 at 01:09
  • On my last application I did a similar model with wines and it works fine but i don't know why it doesn't work now – Skurt Mar 04 '16 at 01:14

1 Answers1

0

something like this:

[UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
NSURLSessionDataTask *dataTask = [_session dataTaskWithURL:url
 completionHandler:^(NSData *data, NSURLResponse *response, 
NSError *error) {
if (!error) {
  UIImage *image = [[UIImage alloc] initWithData:data];
  photo.thumbNail = image;
  dispatch_async(dispatch_get_main_queue(), ^{
    [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
    cell.thumbnailImage.image = photo.thumbNail;
  });
} else {
  // HANDLE ERROR //
}
}];
[dataTask resume];
Eugene Gordin
  • 4,047
  • 3
  • 47
  • 80
  • I have tried the same code but in CollectionViewController in viewWillAppears and i have the same issue – Skurt Mar 04 '16 at 01:23
  • and your @property (weak,nonatomic) NSArray *jsonObject; is a property inside CollectionViewController ? Can you please post that code? top of your class with properties declared and down to init methods like viewDidLoad, viewWillAppear etc. ? – Eugene Gordin Mar 04 '16 at 03:55