2

i'm working on a little iOS project, and i stumbled across a problem with variable scoping.

what i need is an object that is initialised at launchtime, and is available to all controllers until the application closes.

the object will hold data that is loaded either from a database (sql) or from local storage - im not 100% sure yet what to do here.

i need all viewControllers to access that data-holding object at all times, and i need the object to retain when the app enters the background.

is this possible to achieve? and if, then how would i do it?

for simple variables i know i can use extern variables, but does it also work for complete objects?

thanks for an answer, sebastian

Sebastian Flückiger
  • 5,525
  • 8
  • 33
  • 69
  • Perhaps, what you need is a singleton managing an SQL database or a Core Data store. – Costique Feb 24 '12 at 07:49
  • the object will basically hold a very big NSDictionary, that will be initialised with the data from the database or the file system, and will hold some functions manipulating & getting the right content out of the dictionary. – Sebastian Flückiger Feb 24 '12 at 08:11
  • im not quite sure if coredata is really needed for one single dictionary? if i use the file system i'll probably just store the dictionary to a plist, seems a bit easier? – Sebastian Flückiger Feb 24 '12 at 08:12

2 Answers2

3

This is one of the more common questions here. I would advise to stay away from extern variables and singletons, see my answer for this related question and this sample Xcode project for a better solution. (The sample project is very bare-bones at the moment, I will add more common scenarios later.)

Community
  • 1
  • 1
zoul
  • 102,279
  • 44
  • 260
  • 354
  • hi thanks for the link. i just read through your answer. so you would advise me to create a : `Factory *myFactory = ...` in the applicationDidFinishLaunching method? and in this factory i will have my Object stored? and how would i then access this object from any viewcontroller? im a bit confused – Sebastian Flückiger Feb 24 '12 at 08:30
  • 1
    I’d like to create a sample project that would make the usual use cases clear. Soon, maybe during the weekend. The point is that all your view controllers would be built by the factory, and the factory can easily supply them with all the dependencies needed. If a view controller X needs to create a view controller Y, X would have a reference to the factory and let it build Y for him. – zoul Feb 24 '12 at 08:34
  • im really looking forward to see that =) i think i generally understand the idea, but im a bit confused about 'view controller needs to create view controller'. im using a tabbed application interface, so i have only 4 view controllers. and each of them needs access to the same data model =) – Sebastian Flückiger Feb 24 '12 at 15:05
  • 1
    The sample project is live now. In your case the controllers don’t need to create other view controller’s, that’s a scenario quite common with `UINavigationController`. The access to the shared data model is illustrated with the shared `Logger` instance in the sample project. – zoul Feb 24 '12 at 15:23
  • hi, thanks a lot for the sample, its great! downloaded and understood ;-) – Sebastian Flückiger Feb 24 '12 at 18:22
1

A possible point of initialization could be your app delegate's didFinishLaunching:withOptions: method, or would that be too late? You could also reference the data via the app delegate (like [[UIApplication sharedApplication] delegate]). Edit: Just to be clear, one can do, but I would not recommend storing and accessing arbitrary data this way.

You can also reference objects using external references, as in extern NSString *gGlobalString;. You need a safe place for initialization, though. A singleton could be a better solution.

Peter
  • 531
  • 4
  • 4
  • as far as my research has taken me, the appDelegate method should be avoided, and a singleton probably would be best.. – Sebastian Flückiger Feb 24 '12 at 08:10
  • Apple has a guide to singletons for Cocoa in [Cocoa Fundamentals Guide](https://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/CocoaFundamentals/CocoaObjects/CocoaObjects.html). – Peter Feb 24 '12 at 08:18