简体   繁体   中英

ReactiveCocoa :: Can I subscribe to a signal inside a createsignal?

We are having some problems trying to get CLLocationManager working with iOS8.

The idea is that we have a LocationManager that handles all location related stuff. Since iOS8 the ask for permissions deal is asynchronous, so when we try to get a location we might not have received permissions yet.

To work this around we want to do it in a two-step fashion:

  • First check if we already have authorization. If so, tell the subscriber completed .
  • Otherwise, start updating, which will ask for permission. Once the user replied, get the status from the callback. If YES, send completed . If NO, send error .

In the code below, self.authorizationStatusSignal is observing the callback didChangeAuthorizationStatus , so it will trigger whenever the user decides to give permission (or not).

The thing is that, upon subscribing to that signal inside the creation method, the subscriber has lost reference and the completed is never delivered.

Is it possible to subscribe like this inside the creation? We tried strongifying it but nothing happened.

- (RACSignal *)authorizationSignal {

return [[RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {

    if ([self authorized:@([CLLocationManager authorizationStatus])]) {
        [subscriber sendCompleted];

    } else {
        [self.authorizationStatusSignal subscribeNext:^(RACTuple * args) {

            if ([self authorized:(NSNumber *)args.second]) {
                [subscriber sendCompleted];
            } else {
                [subscriber sendError:nil];
            }
        }];
    }
    return nil;
}] replayLast];
}

After the idea proposed by @kenKuan we did another check that hadn't think about. The problem lies in the sendError that is executing (though it shouldn't reach that instance) before we can actually send completed. This way, it prevents the sendcompleted from actually reaching the subscriber.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM