gpt4 book ai didi

swift - NSTimer 在后台绘制 barChart

转载 作者:行者123 更新时间:2023-11-30 10:03:05 25 4
gpt4 key购买 nike

我在后台的 iOS 应用程序中使用 NSTimer,它每 30 秒在数组中保存一些数据。该应用程序以折线图显示最后 10 个值(5 分钟的值)。

我的问题是当应用程序不在屏幕上时,在后台使用每 30 秒将数据保存到数组中的功能。我写了很多关于这个的主题,但我不明白。

我的计时器如下:

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
}
}

谁能告诉我如何解决这个问题吗?我知道,ViewController 中一定有一些后台方法,但现在不知道要输入什么。我认为必须有一个函数在后台进行计数,还有一个函数在我返回应用程序时重新加载图表。

最佳答案

我可能理解您不想在应用程序委托(delegate)中声明计数器,无论出于何种原因,尽管我会推荐它。但是,您可以从定义计数器的同一类中调用我提到的函数。您需要这样调用它:

 override func viewDidLoad() {
super.viewDidLoad()

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

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

然后对其他函数执行相同的操作。希望是明确的。

关于swift - NSTimer 在后台绘制 barChart,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37385446/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com