ios - ReactiveCocoa: sendNext to a subscriber in asynchronous programming -
i trying utilize reactivecococa network connection using nsinputstream & nsoutputstream . connect code looks follows:
-(racsignal*) connect: (nsstring *) url { homecoming [racsignal createsignal:^racdisposable *(id<racsubscriber> thesubscriber) { self.subscriber = thesubscriber; // create inputstream , outputstream, initialize , open them [self.inputstream open] [self.outputstream open]; }]; homecoming nil; } -(void) stream:(nsstream *)astream handleevent:(nsstreamevent)eventcode { switch (eventcode) { case nsstreameventhasbytesavailable: //read input stream nsarray * info = read_inputstream; [subscriber sendnext:data]; break; } ... }
i have store value of subscriber , phone call sendnext on it, when receive info in callback method.
is there improve way handle in reactivecocoa , avoid declaring subscriber property. besides, not work multiple subscribers.
you can utilize rac_signalforselector
turn delegate callback method signal. can subscribe signal within createsignal
's didsubscribe
block. this:
- (racsignal*)connect:(nsstring *)url { homecoming [racsignal createsignal:^racdisposable*(id<racsubscriber> thesubscriber) { // create inputstream , outputstream, initialize , open them [self.inputstream open]; [self.outputstream open]; [[self rac_signalforselector:@selector(stream:handleevent:) fromprotocol:@protocol(nsstreamdelegate)] subscribenext:^(ractuple *tuple) { nsstream *astream = (nsstream *)tuple.first; nsstreamevent *eventcode = (nsstreamevent *)tuple.second; // check eventcode here nsarray *data = read_inputstream; [thesubscriber sendnext:data]; }]; homecoming nil; }]; } - (void)stream:(nsstream *)astream handleevent:(nsstreamevent)eventcode { }
when using rac_signalforselector
, signal pass arguments method through tuple can @ decide action take.
depending on you're trying accomplish here, there more elegant reactive solutions, rac_signalforselector
@ to the lowest degree solves problem of needing subscriber property.
ios reactive-cocoa
No comments:
Post a Comment