簡體   English   中英

Swift-如何調用函數的特定日期和時間

[英]Swift - How to call function specific date and time

即使應用程序未運行,我如何每天下午13:00之后運行功能

如果您的應用程序在后台而不是在后台運行,並且要運行該功能,則應該使用后台任務。 如:

var timer = NSTimer(timeInterval: Your nstimerinterval, target: self, selector: "someBackgroundTask:", userInfo: nil, repeats: true)

func someBackgroundTask(timer:NSTimer){

    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), { () -> Void in
        println("You can do background tasks here.")

        dispatch_async(dispatch_get_main_queue(), { () -> Void in
            println("If you want to update your UI, you should do it here.")

        })
    })
}

最好的方法是使用NSTimer 使用類似:

var timer = NSTimer.scheduledTimerWithTimeInterval(interval, target: self, selector: "function_name", userInfo: nil, repeats: true)

創建一個可以隨時運行某個功能的循環。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM