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 9299185
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T22:30:02+00:00 2026-06-18T22:30:02+00:00

How would I implement a RACSignal that would stop publishing when there are no

  • 0

How would I implement a RACSignal that would stop publishing when there are no subscribers to it and auto start when there are subscribers?

Here is a scenario:

Let us say I have a currentLocationSignal in the AppDelegate.
My LocationViewController would subscribe to the currentLocationSignal when view loads and unsubscribe (dispose) when view unloads. Since it takes few seconds to get the current location, I would like to always subscribe to the currentLocationSignal when the app opens (and auto unsubscribe after few seconds), so by the time I arrive to LocationViewController I would get an accurate location.
So there can be more then one subscribers to the signal. When the first subscriber listens, it needs to start calling startUpdatingLocation and when there are no subscribers it needs to call stopUpdatingLocation.

  • 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-06-18T22:30:03+00:00Added an answer on June 18, 2026 at 10:30 pm

    Good question! Normally, you’d use RACMulticastConnection for use cases like this, but, because you want the signal to be able to reactivate later, a connection isn’t suitable on its own.

    The simplest answer is probably to mimic how a connection works, but with the specific behaviors you want. Basically, we’ll keep track of how many subscribers there are at any given time, and start/stop updating the location based on that number.

    Let’s start by adding a locationSubject property. The subject needs to be a RACReplaySubject, because we always want new subscribers to get the most recently sent location immediately. Implementing updates with that subject is easy enough:

    - (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations {
        [self.locationSubject sendNext:locations.lastObject];
    }
    

    Then, we want to implement the signal that tracks and increments/decrements the subscriber count. This works by using a numberOfLocationSubscribers integer property:

    - (RACSignal *)currentLocationSignal {
        return [RACSignal createSignal:^(id<RACSubscriber> subscriber) {
            @synchronized (self) {
                if (self.numberOfLocationSubscribers == 0) {
                    [self.locationManager startUpdatingLocation];
                }
    
                ++self.numberOfLocationSubscribers;
            }
    
            [self.locationSubject subscribe:subscriber];
    
            return [RACDisposable disposableWithBlock:^{
                @synchronized (self) {
                    --self.numberOfLocationSubscribers;
                    if (self.numberOfLocationSubscribers == 0) {
                        [self.locationManager stopUpdatingLocation];
                    }
                }
            }];
        }];
    }
    

    In the above code, the +createSignal: block is invoked every time a new subscriber is added to the returned signal. When that happens:

    1. We check to see if the number of subscribers is currently zero. If so, the just-added subscriber is the first one, so we need to enable (or re-enable) location updates.
    2. We hook the subscriber directly up to our locationSubject, so the values from the latter are automatically fed into the former.
    3. Then, at some future time, when the subscription is disposed of, we decrement the count and stop location updates if appropriate.

    Now, all that’s left is subscribing to the currentLocationSignal on startup, and automatically unsubscribing after a few seconds:

    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
        // Use a capacity of 1 because we only ever care about the latest
        // location.
        self.locationSubject = [RACReplaySubject replaySubjectWithCapacity:1];
    
        [[self.currentLocationSignal
            takeUntil:[RACSignal interval:3]]
            subscribeCompleted:^{
                // We don't actually need to do anything here, but we need
                // a subscription to keep the location updating going for the
                // time specified.
            }];
    
        return YES;
    }
    

    This subscribes to self.currentLocationSignal immediately, and then automatically disposes of that subscription when the +interval: signal sends its first value.

    Interestingly, -[RACMulticastConnection autoconnect] used to behave like -currentLocationSignal above, but that behavior was changed because it makes side effects wildly unpredictable. This use case should be safe, but there are other times (like when making a network request or running a shell command) when automatic reconnection would be horrible.

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

Sidebar

Related Questions

I have to make a library as an assignment that would implement a menu-like
I realize that each browser would implement it differently, but are there any references
Is there an R library/function that would implement INTELLIGENT label placement in R plot?
There is a piece of code that I would implement like this in java:
I am trying to create a class say MyStack that would implement a java.util.collections
looking at dribbble, I have started to ask myself, how I would implement a
How would you implement a Listener that is queuing events that are executed at
I would like to have a script with an infinite loop, that kills all
I'm learning Python and would like to start a small project. It seems that
I have interview for an internship with company that wants to implement document management

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.