2

I added a a UIRefreshControl to my tableview so that I could allow the user to pull to refresh.

They do not need to refresh it constantly, only every 30 seconds. How can I allow them to only do it every 30 seconds?

@IBOutlet weak var tableView: UITableView!

private let refresher: UIRefreshControl = UIRefreshControl()

override func viewDidLoad() {
    super.viewDidLoad()

    //load Event data
    loadData()

    // Add Refresh Control to Table View
    tableView.refreshControl = refresher

    refresher.addTarget(self, action: #selector(refreshData(_:)), for: .valueChanged)
    refresher.attributedTitle = NSAttributedString(string: "Fetching events...")

}

@objc private func refreshData(_ sender: Any) {
    // Fetch Data
    loadData()
}


private func loadData() {
     //code here for grabbing data
}
thalacker
  • 2,389
  • 3
  • 23
  • 44
  • When you say 30 secs, you mean 30 secs after their first fetch? – Deepika Dec 14 '18 at 05:04
  • you want to refresh data after 30 secs ? – junaid Dec 14 '18 at 05:04
  • 1
    Simple trick is just remove `UIRefreshControl` from tableview then after nth sec add back to tableview. – Pratik Prajapati Dec 14 '18 at 05:05
  • You can easily do this by using `Timer`, look https://stackoverflow.com/a/43993602/4003548. – vaibhav Dec 14 '18 at 05:05
  • Magic term is : **Timer**. Initiate it on "first" refresh design it to call the designated method after ever 30 seconds. – iOSer Dec 14 '18 at 05:08
  • I want them to be able to refresh every 30 seconds. So there is the initial load and they can refresh right away, but then have to wait 30 seconds before doing it again. Preferablly, show the animation and just don't have it refresh. – thalacker Dec 14 '18 at 05:20

2 Answers2

4

Very simple; every time the user refreshes, you store the Date in a property, and the next time, if 30 seconds have not elapsed, you do nothing.

matt
  • 515,959
  • 87
  • 875
  • 1,141
0

Don't remove refresh control it will let bad user experience

There are several options but I think you have can achieve it by this

take a global variable in your class

var start: CFAbsoluteTime = CFAbsoluteTimeGetCurrent()

and on refreshData method check

let elapsed = CFAbsoluteTimeGetCurrent() - start

you get seconds if it is greater than your required time then fire API other wise just stop refresh control and re assign start = CFAbsoluteTimeGetCurrent()

Prashant Tukadiya
  • 15,838
  • 4
  • 62
  • 98