I have a problem regrading accessing an instance variable outside the method.I want to access the value in other methods.But when I am printing that value in other method ,then null value is printed. So guys I need your help.And if you didn't get my questions ,please let me know . Thanks
//I am giving a snippet of my code
//DBForHomeViewController.h class
@interface DBForHomeViewController : UIViewController{
id _jsonDB;
}
-(void)channelIDForDataBase;
-(void)openDataBase;
@end
//DBForHomeViewController.m class
@implementation DBForHomeViewController
-(void)channelIDForDataBase
{
for (NSString *pNewString in stringValueOfCIdDB) {
NSString *recentVideoUrl=[NSString stringWithFormat:@"%@%@",pNewString,fullPublishDate,];
NSURL *videoUrlPublished=[NSURL URLWithString:recentVideoUrl];
NSURLRequest *requestVideoUrlPublished=[NSURLRequest requestWithURL:videoUrlPublished];
AFJSONRequestOperation *operationURL = [AFJSONRequestOperation JSONRequestOperationWithRequest:requestVideoUrlPublished success:^(NSURLRequest *requestURL, NSHTTPURLResponse *response, id getSTATS)
{
_jsonDB= getSTATS;
// NSLog(@"DB data is:%@",_jsonDB);
//I am getting json data here.Now I want to access _jsonDB in other //method ie -(void)openDataBase)
}failure:nil];
[operationURL start];
}
}
-(void)openDataBase
{
NSLog(@"data in another class is :%@",_jsonDB);
//here I am getting _jsonDB value as null
//I want to to have _jsonDB value in this method
//
}
You are setting the value of the variable through an asynchronous call, so it is possible that the call has not finished and set the variable by the time you are asking for it.
You haven't shown the code that you are using to call this variable, but I suspect that you are calling channelIDForDataBase and then calling openDataBase The trouble is that as the first call is asynchronous, it hasn't finished it's network call so the ivar hasn't been set.
You have a couple of possible solutions: one of which might be suitable for your circumstances is to make a call to openDataBase when your async call finishes:
AFJSONRequestOperation *operationURL = [AFJSONRequestOperation JSONRequestOperationWithRequest:requestVideoUrlPublished success:^(NSURLRequest *requestURL, NSHTTPURLResponse *response, id getSTATS) {
_jsonDB= getSTATS;
[self openDataBase];
failure:nil];
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With