my question is: in which function of a view i must execute a code and be sure all the work of the preceding view has be done? all data saved and whatsoever else?
This is my situation, making me asking this question:
I’ve three view
1) Departure island
2) Arrival Island
3) Map
On the Departure Island you can choose from which island to depart, on the arrival to which you want to arrive, on the Map, you can see visually the route you have to follow in order to reach your destination.
Programmatically, i have the first two view that are two table view (list of string of island name) and the third is a simple image view having a system that draw on it the route. I save data to a simple sql database but i’ve had the same issue saving data more simply using NSUserDefaults.
The two table view save the island choosen at their
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *islandChoosen= [departureIsland objectAtIndex:indexPath.row];
[[IslandDatabase database] saveOneRecord:@"1" name:arrivalIslandChoosen];
}
and in the class IslandDatabase
- (void) saveOneRecord:(NSString *)id name:(NSString *)name {
NSLog(@"Saving data Partenza");
sqlite3_stmt *statementINSERT;
NSString somedataToInsertContainingNameofTheIsland;
NSString *insertSQL = [NSString stringWithFormat: @"INSERT INTO aTable somedataToInsertContainingNameofTheIsland";
NSLog(@"%@", insertSQL);
sqlite3_prepare_v2(database,[insertSQL UTF8String], -1, &statementINSERT, NULL);
sqlite3_step(statementINSERT);
sqlite3_finalize(statementINSERT);
}
*Omitting the code of the real query for the sake of simplicity
Now, when i choose the second island i proceed to the map and on the viewDidLoad of the Map i try to get the data of the two island choosen in order to construct my route.
And here it is the problem. I can get the data of the first island choosen but not of the second and it is a matter of time.
I’ve done a lot of tests and it seem that the save of the data i made in the didSelectRowAtIndexPath of the second view (arrival island) actually finish after the viewDidLoad of the third view (map) not giving me the time to get data when i need them for i execute code drawing the route in the viewDidLoad. The NsLog i call at the time of the real writing of the data on the db happen to show after the map view had been designed and launched.
I tried to do a while at the beginning of the third view viewDidLoad which continued till the data of the second island was available but i had no fortune.. it only seem to be stucked before designing the third view…
You should you try loading your data in the third view using
- (void)viewWillAppear:(BOOL)animatedinstead of- (void)viewDidLoad?The
viewDidLoadmethod is called when the controller has loaded its hierarchy into memory, which happens eagerly to optimize performance.