I have a tab-based application and want to reload data each time I switch between the tabs. So I used viewWillAppear:
which gets called but the data doesn't reload. Could this be a result of using lots of [NSUserDefault]
's?
Here is my code:
-(void)viewWillAppear:(BOOL)animated{
[super viewWillAppear:animated];
[self reloadData];
}
-(void)reloadData {
//Loading all the data
totalApples = [[NSUserDefaults standardUserDefaults]integerForKey:@"totalApples"];
[self setText:[NSString stringWithFormat:@"%.01f", totalApples] withExistingAttributesInLabel:self.applesLabel];
applesPerSecond = [[NSUserDefaults standardUserDefaults]integerForKey:@"applesPerSecond"];
[self setText:[NSString stringWithFormat:@"%.01f", applesPerSecond] withExistingAttributesInLabel:self.applesPerSecondLabel];
applesPerClick = [[NSUserDefaults standardUserDefaults]integerForKey:@"applesPerClick"];
}
When using NSUserDefaults
it is a good idea to call -synchronize
after writing the data.
Take the following example:
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:@100 forKey:@"SomeKey"];
[defaults synchronize]; //This will ensure data is written.
If you do not call this method the system periodically does. However it can lead the timing issues like you are probably experiencing.
Read more about NSUserDefaults
and this behavior here.
Update
This question is losing its context now I think. Here is an updated method that you posted below in the comments. It might be worth posting a follow up question since this is probably not an issue with NSUserDefaults
.
Here at least for posterity is an example of using NSUserDefaults
to write out data. I also updated your method to save priceFarmHands as a float because of the multiplication being performed. (It was being written out as an integer before and probably being truncated).
- (void)buyFarmhand {
farmHands++;
totalApples -= priceFarmHands;
if(priceFarmHands < 1000) {
priceFarmHands = priceFarmHands * 1.35;
} else if(priceFarmHands < 10000) {
priceFarmHands = priceFarmHands * 1.25;
} else {
priceFarmHands = priceFarmHands * 1.15;
}
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setFloat:priceFarmHands forKey:@"priceFarmHands"];
[defaults setInteger:farmHands forKey:@"farmHands"];
[defaults setDouble:totalApples forKey:@"totalApples"];
[defaults synchronize];
[self setText:[NSString stringWithFormat:@"%.f", priceFarmHands] withExistingAttributesInLabel:self priceFarmHandsLabel];
[self setText:[NSString stringWithFormat:@"%d", farmHands] withExistingAttributesInLabel:self.numberFarmhands];
}
I don't see where you create the NSUserdefault object, It should be created in the reloadData method, this should work:
-(void)reloadData {
//Loading all the data
NSUserDefaults *appleData = [NSUserDefaults standardUserDefaults];
totalApples = [appleData integerForKey:@"totalApples"];
applesPerSecond = [appleData integerForKey:@"applesPerSecond"];
applesPerClick = [integerForKey:@"applesPerClick"];
//Set the labels text
[self setText:[NSString stringWithFormat:@"%.01f", totalApples]
withExistingAttributesInLabel:self.applesLabel];
[self setText:[NSString stringWithFormat:@"%.01f", applesPerSecond]
withExistingAttributesInLabel:self.applesPerSecondLabel];
}
Also to help you troubleshoot this add some log to viewWillAppear
:
-(void)viewWillAppear:(BOOL)animated{
NSLog(@"viewWillAppear called, reloadind data...");
[super viewWillAppear:animated];
[self reloadData];
}