Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6346635
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T21:03:21+00:00 2026-05-24T21:03:21+00:00

i have made a singleton class for getting locations via core location. now my

  • 0

i have made a singleton class for getting locations via core location. now my problem is that I want to know when the location is updated. I want to use delegates rather than Notifications in this. I know I can post notification. but I dont want to use notifications at all. Is there any other way to do this or only solution for me is NSNotifications.

here is some code

//Initilizer
+ (LocationController *)locationManager;

//How I want to be informed using delegates

id<locationControllerDelegate> delegate;

//Instead what I am being forced to use since I dont know how to use delegates with singleton :(

[[NSNotificationCenter defaultCenter] postNotificationName:@"updated" object:nil];

Thank you.

Edit 1:

in typical delegate and simple class we do like this

someClass *somecls = [[someClass alloc] init];
somecls.delegate = self

but in singleton we dont make any instance of class

[[LocationController locationmanager] startUpdateLocation];

So in this case how i will be setting the delegate for the singleton class

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T21:03:24+00:00Added an answer on May 24, 2026 at 9:03 pm

    I don’t understand your problem using delegates with a singleton pattern based class.

    You create a NSMutableArray to store the observers and notify all in a loop if something happened.

    - (void)addObserver(id<locationControllerDelegate> observer)
    {
        [observers addObject: observer];
    }
    
    - (void)notifyAll()
    {
        for (id<locationControllerDelegate> observer in observers)
        {
            [observer someMethod];
        }
    }
    

    Don’t forget to add a removeObserver() Method.

    Than you can simply add the delegates via

    [[MyClass sharedInstance] addObserver:self];
    

    In your case

    [[LocationController locationmanager] addObserver:self];
    

    Basic Example

    So here a very basic (no memory management) code example, of how singleton works.

    Protocol:
    DelegateProtocol.h

    #import <Foundation/Foundation.h>
    
    @protocol DelegateProtocol <NSObject>
    
    - (void)someMethod;
    
    @end
    

    Singelton class:

    MySingelton.h

    #import <Foundation/Foundation.h>
    @protocol DelegateProtocol;
    
    @interface MySingleton : NSObject{
        NSMutableArray *observers;
    }
    
    + (MySingleton *)sharedInstance;
    - (void)addObserver:(id<DelegateProtocol>) observer;
    - (void)notifyAll;
    @end
    

    MySingleton.m

    #import "MySingleton.h"
    #import "DelegateProtocol.h"
    
    @implementation MySingleton
    
    static MySingleton *sharedInstance;
    
    - (id)init
    {
        self = [super init];
        if (self) {
            observers = [[NSMutableArray alloc] init];
        }
    
        return self;
    }
    
    + (MySingleton *)sharedInstance
    {
        if (sharedInstance == NULL) {
            sharedInstance = [[MySingleton alloc] init];
        }
    
        return sharedInstance;
    }
    
    - (void)addObserver:(id<DelegateProtocol>)observer
    {
        [observers addObject:observer];
    }
    
    - (void)notifyAll
    {
        for(id<DelegateProtocol> observer in observers) {
            [observer someMethod];
        }
    }
    
    @end
    

    And finally the class using the sharedInstance.

    SomeClass.h

    #import <Foundation/Foundation.h>
    #import "DelegateProtocol.h"
    
    @interface SomeClass : NSObject <DelegateProtocol>
    
    @end
    

    SomeClass.m

    #import "SomeClass.h"
    #import "DelegateProtocol.h"
    #import "MySingleton.h"
    
    @implementation SomeClass
    
    - (id)init
    {
        self = [super init];
        if (self) {
        }
    
        return self;
    }
    
    - (void)someMethod
    {
        NSLog(@"Called from singleton!");
    }
    
    @end
    

    And a main method that will use all this stuff:

    main.m

    #import <Foundation/Foundation.h>
    #import "SomeClass.h"
    #import "MySingleton.h"
    
    int main (int argc, const char * argv[])
    {
    
        NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
    
        SomeClass *aClass = [[SomeClass alloc]init];
        [[MySingleton sharedInstance] addObserver:aClass];
        [[MySingleton sharedInstance] notifyAll];
    
        [pool drain];
        return 0;
    }
    

    You will see that the someMethod-Method will be called on notifyAll.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've made a class that is a cross between a singleton (fifth version) and
I want to have a Singleton that will be auto instantiated on program start.
I have made a map overlay class and have overridden the onTouchEvent method that
I have made a class which a form can inherit from and it handles
i have a singleton class with a nsmutablearray - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change
I have implemented an HTTPOperations class in Android that can successfully produce an HTTP
I have made an app that starts a service, which starts a timer, which
I have made this game for Mac OS, but I realised that i need
i have made an window based application in C# and MySQL is my database.now
I have a singleton ejb which is getting initialised twice. I have no idea

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.