ios - Reactive Cocoa and multiple AFNetworking requests in short period of time -
i'm struggeling way handle multiple requests web service afnetworking , reactive cocoa. in scenario user asking api deliver bunch of suggestions character/integer search input pick city out of list.
here's code:
first method gets executed user types in more 3 characters/integers
- (void)fetchdata:(nsstring *)searchtext { nslog(@"%@", searchtext); if ([searchtext validatestringwithpattern:bkpostcoderegex]) { self.searchurl = [nsstring stringwithformat:@"%@location/postalcode/%@/%@", bkbaseurl, self.countrycode, searchtext]; } else if ([searchtext validatestringwithpattern:bkcityregex]) { self.searchurl = [nsstring stringwithformat:@"%@location/city/%@/%@", bkbaseurl, self.countrycode, searchtext]; } else { nslog(@"error alert - no valid input"); return; } rac(self, searchresults) = [[[self postrequest] map:^(nsdictionary *json) { nsarray *results = json[@"data"][@"locations"]; return results; }] catch:^(nserror *error) { return [racsignal return:@[]]; }]; }
now create signal , pass self.searchresults
:
- (racsignal *)postrequest { return [[[racsignal createsignal:^racdisposable *(id<racsubscriber> subscriber) { [self.requestoperationmanager get:self.searchurl parameters:self.params success:^(afhttprequestoperation *operation, id responseobject) { [subscriber sendnext:responseobject]; [subscriber sendcompleted]; }failure:^(afhttprequestoperation *operation, nserror *error) { [subscriber senderror:error]; }]; return [racdisposable disposablewithblock:^{ [self.requestoperationmanager.operationqueue cancelalloperations]; }]; }] doerror:^(nserror *error) { nslog(@"error: %@", [error description]); }] throttle:0.5]; }
i think problem is, start subscribing signal before signal prior current signal has been completed , cause exception i'm trying subscribe again.
* terminating app due uncaught exception 'nsinternalinconsistencyexception', reason: 'signal name: [[[[+createsignal:] -doerror:] -throttle: 0.500000] -map:] -catch: bound key path "searchresults" on object , adding signal name: [[[[+createsignal:] -doerror:] -throttle: 0.500000] -map:] -catch: undefined behavior'
my guess have try in postrequest method doesn't seem work out of box:
if (self.requestoperationmanager.operationqueue.operationcount == 1) { nslog(@"cancel operations"); [self.requestoperationmanager.operationqueue cancelalloperations]; [subscriber sendcompleted]; }
you can call rac()
once on given object's key path. looks -fetchdata:
can called more once, result in rac()
being called multiple times on same object , key path.
typically, call rac()
in sort of setup method (such initializer or -[uiviewcontroller loadview]
) gets called once. instead of waiting until user types more 3 characters , calling -fetchdata:
, think how create signal sends value when user types more 3 characters, , assign signal rac()
'd property. example (completely untested):
- (void)someinitializationmethod { racsignal *morethan3 = [[mytextfield rac_textsignal] filter:^(nsstring *text) { return [text length] > 2; }]; rac(self, searchresults) = [self rac_liftselector:@selector(fetchdata:) withsignals:morethan3]; } - (void)fetchdata:(nsstring *)searchtext { if ([searchtext validatestringwithpattern:bkpostcoderegex]) { // ... etc ... } return [[[self postrequest] map:^(nsdictionary *json) { nsarray *results = json[@"data"][@"locations"]; return results; }] catch:^(nserror *error) { return [racsignal return:@[]]; }]; } - (racsignal *)postrequest { // ... etc ...
Comments
Post a Comment