For passing data between view controllers, consider creating a singleton

Discussion in 'iOS Programming' started by moonman239, Jul 11, 2015.

  1. moonman239 macrumors 68000

    Joined:
    Mar 27, 2009
    #1
    Just a pro tip: If you've got a whole bunch of view controllers in your app that share an object, you might want to consider creating a singleton instance. A singleton is just an object that can be used by any other object in your app. If you ever called [UIApplication sharedApplication], you used a singleton.
     
  2. xStep macrumors 68000

    Joined:
    Jan 28, 2003
    Location:
    Less lost in L.A.
    #2
    You are off on your explanation of a singleton. Any object can be used by any other number of objects, so that isn't what makes it special.

    The special trait is that when instantiating an object from such a class, you only ever get one instance. The class does not allow more than one object to be created. This allows for one global instance that can be shard by any other objects that need access to it and insures that you don't accidentally create more than one of these shared objects.

    Here is Apples explanation: https://developer.apple.com/library.../Conceptual/DevPedia-CocoaCore/Singleton.html
     
  3. 1458279 Suspended

    1458279

    Joined:
    May 1, 2010
    Location:
    California
    #3
    I was under the impression that the value of the singleton, at least in this usage, was the scope (global access) of the instance.

    Please correct me where I'm wrong. Having access to a global instance, make the transfer of data very simple. Much like user settings, you can pass them thru to every method that needs them, or you can have global access to them.

    This could be balanced against the option of having a datastore where each method does a lookup to find the needed data and the overhead of having to do a lookup and accessing data from a datastore vs having a global structure that stores the same.

    Just like user settings or any other datastore, the singleton has one instance only.

    For some apps, this could become an issue of memory usage vs the time it take to access a datastore, in addition to the need to update the same.
     
  4. moonman239 thread starter macrumors 68000

    Joined:
    Mar 27, 2009
    #4
    Yes. What I like is that I can access the same information from all my view controllers without explicitly passing it on from one controller to the next.
     
  5. 1458279 Suspended

    1458279

    Joined:
    May 1, 2010
    Location:
    California
    #5
    I read a tutorial about just passing data from one to another and it seemed very cumbersome. One problem I saw was that it was all done based on fixed data, meaning that if you change the data structure, you have to go in and change the code.

    Decades ago, I wrote a generic data browser that would read the structure and display whatever it was. It seems pretty backwards that the tutorial was so fixed and cumbersome given the whole MVC/MVVC model and how things are supposed to be flexible, yet it's dependent on the data structure being fixed.

    Although ObjC/Swift isn't really a database language.
     
  6. 1458279 Suspended

    1458279

    Joined:
    May 1, 2010
    Location:
    California
    #6
    Saw what looks like a good post about this:
    http://stackoverflow.com/questions/5210535/passing-data-between-view-controllers
     

Share This Page