I have a property that changes to invalid as I move between viewDidLoad and viewWillAppear. I have no idea why that happens.
I have a helper class that gets the path to my database. I alloc this class in the app delegate. RootViewController gets a reference to the appDelegate like this:
//inside .h file @interface RootViewController : UITableViewController <UITableViewDelegate, UITableViewDataSource>{ NSArray *controllers; myAppDelegate *appDelegate; } //inside .m file @implementation RootViewController @synthesize controllers; - (void)viewDidLoad { appDelegate = [[UIApplication sharedApplication] delegate]; self.controllers = appDelegate.topicControllers; [super viewDidLoad]; }
I mouse over appDelegate to find the database helper class and the property containing the database path:
NSPathStore2 * appDelegate.databaseHeper.databasePath '/Users/userA/Library/Application Support/iPhone Simulator/User/Applications...'
databasePath is declared an NSString. Why has it changed to NSPathStore2?
As I continue in the debugger some other strange thing happens. Once I get in viewWillAppear, the property value becomes ‘invalid’ and the type is back to NSString. Althought I’ve even seen UIButton in there.
Why does it change to invalid? I can no longer use the variable.
- (void)viewWillAppear:(BOOL)animated { [super viewWillAppear:animated]; //changes before this line executes }
I don’t do anything between viewDidLoad and viewWillAppear. Here is how databasePath is assigned in the databaseHelper class, which occurs earlier on applicationDidFinishLaunching:
- (NSString *) getDBPath { NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory , NSUserDomainMask, YES); NSString *documentsDir = [paths objectAtIndex:0]; return [documentsDir stringByAppendingPathComponent:self.databaseName]; }
stringByAppendingPathComponent returns an NSString. Why do I get an NSStorePath2 sometime down the road after the assignment?
I have everything working without issue now but just calling [databaseHelper getDBpath]. My goal is to save some cpu cycles and store that value. Hence databasePath. Any suggestions why databasePath changes to invalid?
Because you have not retained the value. Review the rules in Memory Management Programming Guide to see when you need to retain something.
Of course, as cdespinosa notes, you probably don’t need to cache this pathname in the first place. It’s probably not a big performance-killer. Profile first, then optimize out the hot spots.