HiveBrain v1.2.0
Get Started
← Back to all entries
patternMinor

Localization manager based on ReactiveCocoa

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
managerlocalizationbasedreactivecocoa

Problem

I have a localization manager class which can change app language on the fly. It's using ReactiveCocoa and my main concern is that I'm not using some of RAC parts correctly.

First (in - (void)configureSignals):

+ (instancetype)sharedInstance
{
    static i2KRLMLocalizationManagerObject *sharedInstance = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        sharedInstance = [[[self class] alloc] init];
        [sharedInstance initialConfiguration];
    });
    return sharedInstance;
}

- (void)initialConfiguration
{
    [self configureSignals];
    [self configureAppLocalizations];
}

- (void)configureSignals
{
    RACReplaySubject *subject = [RACReplaySubject replaySubjectWithCapacity:1];
    self.localeConnection = [subject multicast:subject];
    [self.localeConnection autoconnect];

    RACReplaySubject *languagesSubject = [RACReplaySubject replaySubjectWithCapacity:1];
    self.languageNamesConnection = [languagesSubject multicast:languagesSubject];
    [self.languageNamesConnection autoconnect];

    RACReplaySubject *languageIndexSubject = [RACReplaySubject replaySubjectWithCapacity:1];
    self.languageIndexConnection = [languageIndexSubject multicast:languageIndexSubject];
    [self.languageIndexConnection autoconnect];
}


It feels for me that multicast should be used differently(and is used wrong currently, although it works), although I'm not sure how to use it correctly.

Those signals are used to propagate NSLocale, array of supported languages and current language index in that array. I use multicast so that if there is more then 1 subscriber, signal will be shared and current values will be just replayed. I'm not sure that signal multicasting itself is idiomatic/correct way. Also I'm not completely sure if connection is needed at all or I can just go with ReplaySubject.

Second (like here):

```
  • (void)changeToLocale:(NSLocale *)locale


{
[self reloadBundleWithLocaleID:[locale localeIdentifier]];
[(RAC

Solution

Excerpts from answers to my question on RAC repo:

For the first question -


Subjects are naturally multicasted, so you could replace the multicast connection property with @property RACReplaySubject *localeSubject and subscribe directly to that in place of localeSignal.

So there's no need to use connection here.

For the second part - it's enough to have RACSignal in interface and RAC(Replay)Subject in the implementation.

Context

StackExchange Code Review Q#83072, answer score: 3

Revisions (0)

No revisions yet.