views:

1339

answers:

2

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?

+2  A: 

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.

cdespinosa
I didn't understand your last paragraph or why you believe having your value turn into an invalid isn't an issue. I've edited the post to hopefully be more clear. Not sure why there is a -1 vote.
4thSpace
It didn't turn into an invalid. It didn't turn into anything. “NSPathStore2 is [an] internal private subclass of NSString”—i.e., that object *is* an NSString. It's not a direct instance of NSString; it's an instance of a subclass, which is good enough. Just treat it as you would any other NSString.
Peter Hosey
The problem is that I'm are expecting a path in databaePath. But instead get a value of "invalid" when there use to be a path. For whatever reason, the path value disappeared. You keep giving definitions of NSString and NSPathStore2, which isn't what I'm asking.
4thSpace
Actually, what NSPathStore2 is is exactly what you asked. As for why it changes to invalid, you should read the memory management docs. http://developer.apple.com/documentation/Cocoa/Conceptual/MemoryMgmt/
Peter Hosey
(And yes, now I see that the part about it changing to invalid is in your question, but it's quite buried and has nothing to do with the question in your title.)
Peter Hosey
Sorry - you are right. [Editing title...] What are you saying I'm doing wrong in the way of memory management. It isn't apparent to me. Hence my (detailed) question.
4thSpace
+3  A: 

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.

Peter Hosey
How is it you are able to deduce I'm not retaining when the implementation for databasePath hasn't been displayed? Here's the implementation: @property (nonatomic, retain) NSString *databasePath;
4thSpace
Because that's what causes that symptom. Either your object died (because you didn't retain it) or you replaced it with another one (and didn't retain it). Or you over-released it, but that's less likely. As for the @property: Are you actually using the property, or assigning directly to the ivar?
Peter Hosey
>>...or assigning directly to the ivar?<< That was the problem. In databaseHelper, I was assigning databasePath instead of self.databasePath. The latter retains the value. What's the difference? And big thanks!
4thSpace
self.databasePath is the property-access syntax; the compiler translates “self.databasePath = foo” into “[self setDatabasePath:foo]”—a call to your accessor method. The accessor releases the old value and retains the new one.
Peter Hosey
Thanks. How does it translate without self?
4thSpace
Without the “self.” prefix, it's implicitly “self->databasePath”—i.e., direct instance-variable access. An illustration: You can reassign self (common in -init implementations), and “databasePath” will directly access that ivar in the new self, just as “self->databasePath” would.
Peter Hosey
And yes, this means you can directly access instance variables in other objects using the same -> operator (if the object and ivar meet certain conditions, detailed in the Objective-C Programming Language document). It's just not usually a good idea, which is why you rarely see it.
Peter Hosey
In regards to databasePath becoming invalid, did that happen because I was bypassing @property or @synthesize that is used via self.databasePath? I'm not sure what you mean by accessing the ivar directly. I'm always going through the databasePath with or without self.
4thSpace
The name “databasePath” refers to two things: a property, and the instance variable (ivar) that is its storage. Using the property means using its accessor methods, either explicitly or with the . operator. To access the ivar directly, either implicitly or with ->, is to bypass the accessors.
Peter Hosey
The setter accessor method (setDatabasePath:) is what releases and retains the value of the property. When you use @synthesize, the compiler synthesizes setDatabasePath: (and its getter counterpart, the databasePath method) for you. And you need to use the setter to get the value retained for free.
Peter Hosey
Incidentally, all of this is covered in the Objective-C Programming Language document: http://developer.apple.com/documentation/Cocoa/Conceptual/ObjectiveC/
Peter Hosey