objective c - How do I postpone a message send until a signal is subscribed to with -catchTo:? -
ok, know how create looping retry i'm stuck on problem. think know why, can't seem figure out how. in code below have sequence of events collect location. reverse geocode , compare list of allowed country codes. current problem alert asking user allow location services app display if no error have been sent. think because call [self retrywithalert] called straight away when sequence set , shouldn't place code creating , showing alert is. when try wrap in [racsignal create:] can't seem right. , without further ado, here code:
first sequence itself
_locationmanager = [[cllocationmanager alloc] init]; [_locationmanager setdesiredaccuracy:kcllocationaccuracykilometer]; @weakify( self ) [[[[[_locationmanager rac_fetchlocationsignal] catchto:[self retrywithalert]] flattenmap:^racstream *( cllocation *newlocation ) { @strongify( self ) return [self reversegeocodelocation:newlocation]; }] flattenmap:^racstream *( nsstring *isocountrycode ) { @strongify( self ) return [self checkcountrypermissiblesignal:isocountrycode]; }] subscribenext:^( id x ) { [_locationmanager stopupdatinglocation]; nslog( @"country valid!" ); } error:^( nserror *error ) { nslog( @"error: %@", error ); }]; [_locationmanager startupdatinglocation];
and here code [self retrywithalert]
- (racsignal *)retrywithalert { uialertview *retryalert = [[uialertview alloc] initwithtitle:@"trouble" message:@"make sure location services enabled , app allowed use them." delegate:self cancelbuttontitle:@"retry" otherbuttontitles:nil]; [retryalert show]; return [[[retryalert rac_buttonclickedsignal] flattenmap:^racstream *( id value ) { [_locationmanager startupdatinglocation]; return [_locationmanager rac_fetchlocationsignal]; }] catch:^racsignal *( nserror *error ) { return [self retrywithalert]; }]; }
you're close. noted, problem -retrywithalert
message sent when code runs. happens because message send part of code constructs signal chain in first place. want message sent when catchto
signal triggered.
what think want, instead of sending -retrywithalert
immediately, defer until catchto
'd signal subscribed (at point it's okay display alert). accomplished -[racsignal defer:]
, lets put -retrywithalert
message send in block isn't called until subscription occurs.
@weakify( self ) [[[[[[_locationmanager rac_fetchlocationsignal] catchto:[racsignal defer:^{ // <--- defer until subscription @strongify( self ); return [self retrywithalert]; }]] flattenmap:^racstream *( cllocation *newlocation ) { @strongify( self ) return [self reversegeocodelocation:newlocation]; }] ... (etc)
Comments
Post a Comment