Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

delegate for a singleton object

I have a NSObject which is a singleton. Is there any issue of having a delegate for this singleton class? I am worried that it would fail for a singleton type.

Here's my scenario. I have a function (inside this singleton class) that does a async request to pull out a NSDictionary from an API. Basically when this request is done I want to notify a class that the request has finished.

like image 428
adit Avatar asked Feb 28 '12 02:02

adit


People also ask

Can you instantiate a singleton?

We can distinguish a Singleton class from the usual classes with respect to the process of instantiating the object of the class. To instantiate a normal class, we use a java constructor. On the other hand, to instantiate a singleton class, we use the getInstance() method.

Can we create two objects in singleton?

It falls under the category of the creational design pattern in Java. A Singleton class in Java allows only one instance to be created and provides global access to all other classes through this single object or instance.

What are the singleton objects?

An object is a class that has exactly one instance. It is created lazily when it is referenced, like a lazy val. As a top-level value, an object is a singleton.

Can you destroy a singleton object?

You cannot destroy an object, only the JVM can do that. Once an object is inaccessible by any live threads, it then becomes eligible for garbage collection. Sounds like something which possibly shouldn't be a Singleton.


2 Answers

No, a delegate wouldn't fail, but consider using NSNotificationCenter instead:

static NSString *const kMyClassNotificationName = @"myClassNotificationName";

// where you would call a delegate method (e.g. [self.delegate doSomething])
[[NSNotificationCenter defaultCenter] postNotificationName:kMyClassNotificationName object:self userInfo: /* dictionary containing variables to pass to the delegate */];

// where you would set up a delegate (e.g. [Singleton instance].delegate = self)
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(doSomething) name:kMyClassNotificationName object:[Singleton instance]];
like image 181
Richard J. Ross III Avatar answered Nov 16 '22 02:11

Richard J. Ross III


You have basically three options:

  • Use a delegate. A singelton is a objetct, so of couse it can have a delegate. If several objects whants to use it and needs to set themselves as delegates, you can reset them each time, but that might get hairy.

  • Use notifications, as shown by Richard J. Ross III., but seriously: It seems to be strange to me, if you have a singleton, that needs to inform one delegate, but you'd use a broadcasting technology.

  • use completion blocks, where the calling objects passes a block to the singleton, that gets executed, once the singleton fulfilled a task. See [NSURLConnection sendAsynchronousRequest:queue:completionHandler:] (ok, this is not a singleton, but a class method. The principle is the same),that uses one completion block, or the great AFNetworking, that uses a success and a failure block.
    From it's example codes:

    [[AFGowallaAPIClient sharedClient] getPath:urlString 
                                    parameters:mutableParameters 
                                       success:^(__unused AFHTTPRequestOperation 
                                                 *operation, 
                                                 id JSON) 
        {
            NSMutableArray *mutableRecords = [NSMutableArray array];
            for (NSDictionary *attributes in [JSON valueForKeyPath:@"spots"]) {
                Spot *spot = [[[Spot alloc] initWithAttributes:attributes] autorelease];
                [mutableRecords addObject:spot];
            }
    
            if (block) {
                block([NSArray arrayWithArray:mutableRecords]);
            }
        } failure:^(__unused AFHTTPRequestOperation *operation, NSError *error) {
            if (block) {
                block([NSArray array]);
            }
    }];
    
like image 27
vikingosegundo Avatar answered Nov 16 '22 00:11

vikingosegundo