Skip to content

Instantly share code, notes, and snippets.

@hsavit1
Last active August 29, 2015 14:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hsavit1/a4da71c406eea09b5173 to your computer and use it in GitHub Desktop.
Save hsavit1/a4da71c406eea09b5173 to your computer and use it in GitHub Desktop.
// creates a signal that fetches an image in the background, delivering
// it on the UI thread. This signal 'cancels' itself if the cell is re-used before the
// image is downloaded.
-(RACSignal *)signalForImage:(NSURL *)imageUrl {
RACScheduler *scheduler = [RACScheduler schedulerWithPriority:RACSchedulerPriorityBackground];
RACSignal *imageDownloadSignal = [[RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
NSData *data = [NSData dataWithContentsOfURL:imageUrl];
UIImage *image = [UIImage imageWithData:data];
//now we can tell the SUBSCRIBER to do something!! This is the key with the signals
[subscriber sendNext:image];
[subscriber sendCompleted];
return nil;
}] subscribeOn:scheduler];
return [[imageDownloadSignal
takeUntil:self.rac_prepareForReuseSignal]
deliverOn:[RACScheduler mainThreadScheduler]];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment