0

I am using following method to get back an NSDictionary object in ViewDidAppear. But when I attempt to access it in CellForRowAtIndexPath() it is always nil. I have tried adding an extra retain and copy to it, but it still gets released. I have been pulling my hair for 3 hours now. Any help would be appreciated.

Excerpt :

@property(nonatomic, retain) NSDictionary* userInfoObj;  

- (void) viewDidAppear:(BOOL)animated  
{  
 [super viewWillAppear:animated];  
        **//The object has data in it at this point**  
 self.UserInfoObj  = [self getUserInfo];  

}  

- (NSDictionary*)getUserInfo  
{  
 JsonHelper *helper=[[JsonHelper alloc] autorelease];  
 NSString* apiURL = [self.appDelegate urlGetUserInfo];  
 apiURL = [apiURL stringByReplacingOccurrencesOfString:@"{user_id}" withString:[UserSettings lastLoginUserId]];   
 return [helper getJsonDictionaryFromWebMethod:apiURL];  
}  


- (NSDictionary*)getJsonDictionaryFromWebMethod :(NSString*) url  
{  
.....  
.....  
....  
 // Get JSON as a NSString from NSData response  
 NSString *json_string = [[NSString alloc] initWithData:response encoding:NSUTF8StringEncoding];  

 // parse the JSON response into an object  
 // Here we're using NSArray since we're parsing an array of JSON status objects  
 dict = [[parser objectWithString:json_string error:nil] retain];  

 return dict;  
}  
kennytm
  • 510,854
  • 105
  • 1,084
  • 1,005
StackThis
  • 1,262
  • 1
  • 14
  • 23

1 Answers1

0

Try putting self.UserInfoObj = [self getUserInfo]; in the viewDidLoad delegate method instead.

Evan Mulawski
  • 54,662
  • 15
  • 117
  • 144
  • Thanks. That did the trick! Wohoo! But why was that happening. I would like to call that API every time the view is shown not just once. That is why I had it in viewDidAppear. Also, this view is loaded from a UITabBar (i.e user clicks on Tab# 3 and the view gets loaded) – StackThis Oct 21 '10 at 19:16
  • (As a side note, you have the incorrect super method inside of the viewDidAppear delegate method, they should match.) I have been trying to figure out the same question, but I found that using NSLog("This method was called!") inside of the view's delegate methods helps to narrow down the ones that respond to certain events. – Evan Mulawski Oct 21 '10 at 19:26
  • Still looking for an answer on this one. Why does the array get released when populated in ViewWillAppear but stays retained when populated in ViewDidLoad – StackThis Nov 13 '10 at 21:09
  • I have been investigating this. If you are using `viewWillAppear` inside a view controller, it must be called manually. I am not sure why this is so. There are several active SO questions like this: http://stackoverflow.com/questions/895713/viewdidload-gets-called-viewwillappear-does-not-get-called-view-does-not-appear – Evan Mulawski Nov 13 '10 at 21:19
  • I put a breakpoint in ViewDidAppear and it always gets called for me and I can even see the array getting populated with 10 objects. But when I call [self.tableView reloadData] and it reaches...numberOfRowsInSection {} the array has been released and contains 0 objects !!! As I mentioned above if I populate the same array in ViewDidLoad the array stays retained. – StackThis Nov 14 '10 at 22:34