So I’ve been trying to get a variable in my class (TrackViewController) initialize just once.
This is done to keep the inserted id from the database known to the viewcontroller.
In short:
I have a TrackManager which receives a location update through a NSNotificationCenter.
The TrackViewController uses the startTracking method when a button is clicked.
The function is set in my TrackManager and returns the id of the newly inserted row in the database.
This id should be kept by the TrackViewController so that when I go back to my main screen of my application and then get back to the TrackViewController it still knows this id. This is needed to call the method stopTrack:(long)trackID to stop tracking locations for that track and set the database column “TrackFinished” to finished.
I thought to solve this using static variables but I’d rather not.
Is there a way to initialize a variable just once in TrackViewController.
I’ve tried awakeFromNib, ViewDidLoad, and such but those methods are called every time I get to the TrackViewController screen.
Many thanks in advance
Try putting something like this in your viewDidLoad method, or initMethod. Of course replacing NSObject with whatever type your variable is.
Another approach is to check whether the variable is already allocated: