I ran into a problem tonight. Basically I have 3 classes which 2 are viewControllers and one NSObject. My NSObject Class is called "BoilerPlateCode", it contains methods that I call often from different classes like accessing the Documents folder.
The problem that I have is viewController1 is pushing viewController2 on to the stack before it finishes saving. In viewController1's method viewWillDisappear: I have it call the BoilerPlateCode class to save the data to the Documents folder.
When viewController2 instantiates the viewDidLoad method is called. It then retrieves some of the saved data using the BoilerPlateCode to display on the screen.
The problem that I am having is that viewController2 is being pushed on the stack before VC1 has finished writing the data. The result is that the old data is loaded instead.
I tested this with 2 NSLogs, one writing out to the screen "Saved" from VC1 and the other writing out "Loaded" from VC2.
VC1
VC2
On the console I should see 'saved' first then 'loaded', but it is the opposite. So I am thinking that when I call the BoilerPlateCode object to perform the save task, it is performing this task as an asynchronous operation. This allows VC1 to push VC2 on the stack and start to load the data before it finishes saving it.
If this is the case is there a way to make it perform the save on the main thread so it has to wait till the save is finished before pushing VC2 on the stack and calling the load information?
The problem that I have is viewController1 is pushing viewController2 on to the stack before it finishes saving. In viewController1's method viewWillDisappear: I have it call the BoilerPlateCode class to save the data to the Documents folder.
When viewController2 instantiates the viewDidLoad method is called. It then retrieves some of the saved data using the BoilerPlateCode to display on the screen.
The problem that I am having is that viewController2 is being pushed on the stack before VC1 has finished writing the data. The result is that the old data is loaded instead.
I tested this with 2 NSLogs, one writing out to the screen "Saved" from VC1 and the other writing out "Loaded" from VC2.
VC1
Code:
-(void)viewWillDisappear:(BOOL)animated{
if (shouldSaveTextView || ![currentXP isEqualToString:savedXP]) {
NSString *textFromView = notesField.text;
[characterLoadInfo setValue:textFromView forKey:@"textView"];
[characterLoadInfo setValue:currentXP forKey:@"savedXP"];
[COLOR="Red"] BoilerPlateCode *bpc = [[BoilerPlateCode alloc] init];
[bpc saveAllCharacter:characterName with:characterLoadInfo];
NSLog(@"saved");[/COLOR]
}
}
VC2
Code:
- (void)viewDidLoad{
BoilerPlateCode *bpc = [[BoilerPlateCode alloc] init];
NSDictionary *charDict2 = [bpc reloadCharacter:characterName];
maxHP = [[charDict2 objectForKey:@"hitPoints"]intValue];
NSString *hp = nil;
if ([charDict2 objectForKey:@"currentHP"]) {
hp = [charDict2 objectForKey:@"currentHP"]; // If HP hs been modified use this
}
else if ([charDict2 objectForKey:@"hitPoints"]){ // If no combat has happened yet then use this
hp = [charDict2 objectForKey:@"hitPoints"];
}
else
hp = @"0";
[COLOR="Red"]NSLog(@"loaded");[/COLOR]
}
2013-08-26 20:44:12.721 XP Tracker[7691:15503] loaded
2013-08-26 20:44:12.729 XP Tracker[7691:15503] saved
On the console I should see 'saved' first then 'loaded', but it is the opposite. So I am thinking that when I call the BoilerPlateCode object to perform the save task, it is performing this task as an asynchronous operation. This allows VC1 to push VC2 on the stack and start to load the data before it finishes saving it.
If this is the case is there a way to make it perform the save on the main thread so it has to wait till the save is finished before pushing VC2 on the stack and calling the load information?