3

In my current new project i have to get most of the data from internet and that each object of the downloaded data has to be used in many View controllers and should be available all the time. The question is that should i create all the objects as singleton that downloads the data each time the object has not been initialized? or is there any other efficient way of doing this?

Quamber Ali
  • 2,170
  • 25
  • 46

5 Answers5

3

should i create all the objects as singleton that downloads the data each time the object has not been initialized?

No. Create No Singletons ;)

A common problem with a singleton here is that memory you do not reference is not or cannot be easily purged when not referenced.

or is there any other efficient way of doing this?

NSURLCache would seem a good starting point for your usage. You can create multiple caches, if you are dealing with large data sets. They can reduce redundant download requests, and they can store and evict the information on demand.

Sample

URLCache

Intro

http://petersteinberger.com/blog/2012/nsurlcache-uses-a-disk-cache-as-of-ios5/

justin
  • 104,054
  • 14
  • 179
  • 226
  • as i have studied the intro the **NSURLRequestReturnCacheDataElseLoad** if used will create an issue for me that if anything updated/ changed in the web service it will continuously return the previous cached response, Right? – Quamber Ali Jul 10 '13 at 08:10
  • @Quamber `NSURLRequestReturnCacheDataElseLoad` is like saying "Load it only if it does not exist in the cache". so yes, that mode can return cached data which is older than the current representation on the server. `NSURLRequestUseProtocolCachePolicy` should be your default for cached information: http://www.w3.org/Protocols/rfc2616/rfc2616-sec13.html#sec13 – justin Jul 10 '13 at 09:26
  • justin is correct, a singleton is just a memory leak. Do not use them, they do not play nice with Objective-C because at least on iOS the memory is not garbage collected. So, the singleton is always leaked and anything it references is leaked. – MoDJ Jul 11 '13 at 04:00
1

Usually, in your case, you would use just one singleton object playing the role of a model in your app (as in model-view-controller). The model, thus, would host all of the objects you download from the internet and could even handle their update, initial download, etc.

Using a single model singleton would allow far more flexibility in case you would ever need a refactoring or redesign.

sergio
  • 68,819
  • 11
  • 102
  • 123
1

In your case I would suggest you should not use Singleton in your object classes. Considering the downloaded data might change, updated or deleted at all, I would put a refresh button somewhere and re-fetch the data anytime it is pressed. I would also create the object from the scratch, and avoid using Singleton classes.

Yet if you are sure that the data will not change, save any downloaded data to a plist, core data or etc. and create a Singleton class uses your saved data.

Engnyl
  • 1,380
  • 16
  • 24
0

I think below post may already have answer to your question

On Design Patterns: When to use the Singleton?

Hope this will help you out.

Community
  • 1
  • 1
Kamleshwar
  • 2,967
  • 1
  • 25
  • 27
0

Maybe it's not an answer to question you have posted, but I just want to help you. If you have many objects that you want to get from any controller you can use Core Data. In this case you'll have one data context for whole app.

Valentin Shamardin
  • 3,569
  • 4
  • 34
  • 51