<html><head><meta http-equiv="Content-Type" content="text/html charset=utf-8"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class="">On 3 Oct 2016, at 5:46 PM, jason bronson via swift-users &lt;<a href="mailto:swift-users@swift.org" class="">swift-users@swift.org</a>&gt; wrote:<br class=""><div><blockquote type="cite" class=""><br class="Apple-interchange-newline"><div class=""><div dir="ltr" class="">I have this class I wrote which stores the error messages from the firebaseauth if a error occurs.<div class="">The problem is that the variable on first return is not set and on second return is.</div><div class="">variable _errorMsg is empty on first return of method registerUser</div><div class=""><br class=""></div><div class="">Why is it not storing the variable when it's initially triggered?</div></div></div></blockquote></div><br class=""><div class="">I assume you are talking about this function, which I’ll elide for purposes of discussion:</div><div class=""><br class=""></div><div class="">===</div><div class=""><div class=""><font face="Menlo" class=""><span style="font-size: 11px;" class="">func registerUser(email : String, password: String) -&gt; Bool{</span></font></div><div class=""><font face="Menlo" class=""><span style="font-size: 11px;" class="">&nbsp; &nbsp; createdUser = false</span></font></div><div class=""><font face="Menlo" class=""><span style="font-size: 11px;" class="">&nbsp; &nbsp;&nbsp;</span></font></div><div class=""><font face="Menlo" class=""><span style="font-size: 11px;" class="">&nbsp; &nbsp; FIRAuth.auth()?.createUser(withEmail: email, password: password, completion: {(user, error) in</span></font></div><div class=""><font face="Menlo" class=""><span style="font-size: 11px;" class="">&nbsp; &nbsp; &nbsp; &nbsp; if error != nil {</span></font></div><div class=""><font face="Menlo" class=""><span style="font-size: 11px;" class="">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // ...</span></font></div><div class=""><font face="Menlo" class=""><span style="font-size: 11px;" class="">&nbsp; &nbsp; &nbsp; &nbsp; }else{</span></font></div><div class=""><font face="Menlo" class=""><span style="font-size: 11px;" class="">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; print("DEBUG: New user created ")</span></font></div><div class=""><font face="Menlo" class=""><span style="font-size: 11px;" class="">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; self.createdUser = true</span></font></div><div class=""><font face="Menlo" class=""><span style="font-size: 11px;" class="">&nbsp; &nbsp; &nbsp; &nbsp; }</span></font></div><div class=""><font face="Menlo" class=""><span style="font-size: 11px;" class="">&nbsp; &nbsp; })</span></font></div><div class=""><font face="Menlo" class=""><span style="font-size: 11px;" class="">&nbsp; &nbsp;&nbsp;</span></font></div><div class=""><font face="Menlo" class=""><span style="font-size: 11px;" class="">&nbsp; &nbsp; return self.createdUser</span></font></div><div class=""><font face="Menlo" class=""><span style="font-size: 11px;" class="">}</span></font></div></div><div class="">===</div><div class=""><br class=""></div><div class="">The <font face="Menlo" class=""><span style="font-size: 11px;" class="">error</span></font> parameter to the closure; the nature of the task, which must surely involve turnaround from another process or host; and the compiler error telling you (I’m betting) to refer explicitly to <font face="Menlo" class=""><span style="font-size: 11px;" class="">self</span></font> in the closure; are a tipoff that the closure&nbsp;escapes. Escaping closures do not return to the code that presented them (or they don’t promise to; see below). <font face="Menlo" class=""><span style="font-size: 11px;" class="">createUser(withEmail:password:completion:)</span></font> merely registers your completion (result) handler; it does not execute it; it waits in the background until the remote process responds.&nbsp;<span style="font-family: Menlo; font-size: 11px;" class="">registerUser(...</span>&nbsp;proceeds while&nbsp;<span style="font-family: Menlo; font-size: 11px;" class="">createUser(...</span>&nbsp;waits.</div><div class=""><br class=""></div><div class="">Upon the first call with that email/password combination., none of the code in your closure — including the part that sets&nbsp;<font face="Menlo" class=""><span style="font-size: 11px;" class="">self.createdUser</span></font>&nbsp;to <font face="Menlo" class=""><span style="font-size: 11px;" class="">true</span></font> — will have run by the time you&nbsp;<span style="font-family: Menlo; font-size: 11px;" class="">return self.createdUser</span>. By the end of <font face="Menlo" class=""><span style="font-size: 11px;" class="">registerUser(...</span></font>, <font face="Menlo" class=""><span style="font-size: 11px;" class="">createdUser</span></font> is still <font face="Menlo" class=""><span style="font-size: 11px;" class="">false</span></font>.</div><div class=""><br class=""></div><div class="">---</div><div class=""><br class=""></div><div class="">As for the second return I’m guessing something like this: <font face="Menlo" class=""><span style="font-size: 11px;" class="">createUser(...</span></font> caches the success of the last time it executed with that email/password pair. It sees there’s no need for a round trip through the external process, so it executes the completion closure immediately, with <font face="Menlo" class=""><span style="font-size: 11px;" class="">error == nil</span></font>. The closure sets <font face="Menlo" class=""><span style="font-size: 11px;" class="">self.createdUser = true</span></font> before it&nbsp;returns to your function. Your function returns the&nbsp;<font face="Menlo" class=""><span style="font-size: 11px;" class="">true</span></font>.</div><div class=""><br class=""></div><div class=""><span class="Apple-tab-span" style="white-space:pre">        </span>— F</div><div class=""><br class=""></div></body></html>