Question regarding SharedInstance

Hey guys, I want to ask what is sharedInstance? I mean what is use?

I am currently having some problem while exchanging data between two different files.

Here is my question: I have 1 Ah / Am call file and another Bh / Bm file call Ah need to access some data in Bh, so ... is there any possible way to achieve what I want? Just wondering if this "SharedInstance" can solve my problem?

Looking for an answer :)

+6
source share
4 answers

sharedInstance can be used in several ways.

For example, you can access an object from a static context. In fact, it is used by most ways to support the Singleton template. This means that only one class object is used in the entire program code, only one instance.

The interface might look like this:

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

Implementation of 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; } 

To access it, use the following in the CustomARFunction class:

 #import "ARViewController.h" @implementation CustomARFunction.m - (void) yourMethod { [ARViewController sharedInstance].ARName = @"New Name"; } 
+11
source

A shared instance is a process by which you can access a single instance or object of a class anywhere in a project. The basic idea is to return the same object every time the 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 processes as follows: -

1) Using a static variable initialized only once

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

2) Using GCD: -

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

They should be called as: -

 SharedInstanceClass *instance = [SharedInstanceClass sharedInstance]; 

Thus, each time the same instance will be returned from the function, and the values ​​set for the properties will be saved and can be used anywhere in the application.

Hi,

+7
source

Usually sharedInstance is implemented with a singleton pattern . As in [UIApplication sharedApplication] → There is only one application that you access through this singleton.

The idea is to have one instance of the class that can be accessed by calling the class method in objective-c, usually called sharedXXX.

To solve your problem, you could really implement a singleton model class, as well as the date of recording and access to one existing instance and to it, which can be accessed with a static method, let it use sharedModel.

The next step to improve your model and update your views will be KVO: Key Value Observing. You add an observer to your viewController to "observe" the changes made to your model. If such a change occurs, your viewController is informed and you can update it.

You can learn more about KVO at Apple Documentation or more at mindsizzlers for a small and easy lesson.

+3
source

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 
+1
source

Source: https://habr.com/ru/post/885483/


All Articles