ios - swift, removing objects from core data, still appends empty objects? -


i'm attempting set uitableview, objects in entity.

however, i'm loading data api. every time loads, i'm deleting objects in entity. , adding new ones.

however, when this. it's showing 5 cells api data, , every time load it. adds 5 empty cells.

the reason it's adding empty cells because i'm defining numberofrowsinsection objects count so:

func tableview(tableview: uitableview, numberofrowsinsection section: int) -> int {     return stocks.count } 

where stocks is: var stocks = [nsmanagedobject]()

so assume it's because there's somehow empty objects in entity?

here's how i'm trying set data:

    func loadsuggestions(formula: int) {     println("----- loading data -----")     // check internet connection     if reachability.isconnectedtonetwork() == false {         println("error: -> no internet connection <-")     } else {          // delete current objects in entity         let fetchrequest = nsfetchrequest(entityname: formulaentity)         let = managedcontext.executefetchrequest(fetchrequest, error: nil) as! [nsmanagedobject]         mo in {             managedcontext.deleteobject(mo)             //println("removed \(mo)")         }         // save context after removing objects.         managedcontext.save(nil)          // setting fetch request 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!)          (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 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 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 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(&error) {                 println("could not save \(error), \(error?.userinfo)")             }             // add stockdata coredata entity.             stocks.append(stock)           }            // reload tableview new data.         self.tableview.reloaddata()     } } 

there isn't lot of information on how delete of objects in entity. tried print out objects deleting, , seemed correct.

but must doing wrong somewhere in above function. since adds 5 more cells every time above function called.

any figuring out 5 empty cells coming , how fix appreciated!

the simple reason ever add new entries stocks, never remove anything. if never remove array, never smaller.

you delete objects core data, doesn't automatically remove them array. don't disappear memory , array because core data doesn't have them anymore. need separately calling removeall on array after deleting objects core data.


Comments

Popular posts from this blog

css - SVG using textPath a symbol not rendering in Firefox -

Java 8 + Maven Javadoc plugin: Error fetching URL -

order - Notification for user in user account opencart -