I want to run saves to my database periodically instead of every time the data is updated. I want to run it sort of like a garbage collection routine. Sleep X seconds, run, sleep X seconds, run.
I tried using repeatInvoking first but it can only run from the main thread. So then I decided to use a coroutine. It works fine as long as I don’t put in the yield statement, but as soon as I add Yield it simply does not run.
in Awake()
savePis = new Thread(DoSaves);
savePis.Start();
void DoSaves()
{
Debug.Log("Calling SaveToDB ");
SaveToDB(); // because you cannot start a thread with an IEnumerator
}
IEnumerator SaveToDB()
{
Debug.Log("STARTING SAVETODB....");
Debug.Log("YEILDING ..... ");
yield return new WaitForSeconds(1); // because invoke repeating only works in the main thread
Debug.Log("FINISHED YEILDING");
}
The Debug.Log never prints Starting SaveToDB. But if I switch the yield statement for a return null, it prints out great.
I’m not calling ANY unity APIs here so why is the yield failing? I thought it might be Debug.Log that was failing, but changing them to print doesn’t help.