I cant figure out how to run a part of code in ViewDidLoad()
method only once. I don't want that part of the code to be executed until the app is started again. Thanks for any help!
You can use dispatch_once
in your viewDidLoad
:
static dispatch_once_t once;
dispatch_once(&once, ^
{
// Code to run once
});
This will make the code run only once until you exit and close your app.
You can also set up a BOOL flag to check whether the code has executed.
set up the bool above the implementation in the .m file:
static BOOL codeExecuted = NO;
in the -viewDidLoad() method:
if (!codeExecuted)
{
//run code
codeExecuted = YES;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With