gpt4 book ai didi

ios - 当应用程序在后台时 Alamofire 请求

转载 作者:行者123 更新时间:2023-11-30 13:54:29 25 4
gpt4 key购买 nike

嗨,我想使用 Alamofire 和 NSTimer 获取一些数据

该应用程序在前台运行良好,计时器设置为每 30 秒调用一次 GetEstado 函数,但当应用程序进入后台时,计时器会暂停,如果我的服务器更新某些内容,它实际上不会更新在应用程序上

这是我的代码

func GetEstado(){
Alamofire.request(.POST, "https://myurl/getestado.php", parameters: ["id": id])
.responseString { rta in
if let dataFromString = rta.result.value!.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: false) {
let json = JSON(data: dataFromString)
for (key,subJson):(String, JSON) in json {
if(key=="e"){
if(subJson.stringValue=="1"){
self.Timer.invalidate()
print("ACTUALIZACION")
let notification = UILocalNotification()
notification.fireDate = NSDate(timeIntervalSinceNow: 5)
notification.alertBody = "UPDATED"
notification.soundName = UILocalNotificationDefaultSoundName
UIApplication.sharedApplication().scheduleLocalNotification(notification)
self.performSegueWithIdentifier("UPDATED", sender: self)
}
else{
print("UPS")
}
}
}
}
}
}

谢谢

最佳答案

我认为@Paulw11在他的评论中回答了你的问题,你不能在后台使用NSTimer,Apple有一个已知的可以在后台生成的任务列表,你可以阅读更多内容here

但是让我们想一下您想要实现的目标,假设您可以使用 NSTimer 来实现,然后您的应用程序会在后台不断消耗设备中的内存和电池来尝试从您的服务器获取更新,根本不建议这样做。

为此,Apple/Google 引入了 Apple 推送通知(Google:Android 推送通知)来处理服务器在后台或不活动时通知应用程序有关服务器中编程的任何任务(例如一封新邮件)的情况、消息应用程序中的新消息等。

然后,您可以选择使用 Apple 提供的服务,您可以在以下文档中阅读更多内容:

希望这对您有帮助。

关于ios - 当应用程序在后台时 Alamofire 请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33814718/

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