- If there is no primary, I'd like to return the first record encountered, whether it's primary or not, and
- If there aren't any records encountered at all, I'd like to return a default image.
This isn't using your original code, but extracting out the basics of what you are asking for, here is one way to compose signals to get what you want:
MyImageModel *myDefaultImageModel = [self createDefaultImageModel];
RACSignal *images = [self fetchImagesOrWhatever];
RACSignal *primaryImages = [images filter:^(MyImageModel *im) { return im.isPrimary; }];
RACSignal *defaultImage = [RACSignal return:myDefaultImageModel];
// This is the line to pay attention to:
RACSignal *finalImage = [[[primaryImages concat:[images concat:defaultImage]]] take:1];
RAC(self, imageView.image) = [finalImage map:^(MyImageModel *im) {
// Convert MyImageModel to a UIImage here, and return it.
}];
This code constructs one signal, called finalImage
, that consists of the following values, in order:
- Every image that is marked "primary".
- The first image (from the same signal of images that fed #1).
- A default image.
and then uses -[RACSignal take:1]
to take the first of these. If #1 or #2 could be empty signals (because there are no primaries or no images at all), you know that you will at least get #3.