title: Strategy to launch a task - and avoid race conditions - from the App Delegate
body: <p>I'm developing a small app on top of core data. At startup, I need to launch a maintenance task - fast but important -. To run this task, the app delegate must open a UIManagedDocument, and perform various checks on it. The app views must not start until the checks are completed. Because opening a UIMD is asynchronous, the app delegate isn't done when the first UIview tries to access the doc. The application then crashes due to a race condition because the app delegate  and the view are trying to open the doc at the same time, while the doc state isn't yet finalised.</p>
  
  <p>I'm using a storyboard, so segues are pretty much in control of the OS... Next time, I'll do it all manually..</p>
  
  <p>What would be your recommendations ?
  Note:
  1)I can not perform the task when the app. goes into background state, because if it is brought back up again, avoiding inconsistent states between the underlying database and what's displayed in the view will be very tedious.
  2)For the same reasons, performing the maintenance task during normal execution is not easily done.
  3) All views access the UIMD via a singleton, according to the code proposed <a href="http://adevelopingstory.com/blog/2012/03/core-data-with-a-single-shared-uimanageddocument.html" rel="nofollow">here</a></p>
  
  <p>Setting a mutex lock in the UIView isn't my preferred route, because the screen remains black - no data -, while displaying the tab bars, until it is released by the app delegate.</p>
  
  <p>Is there a way to have the app delegate wait for a signal before it hands the control over to the UIViews ? In this case, are there any gotchas ? I suspect this is probably not the recommended way to do, and iOS might kill the app if the delegate stays too long waiting for the maintenance task to complete. What would be "too long" in this case?</p>