运行一个与应用程序并行运行的进程,将进行网络调用并再次进入休眠状态

时间:2015-01-23 11:59:06

标签: ios objective-c web-services

在我的应用程序中,我需要一个始终与应用程序并行运行的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?

1 个答案:

答案 0 :(得分:0)

您可以按如下方式使用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