Use Grand Central Dispatch to schedule a function same time every day

后端 未结 2 1472
故里飘歌
故里飘歌 2021-01-25 16:12

I have read this answer and I do not believe it has what I am looking for but I a am beginner and I am happy to have someone point out the answer in this link : dispatch_after -

2条回答
  •  Happy的楠姐
    2021-01-25 16:31

    In short, you cannot generally execute some arbitrary function at some arbitrary time unless the app is still running. The dispatch_after presumes that the app is still running at the scheduled time, which is not generally assured. The dispatch_after is great for "do something in x seconds", not "do something tomorrow at 9am".

    Yes, dispatch_after can perform some task on some background thread, but that's very different concept from having the app run in the background (i.e., when the app, itself, is no longer in foreground). Please refer to App Programming Guide for iOS: Background Execution, which enumerates all of the various background mechanisms.

    The key technologies for performing something when the app is not currently active include:

    • Using background fetch, you can opportunistically check for new data on the server (but not per your schedule, but rather at the discretion of the OS).

    • If your app is serving several very specific tasks (e.g. music app, VOIP, navigation app, etc.) you can register for background operation.

    • You can schedule a local notification to fire at particular time (though it is incumbent on the user to see the notification and tap on it in order for the app to run). For more information see the Local and Remote Notification Programming Guide.

    • You can register for push notifications and then your server could push a notification to clients at some time of your choosing (but that requires server-side development).

    There are other background tasks that can be configured (e.g. continue network requests in background, request a few minutes to finish some finite length task even if the app is no longer active, etc.), but those seem unrelated to your question.

    If you could clarify what you want the app to do at the scheduled time, we might be able to advise what is possible and what the alternatives are. But, in broad brush strokes, those are the basic options you have.

提交回复
热议问题