1
-(NSDictionary *)fetchFromUrl:(NSString *)url{
    NSURLRequest *request = [NSURLRequest  requestWithURL:[NSURL URLWithString:url]];
    NSURLSession *session = [NSURLSession sharedSession];
    NSURLSessionDataTask *task = [session dataTaskWithRequest:request
                                        completionHandler:
                              ^(NSData *data, NSURLResponse *response, NSError *error) {
                                  dataFetched = [NSJSONSerialization JSONObjectWithData:data
                                                                                           options:0
                                                                                             error:NULL];

                              }];
    [task resume];
    NSLog(@"dataFetched, %@", dataFetched);

    return dataFetched;
}

So I have tried putting the dataFetched as a global variable so I could access it around my .m file and make it accessible to other .m file but when I tried to NSLog the dataFetched from other .m file it outputs (null). Is there anyway I could make the data accessible throughout my other .m files that needed the data?

Nirav D
  • 71,513
  • 12
  • 161
  • 183
jane
  • 309
  • 2
  • 11

2 Answers2

1

You need to use block with your method, instead of returning NSDictionary, So change your code like this.

First Change your method like this

-(void)fetchFromUrl:(NSString *)url withDictionary:(void (^)(NSDictionary* data))dictionary{ 
    NSURLRequest *request = [NSURLRequest  requestWithURL:[NSURL URLWithString:url]];
    NSURLSession *session = [NSURLSession sharedSession];
    NSURLSessionDataTask *task = [session dataTaskWithRequest:request
                                            completionHandler:
                                  ^(NSData *data, NSURLResponse *response, NSError *error) {
                                      NSDictionary *dicData = [NSJSONSerialization JSONObjectWithData:data
                                                                                    options:0
                                                                                      error:NULL];
                                      dictionary(dicData);
                                  }];
    [task resume];          
}

Now call your method like this

[self fetchFromUrl:urlStr withDictionary:^(NSDictionary *data) {
    self.dataFetched = data;
    NSLog(@"data %@",data);
}];
Nirav D
  • 71,513
  • 12
  • 161
  • 183
  • it worked! but how does this (void (^)(NSDictionary* data))dictionary works? – jane Aug 01 '16 at 05:18
  • You are making an API call and that works in the background (async), and the the function that you declare with return `NSDictionary` is work in sync manner thats why when you get the response before that it will return the blank Dictionary, where as block work in async manner. – Nirav D Aug 01 '16 at 05:29
  • okay, it worked, but how am I going to access the data values? I tried using __block but it still not getting the values. – jane Aug 01 '16 at 05:48
  • remove that __block, there is no necessary for that and that data is the dictionary that you want. – Nirav D Aug 01 '16 at 05:58
  • yes you are right, but I need that data to be available throughout the .m file because I need it to populate a UITableViewController cells. – jane Aug 01 '16 at 23:52
  • Check my edited answer instead of `self.dataFetched` inside method use it when you call the method, now you can reload the table after initalization of `self.dataFetched` – Nirav D Aug 02 '16 at 04:18
0

If you modify your NSDictionary inside the block you need declare __block attribute for your property like below

@property (nonatomic, strong) __block NSDictionary *dataFetched;

Look at doc

Use __block Variables to Share Storage If you need to be able to change the value of a captured variable from within a block, you can use the __block storage type modifier on the original variable declaration. This means that the variable lives in storage that is shared between the lexical scope of the original variable and any blocks declared within that scope.

iSashok
  • 2,326
  • 13
  • 21