ios - Removing rows from Tableview after time expires -


i have searched every source know of on problem. want make individual rows within tableview disappear after amount of time expires. if app not open, want rows delete timer reaches zero. have been trying arrange each post dictionary timer pairings handle row deletion when time occurs. have looked @ post guidance no solutions. swift deleting table view cell when timer expires.

this code handling tableview , timers:

var nextid: string? var postsinfeed = [string]() var posttimer = [timer: string]() var timeleft = [string: int]() 

(in view did load)

dataservice.ds.ref_feed.observe(.value, with: { (snapshot) in         self.posts = []         self.postsinfeed = []         self.nextid = nil          if let snapshot = snapshot.children.allobjects as? [datasnapshot] { //gets our users class of our db             snap in snapshot { // iterates on each user                 if let postdict = snap.value as? dictionary<string, any> { // opens dictonary key value pairs each user.                     let key = snap.key //                     let post = post(postid: key, postdata: postdict) // sets properties in dictionary variable.                     self.posts.append(post) // appends each post contains caption, imageurl , likes empty posts array.                       self.nextid = snap.key                     let activeid = self.nextid                     self.postsinfeed.append(activeid!)                      print(self.postsinfeed)                     print(activeid!)                   }              }          }         self.tableview.reloaddata()      })  ////////////////////////////////////////////////////////////////////////////////////////////// // sets our tableview func numberofsections(in tableview: uitableview) -> int {     return 1 }  func tableview(_ tableview: uitableview, numberofrowsinsection section: int) -> int {     return postsinfeed.count }   func tableview(_ tableview: uitableview, cellforrowat indexpath: indexpath) -> uitableviewcell {      let post = posts[indexpath.row] // our post object array populate when call data database above.      if let cell = tableview.dequeuereusablecell(withidentifier: "tablecell") as? tableviewcell { //specifies format of cell want ui 

// cell.cellid = self.postsinfeed[indexpath.row]

        cell.cellid = self.postsinfeed[indexpath.row]           cell.homevc = self           if let img = homevc.imagecache.object(forkey: post.imageurl nsstring){             cell.configurecell(post: post, img: img as? uiimage)             print(posttimer)             print(self.timeleft)          }  else {              cell.configurecell(post: post)             print(posttimer)             print(self.timeleft)         }           return cell       } else {         return tableviewcell()     } }      func handlecountdown(timer: timer) {     let cellid = posttimer[timer]      // find current row corresponding cellid     let row = postsinfeed.index(of: cellid!)      // decrement time left     let timeremaining = timeleft[(cellid!)]! - 1     timeleft[cellid!] = timeremaining     if timeremaining == 0 {         timer.invalidate()          posttimer[timer] = nil         postsinfeed.remove(at: row!)         tableview.deleterows(at: [indexpath(row: row!, section: 0)], with: .fade)      } else {         tableview.reloadrows(at: [indexpath(row: row!, section: 0)], with: .fade)     } } 

in tableviewcell:

weak var homevc: homevc? var cellid: string!      func calltime() {     homevc?.timeleft[cellid] = 25     let timer = timer.scheduledtimer(timeinterval: 1.0, target: self, selector: #selector(homevc?.handlecountdown(timer:)), userinfo: nil, repeats: true)     homevc?.posttimer[timer] = cellid  } 

any appreciated!

timers don't run when app not running or suspended.

you should rework code save date (to userdefaults) when start timer, , each time timer fires, compare current date saved date. use amount of elapsed time decide how many entries in table view's data model delete. (in case more 1 timer period elapsed while suspended/not running.)

you should implement applicationdidenterbackground() in app delegate (or subscribe equivalent notification, uiapplicationdidenterbackground) , stop timer(s).

then implement app delegate applicationdidbecomeactive() method (or add notification handler uiapplicationdidbecomeactive notification), , in method, check amount of time has elapsed, update table view's data model, , tell table view reload if you've removed entries. restart timer update table view while app running in foreground.


Comments

Popular posts from this blog

What is happening when Matlab is starting a "parallel pool"? -

angular - DownloadURL return null in below code -

php - Cannot override Laravel Spark authentication with own implementation -