objective c - Providing a second choice if primary choice not found in a ReactiveCocoa signal chain -


i have following code runs through series of image records, tries find record that's marked "primary", loads it, , assigns uiimageview:

    // when there's new image, fetch it, , set headerview (which default uiimageview) rac( self, imageview.image ) =      // return sequence photos     [[[[[[self modelimagessignal] ignore:nil] flattenmap:^racstream *(nsdictionary *photos)         {         nslog(@"got photos: %@" , photos) ;         return photos.rac_sequence.signal ;         }]      // consider each photo     filter:^bool(nsdictionary *photodescriptor)         {         nslog(@"descriptor: %@" , photodescriptor) ;         return ((nsnumber *)photodescriptor[@"primary"]).boolvalue ;         }]      // load selected photo     flattenmap:^racstream *(nsdictionary *selectedphotodescriptor)         {         nslog(@"selected photo desc: %@" , selectedphotodescriptor) ;         return [asyncimagefetcher imageaturl:[nsurl urlwithstring:selectedphotodescriptor[@"url"]] cache:yes] ; // -deliveron: main thread         }]      // catch errors     catch:^racsignal *(nserror *error)         {         flash_report_t(error.description, nil, xkflashmessagetypeerror) ;         return [racsignal empty] ;         }] ; 
  1. if there no primary, i'd return first record encountered, whether it's primary or not, and
  2. if there aren't records encountered @ all, i'd return default image.

how can these?

  1. if there no primary, i'd return first record encountered, whether it's primary or not, and
  2. if there aren't records encountered @ all, i'd return default image.

this isn't using original code, extracting out basics of asking for, here 1 way compose signals want:

myimagemodel *mydefaultimagemodel = [self createdefaultimagemodel];  racsignal *images = [self fetchimagesorwhatever]; racsignal *primaryimages = [images filter:^(myimagemodel *im) { return im.isprimary; }]; racsignal *defaultimage = [racsignal return:mydefaultimagemodel];  // line pay attention to: racsignal *finalimage = [[[primaryimages concat:[images concat:defaultimage]]] take:1];  rac(self, imageview.image) = [finalimage map:^(myimagemodel *im) {     // convert myimagemodel uiimage here, , return it. }]; 

this code constructs 1 signal, called finalimage, consists of following values, in order:

  1. every image marked "primary".
  2. the first image (from same signal of images fed #1).
  3. a default image.

and uses -[racsignal take:1] take first of these. if #1 or #2 empty signals (because there no primaries or no images @ all), know @ least #3.


Comments

Popular posts from this blog

user interface - How to replace the Python logo in a Tkinter-based Python GUI app? -

objective c - Greedy NSProgressIndicator Allocation -

how to set an OCR language in Google Drive -