2

I have some bluetooth class that needs to be alive during all views . That means, that i have to call functions in this class from different views, and ALSO get delegates from this class to different views .

So , i have to initialise it once to create connection, than later, i have to use it again from different views(classes) and register to get notifications from it.

I was thinking about a singleton, that HE will create an instance of that bluetooth class, and i can access him from anywhere . But, i would also like that any view can get delegates from it .

How would i do that ? i have read What should my Objective-C singleton look like?

But maybe singleton is not what i need ? How can you create a class to always be alive, and register to get delegates from it from anywhere ? (how can it be done with app delegate class ? )

Community
  • 1
  • 1
Curnelious
  • 1
  • 16
  • 76
  • 150
  • Delegate and protocols there are different oop patterns. – Anton Feb 18 '15 at 08:32
  • thanks a lot Anton , so what are you suggest ? – Curnelious Feb 18 '15 at 08:36
  • every pattern have advantages and disadvantages. singleton it's simplest pattern to use. – Anton Feb 18 '15 at 08:55
  • You could use a few patterns in one class. Because singleton you are calling always like [dataobject sharedobject] somemethod]; I suggest every pattern to use in different classes. But sometimes you will use a few patterns in singleton also. like nsnotification or delegate also. it's depend from your needs. – Anton Feb 18 '15 at 08:56

4 Answers4

2

I have had the similar query a while back

Problem : Multiple Classes need to receive delegate calls from single instance

Solution: I used a combination of sharedInstance , delegates and NSNotifications to handle the problem

SharedInstance

+ (SomeBluetoothClass *) sharedInstance {
  static dispatch_once_t once;
  static SomeBluetoothClass *sharedInstance;
  dispatch_once(&once, ^{
    sharedInstance = [[self alloc] init];
  });
  return sharedInstance;
}

Delegate

@property (weak, nonatomic) id <SomeBluetoothClassDelegate> delegate;

Since delegate can respond to only one class at a time. Assign class in focus based on your logic. Then whenever you want send info to all send it across using NSNotificationCenter from the sharedInstance. Send the info through using userInfo dictionary of NSNotifications

Notifications

[[NSNotificationCenter defaultCenter] postNotificationName:SomeBluetoothClassNotification
                                                        object:self
                                                      userInfo:info];

Model the structure of SomeBluetoothClass to be thread safe and handle all notifications along with the delegates and it should work fine.

Edwin Abraham
  • 645
  • 7
  • 24
2

Have a lots way for your situation. Firstly, you should understand that creating object is not too heavy.

  • So if you want to use Delegate, you can create a Factory method Ex:
+ (instancetype)bluetoothManagerWithDelegate:(id<delegate>)delegate {
     return [self alloc] initWithDelegate:delegate];
}

So that you also don't care about conflict of concurrence. Because you have separate Bluetooth class.

  • If you still want to use Singleton, in this situation, it depends on how many object you want to notify.

    • Only 1 views, just use delegate, and set new delegate when you present new view.
    • More than one, you can use NSNotificationCenter or Observer, you can google these keywords, it have a lot of tutorial and document on the internet help you use it.
Huy Le
  • 2,503
  • 1
  • 15
  • 15
0

You need to create a class that should be allocated memory once in a lifetime I am posting a small code snippet which can help you.

In your .m file

#pragma mark - Shared Instance

static BlootoothClass *_sharedblootoothclass = nil;

+ (BlootoothClass *) sharedClass {
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        _sharedblootoothclass = [[self alloc] init];
        // DO YOUR STUFF TO INTIALISE YOUR CLASS
    });
    return _sharedblootoothclass;
}

in Your .h file

+ (BlootoothClass *) sharedClass

dispatch_once is the queue with dispatch the instance of your class one in a lifetime and you can access its function all over in the app any time.

Now to get any data from it you can get it from instance from any where like

[BlootoothClass sharedClass].anyObject

And you also can send post notification from here in any of its function

 - (void)detectedBlootoothdevice{
   [[NSNotificationCenter defaultCenter] postNotificationName:@"newdevicedetected" object:nil];
}

You should not use delegates as you cant call same delegate function at multiple class because last delegate get overwritten.

haresh
  • 486
  • 3
  • 18
0

I think you can create NSMutableArray in your singleton with links on your views and call someMethod for all objects when is needed. Don't forget remove views from array when its don't need anymore. That is simple realization of pattern called "Observer".

Slashik
  • 325
  • 1
  • 4