0

I'm using an NSTimer in an iOS App in background, which is saving some data every 30 seconds in an array. The app shows the last 10 values (values of 5 minutes) in a linechart.

My problem is to use the function of saving data into the array every 30 seconds also in background, when the app isn't on screen. I've written a lot of themes about this, but I don't understand it.

My timer is the following:

timer = NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: (#selector(ViewController.counting)), userInfo: nil, repeats: true)
func counting() {
 timerCounter += 1 //Int
 if timerCounter%30==0 {
   arrayOfValues.append(...) //Appending the array
   reloadLineChart() // reload chart
 }
}

Could anyone show me how to solve this? I know, there must be something with the background-methods in the ViewController, but don't now what to type in. I think there must be a function, which is counting in background and a function that is reloading the chart when I'm back in the app.

Hemmelig
  • 803
  • 10
  • 22

2 Answers2

1

I might understand that you don't want to declare the counter in app delegate, for whatever reason you might have,although I would recommend it. However you can call the functions I mentioned, from the same class in which you have defined the counter. You would need to call it like this:

 override func viewDidLoad() {
    super.viewDidLoad()

    NSNotificationCenter.defaultCenter().addObserver(
        self,
        selector: "applicationWillResignActive:",
        name: UIApplicationWillResignActiveNotification,
        object: nil)
}

func applicationWillResignActive(notification: NSNotification) {
    // stop your counter
}

Then do the same with the other function. Hope is clear.

Alex Zanfir
  • 573
  • 4
  • 13
  • Hello. This function works, thank you very much. Now I've two observer-functions for resignActive and enterForeground, thats good. But my last question isn't clear. I have this Timer in my app. How can I say the code, that he has to run also in background and write data in the array? – Hemmelig May 23 '16 at 08:41
  • Addition: Yes I know, there is a better way, but I'm still learning and for this moment, that is better. I will search for the better way, when I understand the MVC-Concept in iOS-Apps even better. – Hemmelig May 23 '16 at 08:42
  • you have several ways... I would recommend you to follow this example:http://stackoverflow.com/questions/28313702/trying-to-implement-beginbackgroundtaskwithexpirationhandler-and-uilocalnotifica I would also add that iOS will only allow your function to run in the background for 10 minutes. – Alex Zanfir May 23 '16 at 09:29
  • Thanks for your response! I've tried that and it's running in background, very nice! :) But there is still one problem: Every time, I'm reopening the app, there is one more timer in it. After 5 starts, there are six more seconds per second in the counter-value. because there aren’t enough characters left, I’m using pastebin for my problem: http://pastebin.com/P33WuAFM I hope it is understandable. Thank you! :) – Hemmelig May 23 '16 at 10:25
  • Addition: I think the problem is, that my timer in the App is reloading after going into the app, and the timer in the background doesn't stop, thats why I have more than one timer. But how to solve that problem? It's very hard to understand for my, thanks for every person who is helping! – Hemmelig May 23 '16 at 10:30
  • Is there someone who knows a solution for this special problem? :) – Hemmelig May 25 '16 at 11:48
0

In your app delegate you can use this method :

func applicationWillResignActive(application: UIApplication) {}

The code you add in this function will run right before your app goes in the background . Therefore you can write there the code to stop the counter.

Afterwards you need to use the following function to activate the counter again:

 func applicationWillEnterForeground(application: UIApplication) {}

The code you write in this function will run when you come back to the app again. Hope it is clear enough.

Alex Zanfir
  • 573
  • 4
  • 13
  • Thank you very much. That is clear. :) But my problem is, that I want to access methods from my ViewControllerClass in AppDelegate, because the Timer is defined there. – Hemmelig May 23 '16 at 08:14