NSData+RACSupport.m 917 B

1234567891011121314151617181920212223242526272829303132333435
  1. //
  2. // NSData+RACSupport.m
  3. // ReactiveObjC
  4. //
  5. // Created by Josh Abernathy on 5/11/12.
  6. // Copyright (c) 2012 GitHub, Inc. All rights reserved.
  7. //
  8. #import "NSData+RACSupport.h"
  9. #import "RACReplaySubject.h"
  10. #import "RACScheduler.h"
  11. @implementation NSData (RACSupport)
  12. + (RACSignal *)rac_readContentsOfURL:(NSURL *)URL options:(NSDataReadingOptions)options scheduler:(RACScheduler *)scheduler {
  13. NSCParameterAssert(scheduler != nil);
  14. RACReplaySubject *subject = [RACReplaySubject subject];
  15. [subject setNameWithFormat:@"+rac_readContentsOfURL: %@ options: %lu scheduler: %@", URL, (unsigned long)options, scheduler];
  16. [scheduler schedule:^{
  17. NSError *error = nil;
  18. NSData *data = [[NSData alloc] initWithContentsOfURL:URL options:options error:&error];
  19. if (data == nil) {
  20. [subject sendError:error];
  21. } else {
  22. [subject sendNext:data];
  23. [subject sendCompleted];
  24. }
  25. }];
  26. return subject;
  27. }
  28. @end