title: Threading issue with unwindFrom
body: <p>High level summary of my issue: </p>
  
  <p>I have a UITableViewController (TVC1) that does a performSegue to invoke a static UITableViewController (TVC2) so the user can enter details. After pressing a button called "Add another" on TVC2, I want TVC2 to save its data and to be invoked again to add another record.</p>
  
  <p>Details: </p>
  
  <p>I have a tableViewController (TVC1) which implements this data model:</p>
  
  <pre><code>- Category &lt;=&gt; maps to tableview sections
    - Group &lt;=&gt; maps to tableview rows
      - Detail &lt;=&gt; maps to tableview rows
  </code></pre>
  
  <p>A display could look like this:</p>
  
  <pre><code>- Section 0: Category 1
    - Row 0: Group A
    - Row 1: Detail x1
    - Row 2: Detail y2
    - Row 3: Detail z3
    - Row 4: Group B
    - Row 5: Detail x2
    - Row 6: Detail y2
    - Row 7: Detail y3
  </code></pre>
  
  <p>When I want to edit a detail by clicking on detail row 5 for example, I do a <code>performSegue</code> in the <code>didSelectRowAt</code>.</p>
  
  <p>In the <code>TVC1.prepareForSegue</code> I provide TVC1 as the delegate for TVC2 where that detail can be added/edited. I also provide the data of the detail to be edited to properties of TVC2.</p>
  
  <p>On TVC2 - after providing the detail - the user can click a save button in the toolbar, which will save the data and take him back to TVC1. Alternatively, at the end of the form where the details can be entered, I have a button:</p>
  
  <ul>
  <li>Add another detail</li>
  </ul>
  
  <p>The idea is that the user is directly taken to a new screen where they can add another detail for the same group. So basically after TVC2 disappears, it should appear again to add new details.</p>
  
  <p>In TVC2, I linked this button to the <code>unwindFrom</code> method on TVC1. Then in the <code>prepareForSegue</code> of TVC2, I call a protocol method on the delegate (TVC1) to provide back the edited details and provide a structure called <code>NextAction</code> to inform the root TVC1 what its next action should be. In this case it should add another detail in the same group.</p>
  
  <p>Then in the <code>unwindFrom</code> method of TVC1 I look at the <code>nextAction</code> and initiate the next <code>performSegue</code>.</p>
  
  <p>I don't know of another place to do this. For me that is the last hook where I can present the next action, which is TVC2 again.</p>
  
  <p>This results in weird behaviour. It appears to work once, but not a second time.</p>
  
  <p>Debugging shows (=====> lines are my own print debug lines):</p>
  
  <pre><code>========&gt; TVC2 prepare start (means the add more details button was pressed)
  ========&gt; TVC1 newDetailValues begin (TVC2 calling the delegate protocol method to provide back edited data - including NextAction)
  ========&gt; TVC1 newDetailValues end
  ========&gt; TVC2 prepare end (I am not sure if TVC2 is now closed/inaccessible)
  ========&gt; TVC1 unwindFromAddEdit begin (here nextAction is evaluated and a performSegue done)
  ========&gt; TVC1 prepare begin (initiated by the performSegue)
  ========&gt; TVC1 prepare end
  'Presenting view controllers on detached view controllers is discouraged &lt;Project.TVC1: 0x.........&gt;.'
  ========&gt; TVC1 unwindFromAddEdit end
  </code></pre>
  
  <p>Note the error/warning:</p>
  
  <pre><code>'Presenting view controllers on detached view controllers is discouraged &lt;Project.TVC1: 0x.........&gt;.'
  </code></pre>
  
  <p>But I do see that detail TVC2. But when I click on the button again, it displays the root TVC1 and not TVC2. xcode now says:</p>
  
  <pre><code>'Warning: Attempt to present &lt;UINavigationController: 0x.........&gt; on &lt;Project.TVC1: 0x.........&gt; whose view is not in the window hierarchy!'
  </code></pre>
  
  <p>I got the feeling this has to to with threading - possibly where the <code>TVC1.unwindFrom</code> is running in the scope of TVC2.</p>
  
  <p>When I edit <code>TVC1.unwindFrom</code> and replace:</p>
  
  <pre><code>performSegue(withIdentifier: "AddDetails", sender: self)
  </code></pre>
  
  <p>... with:</p>
  
  <pre><code>DispatchQueue.main.async {
     performSegue(withIdentifier: "AddDetails", sender: self)
  }
  </code></pre>
  
  <p>Everything works just fine.</p>
  
  <p>But:</p>
  
  <ol>
  <li>I don't understand what is happening. </li>
  <li>I can't assess if what I'm doing is thread safe. For example: Am I introducing a race condition between TVC2 closing and TVC1 calling TVC2 at the same time?</li>
  </ol>
  
  <p>Who can shine some light into what is happening?</p>