ios - FirebaseTableViewDataSource crashes on user signout and signin -
my app has uitableviewcontroller uses firebasetableviewdatasource (from firebaseui). table shows user's bookmarked posts correctly, when log user off, , log user in, app crashes following message:
* terminating app due uncaught exception 'nsinternalinconsistencyexception', reason: 'invalid update: invalid number of rows in section 0. number of rows contained in existing section after update (1) must equal number of rows contained in section before update (2), plus or minus number of rows inserted or deleted section (1 inserted, 0 deleted) , plus or minus number of rows moved or out of section (0 moved in, 0 moved out).'*
i suspect problem related how firebaseui updates content of tableview. i've being debugging past 4 days; searched questions; read relevant docs none mentions unique issue. appreciated.
the details (sorry it's long)
this database structure:
| +-posts | | | +-$postid | | | +-/* properties of post: title, text, etc */ | +-users | +-$userid | +-bookmarks | +-post1: true | +-post2: true
i using firebaseui show user his/her bookmarks passing users/$userid/bookmarks
ref firebasetableviewdatasource query. each key, observe single value event on posts/$postid
in order retrieve post details...code below:
self.authstatelistenerhandle = firauth.auth()?.addstatedidchangelistener { auth, user in guard let user = user else { return } self.query = firdatabase.database().reference().child("users").child(user.uid).child("bookmarks") self.tableviewdatasource = firebasetableviewdatasource(query: self.query!, view: self.tableview, populatecell: { (tableview, indexpath, snapshot) -> uitableviewcell in if snapshot.exists() { let postref = firdatabase.database().reference().child("posts").child(snapshot.key) let cell = tableview.dequeuereusablecell(withidentifier: bookmarksvc.reuseidentifier, for: indexpath) as! minitablecell postref.observesingleevent(of: .value, with: { (snapshot) in if snapshot.exists() { let post = event(snapshot: snapshot)! let postvm = eventviewmodel(post: post) cell.populatecellwithevent(postvm) cell.delegate = self } }) return cell } else { return uitableviewcell() } }) self.tableview.datasource = self.tableviewdatasource }
i put above code in viewdidappear , remove authstatelistenerhandle in viewwilldisappear so
override func viewwilldisappear(_ animated: bool) { super.viewwilldisappear(animated) if let handle = self.authstatelistenerhandle { firauth.auth()?.removestatedidchangelistener(handle) } }
almost works fine except, when viewing bookmarks user, log out , log in, app crashes message
* terminating app due uncaught exception 'nsinternalinconsistencyexception', reason: 'invalid update: invalid number of rows in section 0. number of rows contained in existing section after update (1) must equal number of rows contained in section before update (2), plus or minus number of rows inserted or deleted section (1 inserted, 0 deleted) , plus or minus number of rows moved or out of section (0 moved in, 0 moved out).' *
in viewwilldisappear
set self.tableviewdatasource = nil
. so, don't update datasource
improperly.
Comments
Post a Comment