不确定如何修复NSInconsistencyException

时间:2016-09-08 21:17:39

标签: ios swift uitableview

我正在处理的应用程序旨在显示一个狗列表,并指出它们目前是否在建筑物中(对于狗日托公司而言)。用户通过搜索来添加狗。该应用程序从数据库中获取这些狗。

我有一点工作。但是我在后台线程上运行自动布局时遇到错误。我用Google搜索并找到了这一行:dispatch_async(dispatch_get_main_queue(), {})修复了错误。

问题是我现在有NSInconsistencyException

导致错误的函数:

 func searchFor(){
    //print(searchBar.text)
    let url = NSURL(string: "http://192.168.0.8/classes/main.php?fn=dogSearch&s=" + searchBar.text!)
    let task = NSURLSession.sharedSession().dataTaskWithURL(url!) {(data, response, error) in
        print(NSString(data: data!, encoding: NSUTF8StringEncoding))
        //var boardings = [String]()
            self.dogs.removeAll()
        do {
            let json = try NSJSONSerialization.JSONObjectWithData(data!, options: .AllowFragments) as! [AnyObject]

            if let theDogs = json as? [[String: AnyObject]] {
                for dog in theDogs {
                    if let ID = dog["ID"] as? String {
                        print(ID + " Safe")
                        let thisDog = Dog(name: (dog["Name"] as? String)!, surname: (dog["Surname"] as? String)!, id: (dog["ID"]  as? String)!, boarding: false)
                        let newIndexPath = NSIndexPath(forRow: self.dogs.count, inSection: 0)
                        self.dogs.append(thisDog)
                        dispatch_async(dispatch_get_main_queue(), {
                            // code here
                        self.tableView.insertRowsAtIndexPaths([newIndexPath], withRowAnimation: .Bottom)
                        })
                    }
                }
            }
        } catch {
            print("error serializing JSON: \(error)")
        }

        // print(names) // ["Bloxus test", "Manila Test"]

    }

    task.resume()
}

错误:

  

***断言失败 - [UITableView _endCellAnimationsWithContext:],    /BuildRoot/Library/Caches/com.apple.xbs/Sources/UIKit_Sim/UIKit-   3512.60.7 / UITableView.m:1716
  2016-09-08 21:56:27.559日间护理注册[1961:654300]   ***由于未捕获的异常而终止应用程序   ' NSInternalInconsistencyException',原因:'无效更新:无效   第0节中的行数。包含在中的行数   更新后的现有部分(3)必须等于数量   在更新(0)之前包含在该部分中的行,加上或减去   从该部分插入或删除的行数(插入1个,0   删除)并加上或减去移入或移出的行数   部分(0移入,0移出)。'

如果我理解正确,它告诉我该功能是将Dog更快地添加到数组中,而不是为表创建行?

1 个答案:

答案 0 :(得分:1)

如果您提供给它的数组与您更新它的数量不相同,UITableView将抛出异常。

为防止这种情况发生,请将您的代码包含在beginUpdates()endUpdates()来电中,这样当您将狗附加到阵列后,您还会将所有行添加到同一批次中的表格。

尝试将整个操作放在主队列上并将其包装在这些更新调用中,如下所示:

dispatch_async(dispatch_get_main_queue(), {
    self.tableView.beginUpdates()
    if let theDogs = json as? [[String: AnyObject]] {
        for dog in theDogs {
            if let ID = dog["ID"] as? String {
                print(ID + " Safe")
                let thisDog = Dog(name: (dog["Name"] as? String)!, surname: (dog["Surname"] as? String)!, id: (dog["ID"]  as? String)!, boarding: false)
                let newIndexPath = NSIndexPath(forRow: self.dogs.count, inSection: 0)
                self.dogs.append(thisDog)


                self.tableView.insertRowsAtIndexPaths([newIndexPath], withRowAnimation: .Bottom)
            }
        }
    }
    self.tableView.endUpdates()
})