ios - __pthread_kill on second request -


i getting json array response particular url
on first request every thing working fine when second request goes application crashes.

libsystem_kernel.dylib`__pthread_kill: 0x1082f885c: movl $0x2000148, %eax 0x1082f8861: movq %rcx, %r10 0x1082f8864: syscall 0x1082f8866: jae 0x1082f8870 ; __pthread_kill + 20 0x1082f8868: movq %rax, %rdi 0x1082f886b: jmp 0x1082f5175 ; cerror_nocancel 0x1082f8870: retq
0x1082f8871: nop
0x1082f8872: nop
0x1082f8873: nop



stacktrace

2015-07-16 13:44:37.126 contact book[15714:106637] * assertion failure in -[uitableview _endcellanimationswithcontext:], /sourcecache/uikit_sim/uikit-3318/uitableview.m:1582 2015-07-16 13:44:37.341 contact book[15714:106637] * terminating app due uncaught exception 'nsinternalinconsistencyexception', reason: 'invalid update: invalid number of rows in section 0. number of rows contained in existing section after update (1) must equal number of rows contained in section before update (11), plus or minus number of rows inserted or deleted section (1 inserted, 0 deleted) , plus or minus number of rows moved or out of section (0 moved in, 0 moved out).' *** first throw call stack: ( 0 corefoundation 0x0000000105cd53f5 exceptionpreprocess + 165 1 libobjc.a.dylib 0x0000000107803bb7 objc_exception_throw + 45 2 corefoundation 0x0000000105cd525a +[nsexception raise:format:arguments:] + 106 3 foundation 0x000000010616c28f -[nsassertionhandler handlefailureinmethod:object:file:linenumber:description:] + 195 4 uikit 0x000000010663346a -[uitableview _endcellanimationswithcontext:] + 11746 5 contact book 0x0000000105ae6a11 _tffc12contact_book20masterviewcontroller38updatesearchresultsforsearchcontrollerfs0_fcso18uisearchcontrollert_u_ftgsqcso6nsdata_gsqcso13nsurlresponse_gsqcso7nserror__t_ + 2625 6 contact book 0x0000000105ae6bb3 _ttrxfo_ogsqcso6nsdata_ogsqcso13nsurlresponse_ogsqcso7nserror__dt__xfo_itgsqs__gsqs0__gsqs1____it + 51 7 contact book 0x0000000105ae4091 _tpa__ttrxfo_ogsqcso6nsdata_ogsqcso13nsurlresponse_ogsqcso7nserror__dt__xfo_itgsqs__gsqs0__gsqs1____it__ + 81 8 contact book 0x0000000105ae6e83 _ttrxfo_itgsqcso6nsdata_gsqcso13nsurlresponse_gsqcso7nserror___it__xfo_ogsqs__ogsqs0__ogsqs1___dt__ + 35 9 contact book 0x0000000105ae6f0f _ttrxfo_ogsqcso6nsdata_ogsqcso13nsurlresponse_ogsqcso7nserror__dt__xfdcb_dgsqs__dgsqs0__dgsqs1___dt__ + 127 10 cfnetwork 0x0000000108aaebbb 49-[__nscflocalsessiontask _task_onqueue_didfinish]_block_invoke + 157 11 foundation 0x000000010618fccf __nsblockoperation_is_calling_out_to_a_block + 7 12 foundation 0x00000001060cfb32 -[nsblockoperation main] + 98 13 foundation 0x00000001060b2104 -[__nsoperationinternal _start:] + 645 14 foundation 0x00000001060b1d13 __nsoqschedule_f + 184 15 libdispatch.dylib 0x0000000107fa87f4 _dispatch_client_callout + 8 16 libdispatch.dylib 0x0000000107f90bf4 _dispatch_queue_drain + 1417 17 libdispatch.dylib 0x0000000107f90506 _dispatch_queue_invoke + 235 18 libdispatch.dylib 0x0000000107f92ff7 _dispatch_root_queue_drain + 682 19 libdispatch.dylib 0x0000000107f9463c _dispatch_worker_thread2 + 52 20 libsystem_pthread.dylib 0x000000010833eef8 _pthread_wqthread + 314 21 libsystem_pthread.dylib 0x0000000108341fb9 start_wqthread + 13 ) libc++abi.dylib: terminating uncaught exception of type nsexception (lldb)

i have following code

import uikit  class masterviewcontroller: uitableviewcontroller,     uisearchcontrollerdelegate, uisearchresultsupdating, uisearchbardelegate  {  var completedata = nsmutablearray() var namedata = nsmutablearray()   override func awakefromnib() {     super.awakefromnib() }   var searchcontroller : uisearchcontroller!  override func viewdidload() {     super.viewdidload()      self.searchcontroller = uisearchcontroller(searchresultscontroller:  nil)      self.searchcontroller.searchresultsupdater = self     self.searchcontroller.delegate = self     self.searchcontroller.searchbar.delegate = self      self.searchcontroller.hidesnavigationbarduringpresentation = false     self.searchcontroller.dimsbackgroundduringpresentation = true     self.navigationitem.titleview = searchcontroller.searchbar      self.definespresentationcontext = true }     var flag = false     var task: nsurlsessiontask!  func updatesearchresultsforsearchcontroller(searchcontroller: uisearchcontroller) {     var inputtext = searchcontroller.searchbar.text nsstring      if inputtext.length > 3 {         //todo repalce space %20          if flag{             task.cancel()}          let url = nsurl(string: "http://172.26.1.39:8080/contactbook/search?term="+inputtext);          task = nsurlsession.sharedsession().datataskwithurl(url){(data, response, error) in              if(error != nil){                 println("some error")                 return             }              var parseerror: nserror?              var jsonarray = nsjsonserialization.jsonobjectwithdata(data, options: nsjsonreadingoptions.allowfragments, error: &parseerror) nsarray              if parseerror != nil {                 println("something wrong")                 return             }               self.completedata.removeallobjects()             self.namedata.removeallobjects()              var tempobject : nsdictionary;              var i=0; < jsonarray.count; i++ {                 var jsondict = jsonarray[i] nsdictionary                 self.completedata.insertobject(jsondict, atindex: 0)                 var name = jsondict.valueforkey("name")! nsstring                 self.namedata.insertobject(name, atindex: 0)                 let indexpath = nsindexpath(forrow: 0, insection: 0)                 self.tableview.insertrowsatindexpaths([indexpath], withrowanimation: .fade)             }         }         flag = true         task.resume()     } }   override func didreceivememorywarning() {     super.didreceivememorywarning()     // dispose of resources can recreated. }  // mark: - segues  override func prepareforsegue(segue: uistoryboardsegue, sender: anyobject?) {     if segue.identifier == "showdetail" {         if let indexpath = self.tableview.indexpathforselectedrow() {             let object = completedata[indexpath.row] nsdictionary             (segue.destinationviewcontroller detailviewcontroller).detailitem = object         }     } }  // mark: - table view  override func numberofsectionsintableview(tableview: uitableview) -> int {     return 1 }  override func tableview(tableview: uitableview, numberofrowsinsection section: int) -> int {     return namedata.count }  override func tableview(tableview: uitableview, cellforrowatindexpath indexpath: nsindexpath) -> uitableviewcell {     let cell = tableview.dequeuereusablecellwithidentifier("cell", forindexpath: indexpath) uitableviewcell     let object = namedata[indexpath.row] nsstring     cell.textlabel?.text = object.description     return cell }  override func tableview(tableview: uitableview, caneditrowatindexpath indexpath: nsindexpath) -> bool {     // return false if not want specified item editable.     return false } } 

it's pretty clear error message, read it. quote:

terminating app due uncaught exception 'nsinternalinconsistencyexception', reason: 'invalid update: invalid number of rows in section 0. number of rows contained in existing section after update (1) must equal number of rows contained in section before update (11), plus or minus number of rows inserted or deleted section (1 inserted, 0 deleted) , plus or minus number of rows moved or out of section (0 moved in, 0 moved out).

it says:

  • after update (insert/delete) you've got 1 cell in section 0,
  • but before did update table view, had 11 cells there, did insert 1 cell, expects 11+1 cells, not 1

problem number of cells in section 0 equals namedata.count. , code (removed unrelated parts) ...

self.namedata.removeallobjects()   <----- (delete)  var tempobject : nsdictionary;  var i=0; < jsonarray.count; i++ {   var jsondict = jsonarray[i] nsdictionary   var name = jsondict.valueforkey("name")! nsstring   self.namedata.insertobject(name, atindex: 0) <----- (insert)   let indexpath = nsindexpath(forrow: 0, insection: 0)   self.tableview.insertrowsatindexpaths([indexpath],     withrowanimation: .fade)  <------ (insert) } 

... removes objects self.namedata, not table view.

table view cells , backing store not synced.

reload data

remove self.tableview.insert... line , place self.tableview.reloaddata() after loop. reloads whole table view.

self.namedata.removeallobjects()  var tempobject : nsdictionary;  var i=0; < jsonarray.count; i++ {   var jsondict = jsonarray[i] nsdictionary   var name = jsondict.valueforkey("name")! nsstring   self.namedata.insertobject(name, atindex: 0)   let indexpath = nsindexpath(forrow: 0, insection: 0) } self.tableview.reloaddata() 

or reload 1 section.

func reloadsections(_ sections: nsindexset) 

delete items & insert

if not want reload cells (whole table view), must keep in mind backing store data must kept in sync table view. in other words - if remove cell table view, must remove backing store. if insert cell table view, must insert backing store. , vice versa.

in case, call self.namedata.removeallobjects(), didn't delete table view cells these objects. should call ...

func deleteitemsatindexpaths(_ indexpaths: [nsindexpath]) 

... on self.tableview objects did remove self.namedata.

if lot of deleteitems.../insertitems/... calls, it's wrap them in performbatchupdates(_:completion:):

func performbatchupdates(_ updates: (() -> void)?,   completion completion: ((bool) -> void)?) 

animates multiple insert, delete, reload, , move operations group.


Comments