运行将与应用程序并行运行的进程,将再次进行网络调用和睡眠



在我的应用程序中,我需要一台始终与应用程序并行运行的thread(换句话说,将始终在后台运行)。将在此线程上运行的进程将执行以下操作:

1) check for a specific value in my sqlite database
2) if the value is what i need, it will make a network call and get some data from a web service
3) parse the received data and save it in sqlite database
4)sleep for a few minutes
5) repeat the process again

我怎样才能做到这一点?我是否使用 NSOperationsQueue?

您可以按如下方式使用dispatch_async

        dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0)) {
        do {
            if "value is what you need" {
                "make the network call and process the data"
            }
            NSThread.sleepForTimeInterval(100)
        } while true

最新更新