title: swift, removing objects from core data, still appends empty objects?
body: <p>I'm attempting to set up a UITableView, with all objects in an entity.</p>
  
  <p>However, I'm loading the data from an api. So every time it loads, I'm deleting all the objects in the entity. And adding the new ones.</p>
  
  <p>However, when I do this. It's showing 5 cells with the api data, and every time I load it. It adds 5 empty cells.</p>
  
  <p>The reason it's adding empty cells is because I'm defining <code>numberOfRowsInSection</code> with objects count like so:</p>
  
  <pre><code>func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -&gt; Int {
      return stocks.count
  }
  </code></pre>
  
  <p>Where stocks is: <code>var stocks = [NSManagedObject]()</code></p>
  
  <p>So from that I assume it's because there's somehow empty objects in my entity?</p>
  
  <p>here's how I'm trying to set up my data:</p>
  
  <pre><code>    func loadSuggestions(Formula: Int) {
      println("----- Loading Data -----")
      // Check for an internet connection
      if Reachability.isConnectedToNetwork() == false {
          println("ERROR: -&gt; No Internet Connection &lt;-")
      } else {
  
          // Delete all the current objects in the entity
          let fetchRequest = NSFetchRequest(entityName: formulaEntity)
          let a = managedContext.executeFetchRequest(fetchRequest, error: nil) as! [NSManagedObject]
          for mo in a {
              managedContext.deleteObject(mo)
              //println("removed \(mo)")
          }
          // save the context after removing objects.
          managedContext.save(nil)
  
          // Setting up a fetch request to get the api data.
          let entity =  NSEntityDescription.entityForName("\(formulaEntity)", inManagedObjectContext:managedContext)
  
          var request = NSURLRequest(URL: formulaAPI!)
          var data = NSURLConnection.sendSynchronousRequest(request, returningResponse: nil, error: nil)
          var formula = JSON(data: data!)
  
          for (index: String, actionable: JSON) in formula["actionable"] {
              stockName = actionable["name"].stringValue
              ticker = actionable["ticker"].stringValue
              action = actionable["action"].stringValue
              suggestedPrice = actionable["suggested_price"].floatValue
              weight = actionable["percentage_weight"].floatValue
  
              // Set the values in CoreData
              let stock = NSManagedObject(entity: entity!,insertIntoManagedObjectContext:managedContext)
  
              stock.setValue(stockName, forKey: "name")
              stock.setValue(ticker, forKey: "ticker")
              stock.setValue(action, forKey: "action")
              stock.setValue(suggestedPrice, forKey: "suggestedPrice")
              stock.setValue(weight, forKey: "weight")
  
              // Set up second api fetch
              var quoteAPI = NSURL(string: "http://dev.markitondemand.com/Api/v2/Quote/json?symbol=\(ticker)")
  
              var quoteRequest = NSURLRequest(URL: quoteAPI!)
              var quoteData = NSURLConnection.sendSynchronousRequest(quoteRequest, returningResponse: nil, error: nil)
              if quoteData != nil {
                  var quote = JSON(data: quoteData!)
                  betterStockName = quote["Name"].stringValue
                  lastPrice = quote["LastPrice"].floatValue
  
                  // Setting the last values in CoreData
                  if betterStockName != "" {
                      stock.setValue(betterStockName, forKey: "name")
                  }
                  stock.setValue(lastPrice, forKey: "lastPrice")
  
              } else {
                  println("ERROR - NO DATA")
              }
  
              var error: NSError?
              if !managedContext.save(&amp;error) {
                  println("Could not save \(error), \(error?.userInfo)")
              }
              // finally add the stockdata to the CoreData entity.
              stocks.append(stock)
  
  
          }
  
  
  
          // Reload the tableview with the new data.
          self.tableView.reloadData()
      }
  }
  </code></pre>
  
  <p>There isn't a lot of information on how to delete all of the objects in an entity. But I tried to print out the objects it was deleting, and that all seemed correct.</p>
  
  <p>But I must be doing something wrong somewhere in the above function. Since it adds 5 more cells every time the above function is called.</p>
  
  <p>Any help figuring out where the 5 empty cells are coming from and how to fix it would be greatly appreciated!</p>