title: Index 7 beyond bounds for empty ordered set - Issue with refreshControl
body: <p><strong>EDIT 2: Added my "fixed" code at the bottom of this post.</strong></p>
  
  <p>My custom refresh control is not working for this news app I'm working on.</p>
  
  <p>I am grabbing JSON objects, parsing them into local objects and then displaying them in a table view. I have implemented a custom refresh control, but it does not work.</p>
  
  <p>The program makes an initial request with Alamofire to populate the first ten articles in the "Editorials" section. Then, when I activate the refresh control, it is supposed to make a new request to get the latest content, and then display the new objects in the same table view with reloadData().</p>
  
  <p>I am getting this error because for some reason, once I have deleted all objects from the editorialObjects set (my set with all local objects parsed from the JSON), which I do in handleRefresh, the array does not get repopulated, so when the cellForRowAtIndexPath function asks for the various objects it needs to populate cells (with the "objectAtIndex" function), the app crashes.</p>
  
  <p>I debugged the issue with breakpoints and what seems to be happening is that as soon as it reaches the point where it has to make a new Alamofire request (to refresh the content), it just automatically returns to handleRefresh without iterating through my Alamofire request (and so my objects don't get parsed at all and the editorialObjects array remains empty).</p>
  
  <p>Why is my editorialObjects set not getting populated and why is the Alamofire request getting skipped ?</p>
  
  <p>Code for cellForRowAtIndexPath:</p>
  
  <pre><code>override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -&gt; UITableViewCell {
  
      let row = indexPath.row
  
      let cell = tableView.dequeueReusableCellWithIdentifier(EditorialTableCellIdentifier, forIndexPath: indexPath) as! EditorialsTableViewCell
  
      let title = (editorialObjects.objectAtIndex(indexPath.row) as! EditorialElement).title
  
      let timeStamp = (editorialObjects.objectAtIndex(indexPath.row) as! EditorialElement).timeStamp
      let timeStampDateObject = NSDate(timeIntervalSince1970: NSTimeInterval(Int((editorialObjects.objectAtIndex(indexPath.row) as! EditorialElement).timeStamp)))
      timeStampDateString = dateFormatter.stringFromDate(timeStampDateObject)
  
      let author : String! = (editorialObjects.objectAtIndex(indexPath.row) as! EditorialElement).author!
  
      let issueNumber = (editorialObjects.objectAtIndex(indexPath.row) as! EditorialElement).issueNumber
      let volumeNumber = (editorialObjects.objectAtIndex(indexPath.row) as! EditorialElement).volumeNumber
  
      let articleContent = (editorialObjects.objectAtIndex(indexPath.row) as! EditorialElement).articleContent
  
      let nodeID = (editorialObjects.objectAtIndex(indexPath.row) as! EditorialElement).nodeID
  
      /* Unlike the Pictures Collection View, there is no need to create another Alamofire request here, since we already have all the content we want from the JSON we downloaded. There is no URL that we wish to place a request to to get extra content. */
  
      cell.editorialHeadlineLabel.font = UIFont.preferredFontForTextStyle(UIFontTextStyleHeadline)
      cell.editorialHeadlineLabel.text = title
  
      cell.editorialAuthorLabel.font = UIFont.preferredFontForTextStyle(UIFontTextStyleSubheadline)
      cell.editorialAuthorLabel.text = author
  
      cell.editorialPublishDateLabel.font = UIFont.preferredFontForTextStyle(UIFontTextStyleSubheadline)
      cell.editorialPublishDateLabel.text = timeStampDateString
  
      return cell
  }
  </code></pre>
  
  <p>Code for my "populateEditorials" function which fills the editorialObjects with JSON data:</p>
  
  <pre><code>func populateEditorials() {
      if populatingEditorials {
          return
      }
      populatingEditorials = true
  
      Alamofire.request(GWNetworking.Router.Editorials(self.currentPage)).responseJSON() { response in
          if let JSON = response.result.value {
             /*
          if response.result.error == nil {
              */
  
              /* Creating objects for every single editorial is long running work, so we put that work on a background queue, to keep the app very responsive. */
              dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0)) {
  
  
                  /* Making an array of all the node IDs from the JSON file */
                  var nodeIDArray : [Int]
  
  
                  if (JSON .isKindOfClass(NSDictionary)) {
  
                      for node in JSON as! Dictionary&lt;String, AnyObject&gt; {
  
                      let nodeIDValue = node.0
                      var lastItem : Int = 0
  
                      self.nodeIDArray.addObject(nodeIDValue)
  
                      if let editorialElement : EditorialElement = EditorialElement(title: "init", nodeID: 0, timeStamp: 0, imageURL: "init", author: "init", issueNumber: "init", volumeNumber: "init", articleContent: "init") {
  
                  editorialElement.title = node.1["title"] as! String
                  editorialElement.nodeID = Int(nodeIDValue)
  
                  let timeStampString = node.1["revision_timestamp"] as! String
                  editorialElement.timeStamp = Int(timeStampString)!
  
                  editorialElement.imageURL = String(node.1["image_url"])
                  editorialElement.author = String(node.1["author"])
                  editorialElement.issueNumber = String(node.1["issue_int"])
                  editorialElement.volumeNumber = String(node.1["volume_int"])
                  editorialElement.articleContent = String(node.1["html_content"])
  
                  lastItem = self.editorialObjects.count
  
                  print (editorialElement.nodeID)
  
  
                  self.editorialObjects.addObject(editorialElement)
  
  
                  /* Sorting the elements in order of newest to oldest (as the array index increases] */
                  let timestampSortDescriptor = NSSortDescriptor(key: "timeStamp", ascending: false)
                  self.editorialObjects.sortUsingDescriptors([timestampSortDescriptor])
  
                  let indexPaths = (lastItem..&lt;self.editorialObjects.count).map { NSIndexPath(forItem: $0, inSection: 0) }
  
  
                 /*
  
                          nodeIDArray[nodeCounter] = jsonValue{nodeCounter}.string
                          let editorialInfos : EditorialElement = ((jsonValue as! NSDictionary].1["\(nodeIDArray[nodeCounter]]"] as! [NSDictionary]].map { EditorialElement(title: $0["title"] as! String, nodeID: $0["nid"] as! Int, timeStamp: $0["revision_timestamp"] as! Int, imageURL: $0["image_url"] as! String, author: $0["author"], issueNumber: $0["issue_int"] as! Int, volumeNumber: $0["volume_int"] as! Int, articleContent: $0["html_content"] as! String] // I am going to try to break this line down to simplify it and fix the build errors */
                  }
  
                  print(self.editorialObjects.count)
  
              }
          }
  
                  dispatch_async(dispatch_get_main_queue()) {
                      self.editorialsTableView.reloadData()
                  }
  
                  self.currentPage++
          }
      }
  
          self.populatingEditorials = false
  }
  }
  </code></pre>
  
  <p>And finally, the code for my handleRefresh method:</p>
  
  <pre><code>func handleRefresh() {
  
          customRefreshControl.beginRefreshing()
  
  
          self.editorialObjects.removeAllObjects()
  
          self.currentPage = 1
  
          self.populatingEditorials = false
          populateEditorials()
  
          print(editorialObjects.count)
  
          customRefreshControl.endRefreshing()
  
      }
  </code></pre>
  
  <p>}</p>
  
  <p><strong>EDIT 1 : here is my scrollViewDidEndDecelerating method as well:</strong></p>
  
  <pre><code>override func scrollViewDidEndDecelerating(scrollView: UIScrollView) {
      if customRefreshControl.refreshing {
          if !isAnimating {
              animateRefreshStep1()
              handleRefresh()
          }
      }
  }
  </code></pre>
  
  <p>animateRefreshStep1 is just a custom animation.</p>
  
  <p><strong>EDIT 2: Here is my fixed code</strong></p>
  
  <pre><code>    func handleRefresh() {
  
          customRefreshControl.beginRefreshing()
  
          self.currentPage = 0
  
          self.populatingEditorials = false
          populateEditorials()
  
          print(editorialObjects.count)
  
  
          customRefreshControl.endRefreshing()
  
      }
  </code></pre>