ios - Firebase - sort posts by number of keys and show in TableView -
i have made app save users message , username firebase childbyautoid key. inside there childs saves username, message, likes , postid (as can see on image below).
after lot of research , lot of trying writing code myself, figure out likes need saved autoid keys inside separate child , have count keys number of likes (you'll see child on image under , child named "liked")
everything displayed in tableview cell. of them displayed randomly ok (i prefer ordered date added), loaded data in next next vc displayed as:
top 10 of week top 10 of month best etc...
there'll separate button menu , when press it, you'll presented next vc contain table view same data, time sorted liked post.
this code writes keys liked child path (when pressed on loaded data firebase database):
@ibaction func likepressed(_ sender: any) { let ref = database.database().reference() self.likebutton.isenabled = false let key = ref.child("frusters").childbyautoid().key ref.child("frusters").child(self.postid).observesingleevent(of: .value, with: { (snapshot) in let updatelikes = ["liked/\(key)" : key] [string : any] ref.child("frusters").child(self.postid).updatechildvalues(updatelikes, withcompletionblock: { (error, reff) in if error == nil { ref.child("frusters").child(self.postid).observesingleevent(of: .value, with: { (snap) in if let properties = snap.value as? [string : anyobject] { if let likes = properties["liked"] as? [string : anyobject] { let count = likes.count self.likelabel.text = "\(count) likes" let update = ["likes" : count] ref.child("frusters").child(self.postid).updatechildvalues(update) self.likebutton.ishidden = true self.unlikebutton.ishidden = false self.likebutton.isenabled = true } } }) } }) }) ref.removeallobservers() }
and code loads data , put in table view:
func loaddata() { self.fetchposts.removeall() let ref = database.database().reference() ref.child("frusters").observesingleevent(of: .value, with: { (snapshot) in if let postdict = snapshot.value as? [string:anyobject] { (_,postelement) in postdict { print(postelement); let post = post() post.username = postelement["username"] as? string post.message = postelement["message"] as? string post.likes = postelement["likes"] as? int post.postid = postelement["postid"] as? string self.fetchposts.append(post) } } self.tableview.reloaddata() }) { (error) in print(error.localizeddescription) } ref.removeallobservers() } func tableview(_ tableview: uitableview, numberofrowsinsection section: int) -> int { return self.fetchposts.count } func tableview(_ tableview: uitableview, cellforrowat indexpath: indexpath) -> uitableviewcell { let cell = tableview.dequeuereusablecell(withidentifier: "cell") as! posttableviewcell cell.messagelabel.text = self.fetchposts[indexpath.row].message cell.usernamelabel.text = self.fetchposts[indexpath.row].username cell.likelabel.text = "\(self.fetchposts[indexpath.row].likes!) likes" cell.postid = self.fetchposts[indexpath.row].postid cell.bckgview.layer.cornerradius = 0 cell.bckgview.layer.shadowoffset = cgsize(width: 0, height: 1) cell.bckgview.layer.maskstobounds = false cell.bckgview.layer.shadowcolor = uicolor.black.cgcolor cell.bckgview.layer.shadowopacity = 0.3 cell.bckgview.layer.shadowradius = 4 return cell } func tableview(_ tableview: uitableview, heightforrowat indexpath: indexpath) -> cgfloat { return uitableviewautomaticdimension; }
well, problem not know how insert inside new uitableview top 10 post likes, sort them liked post next 9 of them likes.
also, possible sort them liked month or week? keys firebase database makes (by autoid) contain date of created post or have insert new child date inside , in code combine "date" child , "liked" child presented top 10 liked post between 1st , last of month?
thanks in advance. ;)
Comments
Post a Comment