ios - Why is my table view updating so slowly? Is this normal? -
i created app stores playlists in core data. use table view controller display playlists. users can add , delete playlists in table view.
the delete button works fine. added add button right side of navigation bar. when button pressed, alert shows , asks user name of playlist. if user enters nothing, error shown alert. if user input valid, playlist added table view.
here relevant code:
import uikit import coredata class playlistcontroller: uitableviewcontroller { var playlists: [playlists] = [] let datacontext: nsmanagedobjectcontext! = (uiapplication.sharedapplication().delegate as? appdelegate)?.managedobjectcontext override func viewdidload() { if datacontext != nil { let entity = nsentitydescription.entityforname("playlist", inmanagedobjectcontext: datacontext) let request = nsfetchrequest() request.entity = entity let playlists = try? datacontext.executefetchrequest(request) if playlists != nil { item in playlists! { self.playlists.append(item as! playlists) } } } } //this called when user clicks add button @ibaction func addplaylist(sender: uibarbuttonitem) { let alert = uialertcontroller(title: "新播放列表", message: "请输入播放列表的名字", preferredstyle: .alert) alert.addtextfieldwithconfigurationhandler({ (textfield) -> void in textfield.placeholder = "名字" }) alert.addaction(uialertaction(title: "确定", style: uialertactionstyle.default, handler: { (action) -> void in if alert.textfields?.first?.text == "" || alert.textfields?.first?.text == nil { let failalert = uialertcontroller(title: "失败", message: "播放列表名不能为空", preferredstyle: .alert) failalert.addaction(uialertaction(title: "确定", style: .default, handler: nil)) self.presentviewcontroller(failalert, animated: true, completion: nil) return } let newplaylist = playlists(entity: nsentitydescription.entityforname("playlist", inmanagedobjectcontext: self.datacontext)!, insertintomanagedobjectcontext: self.datacontext) newplaylist.name = alert.textfields?.first?.text self.playlists.append(newplaylist) { try self.datacontext.save() } catch let error nserror { print(error) } self.tableview.reloaddata() })) alert.addaction(uialertaction(title: "取消", style: uialertactionstyle.cancel, handler: nil)) self.presentviewcontroller(alert, animated: true, completion: nil) } } when click on add button , give playlist name, , click ok (确定), need wait 0.5 ~ 1 second see new cell show up.
what causing this? first guess creation of alerts took long. alerts shows quickly! think there must reloaddata method. exactly? normal behaviour of table views?
looks not using nsfetchedresultcontroller. guess table data based on playlists. maybe can try update table view first save core data later.
self.playlists.append(newplaylist) self.tableview.reloaddata() self.datacontext.performblock({ () -> void in { try self.datacontext.save() } catch let error nserror { print(error) } })
Comments
Post a Comment