Where do I store this data?

Discussion in 'iOS Programming' started by North Bronson, Feb 6, 2009.

  1. North Bronson macrumors 6502

    Joined:
    Oct 31, 2007
    Location:
    San José
    #1
    Right now I'm working on an iPhone app. There are several view controllers that are working with data that needs (for the most part) to be shared through all the controllers. Right now I have a DataController model class for this.

    My AppDelegate class has an instance of the DataController class called myDataController. Each view controller also has an instance of a DataController. Whenever I init a new view controller, I set the new view controller's DataController equal to my old DataController (originally from the AppDelegate). So if View Controller A changes something, View Controller B (pointing to the same DataController) sees the change as well.

    I'm just wondering if this is a good way to go about doing things. Would delegation be a better idea? Should I make my AppDelegate also function as a "Data Delegate" that can return the data that I need to each view controller?

    Which approach is more efficient? Are both about the same? Is one more preferred?
     
  2. robbieduncan Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #2
    Seems like you are trying to use DataController as a singleton object without correctly making it one. Follow the singleton pattern and all will be fine (this is what I do with my root data model object).

    The pattern for this is covered in some detail in the Cocoa Fundamentals document provided by Apple that you should have read.
     
  3. North Bronson thread starter macrumors 6502

    Joined:
    Oct 31, 2007
    Location:
    San José
    #3
    Ahh. Yes, I should have thought of that.

    So then all I would need is something like:

    [ [DataController myOnlyDataController] myArray ];

    To get myArray from anywhere in the app?
     
  4. robbieduncan Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #4
    Yes, although it should be noted that, as per the example, the convention in Cocoa would be for the method to be called something like sharedDataController so the code would look like:

    Code:
    [[DataController sharedDataController] myArray]
    
     

Share This Page