I am currently thinking about the data-model for my iOS App. The app does only receive Information from a server and therefore, the “model” itself generally does all the web-requests, …
However, these network requests have to be performed in the background – I mean another task, not the iOS background state – and after the request has finished, the information in the Application has to be updated.
Would it make more sense to write a delegate to inform the controller or could I also use NSNotificationCenter? I think an NSNotification-based solution could make the model more universal, e.g. in a desktop application.
And maybe, I should add: the model (since it saves some session Information) is a singleton, so a regular delegate based method would not work…
I don’t think that it would be a good idea to use a separate thread to handle the communication. Apart from being complex, it is not necessary since
NSURLConnection/NSURLRequestdo allow you to handle communication asynchronously, i.e., without blocking.In detail, you can create an NSURLRequest executing:
then create an NSURLConnection with:
and start it with:
When the data is ready, one of the methods for your delegate (
connectionDidFinishLoading:, orconnection:didFailWithError:) will be called so that you can update your UI.All this without blocking.
An even better alternative to using
NSURLConnection/NSURLRequest, is using ASIHTTPRequest, which seems to be more resilient as to memory management and also offers a great caching mechanism.EDIT: if your concern is that being your model a singleton, you cannot have a delegate, let me suggest that you look further into the matter.
Your model may be a singleton and the delegate to your request may have nothing to do with the model, apart from knowing how to access it (which is pretty trivial being the model a singleton).
This is possible by several mechanisms with
NSURLConnection, but if you useASIHTTPRequest, it will become really easy, because eachASIHTTRequestcan have its own delegate.