6

What is sharedInstance actually? I mean what is the usage?

Currently I'm having some problem in communicating between 2 different files.

Here's my question:

I have 1 file call A.h/A.m and another file call B.h/B.m. A.h need to access some of the data in B.h, so .... is there any possible way I could achieve what I want?

Just wonder is it "SharedInstance" able to solve my problem?

peterh
  • 11,875
  • 18
  • 85
  • 108
Birdkingz
  • 119
  • 2
  • 2
  • 5

4 Answers4

10

sharedInstance could be used for several ways.

For example you can access an object from a static context. Actually it is used most ways for supporting the Singleton-pattern. That means that just one object of the class is used in your whole program code, just one instance at all.

Interface can look like:

@interface ARViewController
{
}
@property (nonatomic, retain) NSString *ARName;

+ (ARViewController *) sharedInstance;

Implementation ARViewController:

@implementation ARViewController
static id _instance
@synthesize ARName;
...
- (id) init
{
    if (_instance == nil)
    {
        _instance = [[super allocWithZone:nil] init];
    }
    return _instance;
}

+ (ARViewController *) sharedInstance
{
    if (!_instance)
    {
        return [[ARViewController alloc] init];
    }
    return _instance;
}

And to access it, use the following in class CustomARFunction:

#import "ARViewController.h"

@implementation CustomARFunction.m

- (void) yourMethod
{
    [ARViewController sharedInstance].ARName = @"New Name";
}
Lepidopteron
  • 6,056
  • 5
  • 41
  • 53
  • Well, please provide us some more detailed information, where the problem lies and what you exactly don't understand :) – Lepidopteron Apr 08 '11 at 08:32
  • OK, I have 2 files - ARViewController.h /.m - CustomARFunction .h /.m Inside ARViewController.h, I have one string variable call ARName. What I want is actually inside CustomARFunction.m, I need to refer to ARName in ARViewController.h and do some changes on it. Means, If I change the ARName by using CustomARFunction.m, the ARName at ARViewController will change also. – Birdkingz Apr 08 '11 at 08:41
  • This answer is wrong, you need to do an alloc/init in the + sharedInstance if the instance is nil, no way to do that on the init. – DFectuoso Apr 04 '12 at 04:52
  • Described by "// Some init-code" ;) Anyway, I adapted the code – Lepidopteron Apr 19 '12 at 12:28
  • `- (id)init` is not called ?! – Raptor May 23 '13 at 11:13
  • should be called now. Best regards – Lepidopteron May 24 '13 at 14:28
7

Shared Instance is a process by which you can access the same instance or object of a class anywhere in the project. The main idea behind this is to return the same object each time a method is called so that the values/properties stored in the instance can be used anywhere in the application.

This can be done in 2 simple process as follows:-

1) Using a static variable initialised only once

@implementation SharedInstanceClass

static SharedInstanceClass *sharedInstance = nil;

+ (id)sharedInstanceMethod
{
    @synchronized(self) {
        if (sharedInstance == nil) {
            sharedInstance = [SharedInstanceClass new];
        }
    }
    return sharedInstance;
}

@end

2) Using GCD's :-

+ (id)sharedInstance{
    static dispatch_once_t onceToken;
    static SharedInstanceClass *sharedInstance = nil;
    dispatch_once(&onceToken, ^{
        sharedInstance = [SharedInstanceClass new];
    });
    return sharedInstance;
}

These have to be called as:-

SharedInstanceClass *instance = [SharedInstanceClass sharedInstance];

Thus everytime the same instance will be returned from the function and the values set to the properties will be retained and can be used anywhere in the application.

Regards,

borncrazy
  • 1,589
  • 1
  • 12
  • 9
3

A sharedInstance is often implemented with the singleton pattern. Like in [UIApplication sharedApplication] -> There is only one application which you access through this singleton.

The idea is to have one instance of a class which can be accessed by calling a class method, in objective-c commonly named sharedXXX.

To solve your problem you could actually implement a singleton of a model class and write and access date to and from one existing instance which can be accessed with a static method, let's call it sharedModel.

The next step to improve on your model and updating the views would be KVO: Key Value Observing. You add an observer in your viewController to 'watch' changes made to your model. If such a change occurs your viewController is informed and you can update the view then.

You can read more about KVO in Apple's documentation or over at mindsizzlers for a small and easy tutorial.

Nick Weaver
  • 47,228
  • 12
  • 98
  • 108
1

Interface

@interface CouponSynchronizer : NSObject

+ (CouponSynchronizer *) sharedSynchronizer;

@end

Implementation

@implementation CouponSynchronizer

static CouponSynchronizer *sharedSynchronizer = nil;

+ (CouponSynchronizer *)sharedSynchronizer
{
    @synchronized(self) {
        if (sharedSynchronizer == nil) {
            sharedSynchronizer = [[self alloc] init];
        }
    }
    return sharedSynchronizer;
}

@end
Shafraz Buhary
  • 643
  • 6
  • 14