ios - Get Error "Index out of range" When I frequently push/back viewcontroller in real device - Swift -
i have problem tableview.
when push/back between chatlistviewcontroller , detailviewcontroller, crash , error in listviewcontroller.
don't know data make crash.
guess whether use gcd make happen or not?
have advice avoid it?
thanks.
crash log:
fatal error: index out of range
model:
class chatroomlist:model { var all:[chatroom] { var rooms:[chatroom] = [chatroom]() self.chatrooms.foreach({ (id,chatroom) in if showtype.contains(chatroom.type) { rooms.append(chatroom) } }) return rooms } }
viewcontroller:
import rxcocoa import rxswift import alamofire class listviewcontroller: uiviewcontroller, uitableviewdatasource, uitableviewdelegate { let chatrooms:chatroomlist = chatroomlist() var list:[chatroom] = [chatroom]() var subscribe:disposable? override func viewdidload() { super.viewdidload() self.tableview.datasource = self self.tableview.delegate = self } override func viewwillappear(_ animated: bool) { super.viewwillappear(animated) subscribe = rooms.notifysubject.subscribe({ json in self.loadcontents() }) self.loadcontents() } override func viewwilldisappear(_ animated: bool) { super.viewwilldisappear(animated) subscribe?.dispose() } func loadcontents() { var idlist:[string] = [] self.list.removeall() self.list = chatrooms.all guard self.list.isempty == false else { return } dispatchqueue.global().async() { self.list = self.list.filter { (chatroom) -> bool in if chatroom.id.isempty { return true } if idlist.contains(chatroom.id) { return false } idlist.append(chatroom.id) return true } self.list.sort(by: { (a,b) in if a.message.datetime.isempty { return false } return a.message.datetime > b.message.datetime }) dispatchqueue.main.async() { self.tableview.reloaddata() } } } } func tableview(_ tableview: uitableview, numberofrowsinsection section: int) -> int { return list.count } func tableview(_ tableview: uitableview, cellforrowat indexpath: indexpath) -> uitableviewcell { if list[indexpath.row].type == .city { let cell: chatroomcitytableviewcell = chatroomcitytableviewcell(style: .default, reuseidentifier: nil) cell.loadbycitychatroom(chatroom: list[indexpath.row], cityid: list[indexpath.row].cityid) return cell }else{ let cell: chatroomtableviewcell = chatroomtableviewcell(style: .default, reuseidentifier: nil) cell.loadbychatroom(chatroom: list[indexpath.row]) return cell } }
enable zombie objects in scheme.
select application scheme -> run -> diagnostics -> check zombie objects.
run app now. give more precise information crash.
hope helps :)
Comments
Post a Comment