title: Swift nested functions vs Closure Variables
body: <p>I want to make a function that fetches a record from CloudKit, if it encounters a temporary network error the function should retry.</p>
  
  <pre><code>func fetchRecord(withRecordID recordID: CKRecordID, returnBlock: (optError: ErrorType?) -&gt; Void){
  
      func internalReturnBlock(optError optError: ErrorType?){
          if NSThread.isMainThread() {
              returnBlock(optError: optError)
          }
          else{
              dispatch_async(dispatch_get_main_queue(), { 
                  returnBlock(optError: optError)
              })
          }
      }
  
      func internalWork(){
          privateDB.fetchRecordWithID(recordID) { (optRecord, optError) in
              if let error = optError{
                  // IF NETWORK ERROR RETRY
                  internalWork()
              }
              else{
                  internalReturnBlock(optError: nil)
              }
          }
      }
  
      internalWork()
  }
  </code></pre>
  
  <p>Here I define such function (simplified), If the fetch encounters an error it retries by calling the nested function internalWork()</p>
  
  <p>My question is what would be the difference between using nested functions or creating local closure variables?
  For example, here I change the internalReturnBlock to a closure variable:</p>
  
  <pre><code>func fetchRecord2(withRecordID recordID: CKRecordID, returnBlock: (optError: ErrorType?) -&gt; Void){
  
      var internalReturnBlock = { (optError: NSError?) in
          if NSThread.isMainThread() {
              returnBlock(optError: optError)
          }
          else{
              dispatch_async(dispatch_get_main_queue(), {
                  returnBlock(optError: optError)
              })
          }
      }
  
      func internalWork(){
          privateDB.fetchRecordWithID(recordID) { (optRecord, optError) in
              if let error = optError{
                  // IF NETWORK ERROR RETRY
                  internalWork()
              }
              else{
                  internalReturnBlock(nil)
              }
          }
      }
  
  
      internalWork()
  }
  </code></pre>
  
  <p>What are the differences between using a nested function vs a variable block?
  Any advantages or problems?</p>