0

In an Xcode project I have a button that runs a function that retrieves the number of followers of an instagram account each 18.5 seconds.

@IBAction func run(_ sender: UIButton) {
    updateLabel()
    Timer.scheduledTimer(timeInterval: 18.5, target: self, selector: (#selector(ViewController.updateLabel)), userInfo: nil, repeats: true)
    stop.isHidden = false        
}

I would like to add a button stoprun stopping the retrieving of the followers when pressed.

Do you think there is a way to stop this kind of function?

pawello2222
  • 46,897
  • 22
  • 145
  • 209
Bambary
  • 21
  • 2
  • The `scheduledTimer` returns a `Timer` object. You can call `invalidate` to stop that timer. – Rob Jan 01 '21 at 17:49
  • 1
    See https://stackoverflow.com/a/25952724/1271826. Note, you might consider using the block-based rendition with `[weak self]` qualifier in the closure, to prevent strong reference cycle. – Rob Jan 01 '21 at 17:57
  • This question is little bit different from those mentioned in a close vote. The differences are: 1. created timer is not stored in a property which will give a possibility to invalidate it later in a simple way. 2. Timer should be started on a button did tap action and not as in most solutions in viewDidLoad or similar controller life cycle methods which leads to answers which can propose creating a timer in life cycle methods and adding it to a runloop in IBAction – Blazej SLEBODA Jan 01 '21 at 18:20
  • 1
    @BlazejSLEBODA - 1. Yes, Bambary is not storing the resulting `Timer` object anywhere. But that's the point of all of those answers, isn't it? Namely that the OP must keep a reference to the returned `Timer` object if he wants to `invalidate` it later. 2. The fact that a repeating timer with selector results in a strong reference cycle has absolutely nothing to do with where you start the timer. Whether `viewDidLoad` or in a button, if you start a repeating timer with selector, that introduces a strong reference cycle. Where you introduced the strong reference cycle is immaterial. – Rob Jan 01 '21 at 19:04
  • 1
    @Rob Your remark on ARC management is valid. I should be more explicit in my comment. I think that closing this question was not the best experience for the OP and I wanted to point a few major points to explain why. – Blazej SLEBODA Jan 01 '21 at 20:28
  • @BlazejSLEBODA - I agree: Stack Overflow can tough environment for first-time contributors and I hope Bambary isn't too discouraged by the response. So I appreciate your attempt to keep a positive, constructive vibe going! Hopefully Bambary will take this feedback as intended, constructive observations on [how to ask a question](https://stackoverflow.com/help/how-to-ask). The key is to research first, and if he concludes he really needs to ask a new question, then include a few links to related questions/answers that he found, so we can see better understand where he is coming from. – Rob Jan 02 '21 at 00:15
  • Thanks everyone for your constructive feedbacks. This solved my problem – Bambary Jan 03 '21 at 17:14

0 Answers0