How to create thread to checking time?

眉间皱痕 提交于 2019-12-11 19:11:32

问题


I have to setting a time to check for update data from server 2times a day, suppose 9AM and 9PM, but i don't know the way to do that.

Create a thread, run every-time and check time current time with setting,if they are equal,so do task? This way is good? I think that's bad performance.


回答1:


Use thread but instead of checking every sec, if it is equal. You could use NSNotification. When the time is the same, it will send a notification to get the data.

You can read this: iOS timed background processing

Can go through this tutorial as well: http://mobile.tutsplus.com/tutorials/iphone/ios-multitasking-local-notifications/

I have reading up as well. Something new for me as well. Best is NSLocalNotifications. Have a look at this: Reminder App

The documentation for Local Notifications: Local Notifications




回答2:


maybe when your app is launching , you can get the current time(also can get the timeInterval of current time called timeInterval0) using [NSDate date] and if your current time < 9:00AM today , you can get the timeInterval of 9:00AM today called timeInterval1. and then you can get how many seconds between now and your target time , then use NSTimer to invocation your method ,the delay is the between seconds.

In your method , you can check the update , and you must also to calc the seconds from then between next 9:00 as above.



来源:https://stackoverflow.com/questions/14475195/how-to-create-thread-to-checking-time

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!