当我在viewDidLoad和viewWillAppear之间移动时,我有一个属性变为无效。我不知道为什么会这样。
我有一个帮助程序类,它获取数据库的路径。我在app delegate中分配了这个类。 RootViewController获取对appDelegate的引用,如下所示:
//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];
}
我将鼠标移到appDelegate上以查找数据库助手类和包含数据库路径的属性:
NSPathStore2 * appDelegate.databaseHeper.databasePath "/Users/userA/Library/Application Support/iPhone Simulator/User/Applications..."
databasePath声明为NSString。为什么它改为NSPathStore2?
当我继续调试器时,会发生一些其他奇怪的事情。一旦我进入viewWillAppear,属性值变为“无效”,类型返回NSString。虽然我甚至在那里见过UIButton。
为什么会变为无效?我不能再使用变量了。
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated]; //changes before this line executes
}
我在viewDidLoad和viewWillAppear之间没有做任何事情。以下是在databaseHelper类中分配databasePath的方法,该类在applicationDidFinishLaunching上发生:
- (NSString *) getDBPath {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory , NSUserDomainMask, YES);
NSString *documentsDir = [paths objectAtIndex:0];
return [documentsDir stringByAppendingPathComponent:self.databaseName];
}
stringByAppendingPathComponent返回NSString。为什么我会在分配后的某个时间获得NSStorePath2?
我现在所有工作都没有问题但只是调用[databaseHelper getDBpath]。我的目标是节省一些cpu周期并存储该值。因此databasePath。有关databasePath更改为无效的任何建议吗?
答案 0 :(得分:5)
因为您没有保留该值。查看Memory Management Programming Guide中的规则,了解何时需要保留内容。
当然,正如cdespinosa所说,你可能不需要首先缓存这个路径名。它可能不是一个重要的性能杀手。首先介绍,然后优化热点。
答案 1 :(得分:3)
我相信你的意思是NSPathStore2,而不是NSStorePath。
NSPathStore2位于NSString的内部私有子类中,在子系统中使用,其中字符串已知为文件系统路径。它在功能上等同于您关注的所有方式的NSString。
从您的帖子中不清楚您是否有任何问题,除了想知道该类是什么,以及是否通过缓存获得任何优化。答案可能不是:在访问数据库时使用CPU周期的所有事情中,对文件系统路径的访问可能是列表中的最后一个。