最新消息:20210816 当前crifan.com域名已被污染,为防止失联,请关注(页面右下角的)公众号

[或许解决]swift的TableView出错:Terminating app due to uncaught exception ‘NSInternalInconsistencyException’, reason: ‘Invalid update: invalid number of rows in section 0

Swift crifan 3603浏览 0评论
2016-02-01 17:25:32.171 JianDao[602:233478] *** Terminating app due to uncaught exception ‘NSInternalInconsistencyException’, reason: ‘Invalid update: invalid number of rows in section 0.  The number of rows contained in an existing section after the update (5) must be equal to the number of rows contained in that section before the update (4), plus or minus the number of rows inserted or deleted from that section (0 inserted, 0 deleted) and plus or minus the number of rows moved into or out of that section (0 moved in, 0 moved out).’

其中可见:

当前已经有5个row了。

但是现在,before update,却只有4个

很是诡异。。。

搜:

ios moveRowAtIndexPath NSInternalInconsistencyException

iphone – UITableView Removing emtpy section after -tableView:moveRowAtIndexPath:ToIndexPath: causes crash – Stack Overflow

ios – moveRowAtIndexPath – Moving cells between sections – Stack Overflow

ios5 – NSInternalInconsistencyException while deleting/inserting rows in UITableView – Stack Overflow

此处,数据也是5个:

(lldb) po self.tableView.numberOfRowsInSection(0)
5
(lldb) po self.conversationItemList.count
5

iOS 9 CoreData NSFetchedResultsController updat… | Apple Developer Forums

moveRowAtIndexPath – Moving cells between sections – ios

后来又遇到了:

JianDao[33872:2149944] *** Terminating app due to uncaught exception ‘NSInternalInconsistencyException’, reason: ‘Invalid update: invalid number of rows in section 0.  The number of rows contained in an existing section after the update (5) must be equal to the number of rows contained in that section before the update (4), plus or minus the number of rows inserted or deleted from that section (0 inserted, 0 deleted) and plus or minus the number of rows moved into or out of that section (0 moved in, 0 moved out).’

经过调试发现:

感觉像是:

多个线程,同时调用了:

updateConversationCell

导致moveRowAtIndexPath出错

-》

所以去加上lock锁,看看结果如何。

    func updateConversationCell(conversationItem:ConversationItem){
        self.updateCellLock.lock()
        dispatch_async(dispatch_get_main_queue(), { () -> Void in
            let conversationItemIdx = self.getConversationIndexFromConversationItem(conversationItem)
            let curIndexPath:NSIndexPath = NSIndexPath(forRow: conversationItemIdx, inSection: 0)
           
            //TODO: after support top most, adjust this top index
            let topRowIdx:Int = 0
            if conversationItemIdx > topRowIdx {
                //move this cell to top
                let newIndexPath:NSIndexPath = NSIndexPath(forRow: topRowIdx, inSection: 0)
               
                self.tableView.beginUpdates()
                self.tableView.moveRowAtIndexPath(curIndexPath, toIndexPath: newIndexPath)
                self.tableView(self.tableView, moveRowAtIndexPath: curIndexPath, toIndexPath: newIndexPath)
                self.tableView.endUpdates()
               
            }
            //here not use reloadRowsAtIndexPaths, just set new value to improve performance
            self.updateConversationCellFromItem(conversationItem)
        })
       
        self.updateCellLock.unlock()
    }

然后好像真的就解决此问题了。。。

实际上还是没有。。。

JianDao[37055:2311465] *** Terminating app due to uncaught exception ‘NSInternalInconsistencyException’, reason: ‘attempt to move index path (<NSIndexPath: 0x7be85b70> {length = 2, path = 0 – 1}) that does not exist – there are only 1 rows in section 0 before the update’

但是:

(lldb) po self.conversationItemList.count
2
(lldb) po self.tableView.numberOfRowsInSection(0)
2

即:

此处不论是数据源还是row,都已经是2个了。。

结果却出现:moveRowAtIndexPath说是row=1不存在。。。

搜:

ios moveRowAtIndexPath Invalid update: invalid number of rows in section   The number of rows contained in an existing section after the update must be equal to the number of rows contained in that section before the update

后来是把两个:

self.tableView.moveRowAtIndexPath(curIndexPath, toIndexPath: newIndexPath)
self.tableView(self.tableView, moveRowAtIndexPath: curIndexPath, toIndexPath: newIndexPath)

调换了次序后:

    //update last publish time and detail text atMe
    func updateConversationCell(conversationItem:ConversationItem){
        dispatch_async(dispatch_get_main_queue(), { () -> Void in
            //here not use reloadRowsAtIndexPaths, just set new value to improve performance
            self.updateConversationCellFromItem(conversationItem)
           
            let conversationItemIdx = self.getConversationIndexFromConversationItem(conversationItem)
            let curIndexPath:NSIndexPath = NSIndexPath(forRow: conversationItemIdx, inSection: 0)
           
            self.updateCellLock.lock()
            //TODO: after support top most, adjust this top index
            let topRowIdx:Int = 0
            if conversationItemIdx > topRowIdx {
                //move this cell to top
                let newIndexPath:NSIndexPath = NSIndexPath(forRow: topRowIdx, inSection: 0)
               
                if let _ = self.tableView.cellForRowAtIndexPath(curIndexPath) {
//                self.tableView.beginUpdates()
                    self.tableView(self.tableView, moveRowAtIndexPath: curIndexPath, toIndexPath: newIndexPath)
                    self.tableView.moveRowAtIndexPath(curIndexPath, toIndexPath: newIndexPath)
//                self.tableView.endUpdates()
                }
            }
            self.updateCellLock.unlock()
        })
    }

 

至少暂时没出现挂掉的问题了。

转载请注明:在路上 » [或许解决]swift的TableView出错:Terminating app due to uncaught exception ‘NSInternalInconsistencyException’, reason: ‘Invalid update: invalid number of rows in section 0

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
84 queries in 0.161 seconds, using 22.40MB memory