简体   繁体   中英

Crash by adding data to NSTableView

I am trying to add data from an array built with classes, to an NSScrollView using an NSArrayController

Here is the class and array:

class Serie: NSObject {
    @objc dynamic var theID: String
    @objc dynamic var serieName: String
    @objc dynamic var serieAired: String
    @objc dynamic var serieNetwork: String
    @objc dynamic var serieStatus: String

    init(theID: String, serieName: String, serieAired: String, serieNetwork: String, serieStatus: String ) {
        self.theID = theID
        self.serieName = serieName
        self.serieAired = serieAired
        self.serieNetwork = serieNetwork
        self.serieStatus = serieStatus
    }
}

@objc dynamic var Series: [Serie] = []

Next I bind the NSArrayController to the array of classes "Series" and linking the TableViewCell to each class item.

TableView tree image
Bindings & Outlets of ArrayController image
Bindings & Outlets of TableViewCell image

The next function is where I populate the "dynamic var Series" that binds the content to the NSArrayController and fills the NSScrollView:

func GetSerieID(theSerieName: String){

        let url = URL(string: ("https://api.thetvdb.com/search/series?name=" + theSerieName ))!

        var request = URLRequest(url: url)
        request.httpMethod = "GET"

        request.addValue("application/json", forHTTPHeaderField: "Content-Type")
        request.addValue("application/json", forHTTPHeaderField: "Accept")
        request.setValue( "Bearer \(token)", forHTTPHeaderField: "Authorization")
        //request.setValue( "en", forHTTPHeaderField: "Accept-Language")

        let task = URLSession.shared.dataTask(with: request) { data, response, error in
            guard let data = data, error == nil else {
                print(error?.localizedDescription ?? "No data")
                return
            }

            let json = try? JSON(data: data) // >> I use SwiftyJSON to manage JSON
            let theJSONContent = json!["data"]

            self.Series = [] // >> Empty the array Series

            for (_,subJson):(String, JSON) in theJSONContent {

                let serie = Serie(
                    theID: subJson["id"].stringValue,
                    serieName: subJson["seriesName"].stringValue,
                    serieAired: subJson["firstAired"].stringValue,
                    serieNetwork: subJson["network"].stringValue,
                    serieStatus: subJson["status"].stringValue
                )

                self.Series.append(serie) // >> Here is where it crashes
            }
        }
        task.resume()

        serieTabla.reloadData()
    }

From time to time, the TableView does not display or update the view correctly and loads blank data that shows fine a few seconds if you click on it, but normally it works, until more than 10 results are loaded. Here is the big problem.

It happens randomly, but usually when you exceed 10-20 items.

Thread 1: EXC_BAD_INSTRUCTION (code=EXC_I386_INVOP, subcode=0x0)

The crash log:

miVideoGestor[82269:13639248] *** Assertion failure in -[NSTableRowData _addRowViewForVisibleRow:withPriorView:]


I can't figure out the solution.

Could someone give me a clue on how to solve the problem of data loading and display on the tableView?

Thanks in advance guys

As you are using Cocoa Bindings which is based on Key-Value Observing you have to put any line which affects the UI into a DispatchQueue.main block

DispatchQueue.main.async {
    self.Series = []

    for (_,subJson):(String, JSON) in theJSONContent {

        let serie = Serie(
            theID: subJson["id"].stringValue,
            serieName: subJson["seriesName"].stringValue,
            serieAired: subJson["firstAired"].stringValue,
            serieNetwork: subJson["network"].stringValue,
            serieStatus: subJson["status"].stringValue
        )

        self.Series.append(serie) 
    }
    self.serieTabla.reloadData()
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM