Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does NSString property change to invalid value?

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?

like image 451
4thSpace Avatar asked Apr 04 '09 16:04

4thSpace


2 Answers

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.

like image 112
Peter Hosey Avatar answered Sep 21 '22 01:09

Peter Hosey


I believe you mean NSPathStore2, not NSStorePath.

NSPathStore2 is in internal private subclass of NSString that is used among subsystems where the string is known to be a file system path. It is functionally equivalent to an NSString in all the ways you would care about.

It's not clear from your post whether you have any issues other than wondering what that class is, and whether there is any optimization to be gained by caching it. The answer is probably not: of all the things that use CPU cycles when accessing a database, the access to its file system path is probably going to be last on the list.

like image 22
cdespinosa Avatar answered Sep 19 '22 01:09

cdespinosa