0

In my app, I created a class MC_ApiUtility, it contains methods that I use through out the app. Basically, it will contain methods to access API thru web server. I have also some Models.

I prefer to use single instance of MC_ApiUtility & access certain Models through out the app. I mean, I create instance once, use it, update it and in other class also I can use the same updated instance of the object.

What is the best way to achieve such in iOS ?

Tvd
  • 4,463
  • 18
  • 79
  • 125

3 Answers3

1

It sounds as though you're looking for a singleton class. Here's a thread safe pattern for one:

Interface

@interface MYSingeltonClass : NSObject

+ (MYSingeltonClass *) sharedManager;

- (void) doSomething;

@end

Implmentation

@implementation MYSingeltonClass

+ (MYSingeltonClass *) sharedManager
{
    static MYSingeltonClass * sharedManager = nil;

    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        sharedManager = [[MYSingeltonClass alloc] init];
    });

    return sharedManager;
}

- (id)init
{
    self = [super init];
    if (self) {
    }

    return self;
}

- (void) doSomething
{
}    
@end
Ashley Mills
  • 50,474
  • 16
  • 129
  • 160
0

1) Use Singleton design Pattern You will have 1 object usable everywhere via a [MyClass sharedInstance]

2) Create an "helper" : a class with only class methods +(...)... instead of -(...).... You will not have any object, just a boundless of methods (don't use this if you need properties)

Armand DOHM
  • 1,121
  • 1
  • 7
  • 9
0

Well, if i understand your question correctly...

I'm using this model for a long time (original):

  1. You need to create a generic data container class AppDataObject. This is an ancestor class for a data container object that you would actually use to hold your application's global variables.

  2. At init time, You need to add code to the app delegate that create an empty data container object.

  3. After that, you need to create a dirt simple protocol, AppDelegateProtocol. This protocol only lists one method, -theAppDataObject. That method lets you ask the app delegate for it's data object.

  4. And create a subclass of the AppDataObject that actually holds data for a real app. This is where you put the information that you want to share between objects.

Using a protocol means that the only things the objects in your app need to include are the AppDelegateProtocol and the header for your subclass of the app data object. You don't have to #include the header of your app delegate, which makes for good separation between the different objects in your app.

You can download project, ViewControllerDataSharing, that uses the above method to share information between view controllers. The project uses a navigation controller, and has a root view controller and a second view controller. Each view controller has a UITextView and a slider. The app uses an AppDataObject to pass the value of these fields between view controllers.

This method was described by DuncanC - iphonedevsdk forums. This method really saved me a lot of time.

Vlad Z.
  • 3,401
  • 3
  • 32
  • 60
  • Thanks. This was exactly what I was looking for. I have a Tabbed app & call login screen prior that & on successful login I call a web service from where I retrieve data to be shown on 1st Tab view. I get the data from server & update my ExampleAppDataObject to shore the data. In my 1stTabView, I need to update the components using that data. AS tab is already loaded, hence3 the viewDidload doesnt execute again to load the data. From my login view, before calling dismiss loginView, how do I update 1stTabView component with updated code ? – Tvd Mar 01 '14 at 16:12
  • 1
    I got it. I added viewDidAppear method in my 1stTabView & accessed the data & called reloadData of my tableView. – Tvd Mar 01 '14 at 16:42