swift - 在 Swift 4.2 中,后台获取每秒都在工作
问题描述
看哪。我正在尝试使用 swift 4.2 创建一个 IOS 应用程序,据我的客户说,它必须发出警报,但只能通过 QR 码停止。因此,我需要一个在后台工作的计时器,每秒钟检查一次时间,因为别的东西,用户可以在没有二维码的情况下停止它。我唯一找到的就是后台提取,但它可以每次都进行 1 秒检查吗?
有没有人有更好的主意?
干杯
这是我现在的源代码
应用委托:
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
UIApplication.shared.setMinimumBackgroundFetchInterval(2.0)
}
//ini----------------------------background task
func application(_ application: UIApplication, performFetchWithCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
// fetch data from internet now
let time:TimerNow = TimerNow();
let hours = preferences.string(forKey: "hours");
let minutes = preferences.string(forKey: "minutes");
if((hours == time.getHours())&&(minutes == time.getMinutes())){
lem.onPlayCommand();
}
completionHandler(.newData)
}
//fin ---------------------------background task
解决方案
我认为您应该改用本地通知。当您想要通知已用时间时,它将被触发。您可以随时以任何您想要的条件取消它。
IMO 计时器和后台获取不适合这种情况。
推荐阅读
- javascript - 有没有更好的方法来访问 package.json 中的数组数据
- brightway - Brightway ExcelImporter 跨多个 Excel 文件
- javascript - Google Javascript API - .each 中的 If 语句打破循环
- sql - 如何根据 SQL 中的组递增整数
- python-2.7 - 检查 Pyspark Dataframe 中的重复项
- javascript - 角度 4 嵌套 formArray 找不到带有路径的控件:'segmentRows3 -> 1 -> segmentId3'
- c++ - C++ 中的位集初始化
- javascript - 如何检查侦听器事件是否为被动事件?
- java - Eclipse 无法访问项目路径中的 mysql 连接器驱动程序
- c# - 动作委托在 mvvm wpf 中发送空值