Keep looping an HTTP request asynchronously
I am developping an application which aggregates feeds. These feeds are on a server (and updated through a website). My application has to request this server often, to gather new updates. Every request to the server contains the list of feeds the user wants to have. Furthermore, these updates have to be done even if the application is non-active (background mode).
Before jumping into the code, I am wondering which architecture would be the best.
I plan to have the following one :
- NSTimer in the appDelegate didFinishLaunching
- NSTimer fires repeatedly an instance which uses ASIHTTPRequest to request server
- If a new request is fired and the previous one isn't finished, the previous one is cancelled
I have some questions regarding this :
- According to you开发者_StackOverflow社区, is it an appropriate architecture ?
- Will it be hard to continue looping after the application is in background ? (I am not very familiar with how to do that - but I am reading the IPhone mutlitasking / thread guide)
- Should I create a dedicated thread for all that purpose ? and why ?
Karim
The user has no use for your feed if the application is the background. Period.
However, you can push (rather frequently and unobtrusive, if you like) important changes to them using push notifications. You can display badges, notifications, etc.
If your user opens the application again, you can simply get the feed. If your feed is fast (and it should be), the user will hardly notice.
I do not see the need for constant background polling here. Everything can easily be solved through a little inception ;-)
UPDATE: to answer your question; your approach will only work for a maximum of 10 minutes (you can ask the system for some background time). After those 10 minutes you'll have to have a plan B (which, in my opinion, should be your plan A).
Backgrounding in iOS is very limited. You're able to play music in the background, get updated location information, and take voip calls. You can also set local notifications so that if you are creating maybe an alarm clock app then that can be backgrounded.
But I don't think you'll be able to fire off http requests to keep updating the data that your app uses. It's possibly not wise to continually update via http also as it will grind the battery right down.
精彩评论