SDAsyncBlockOperation.m 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. /*
  2. * This file is part of the SDWebImage package.
  3. * (c) Olivier Poitrey <rs@dailymotion.com>
  4. *
  5. * For the full copyright and license information, please view the LICENSE
  6. * file that was distributed with this source code.
  7. */
  8. #import "SDAsyncBlockOperation.h"
  9. @interface SDAsyncBlockOperation ()
  10. @property (assign, nonatomic, getter = isExecuting) BOOL executing;
  11. @property (assign, nonatomic, getter = isFinished) BOOL finished;
  12. @property (nonatomic, copy, nonnull) SDAsyncBlock executionBlock;
  13. @end
  14. @implementation SDAsyncBlockOperation
  15. @synthesize executing = _executing;
  16. @synthesize finished = _finished;
  17. - (nonnull instancetype)initWithBlock:(nonnull SDAsyncBlock)block {
  18. self = [super init];
  19. if (self) {
  20. self.executionBlock = block;
  21. }
  22. return self;
  23. }
  24. + (nonnull instancetype)blockOperationWithBlock:(nonnull SDAsyncBlock)block {
  25. SDAsyncBlockOperation *operation = [[SDAsyncBlockOperation alloc] initWithBlock:block];
  26. return operation;
  27. }
  28. - (void)start {
  29. if (self.isCancelled) {
  30. return;
  31. }
  32. [self willChangeValueForKey:@"isExecuting"];
  33. self.executing = YES;
  34. [self didChangeValueForKey:@"isExecuting"];
  35. if (self.executionBlock) {
  36. self.executionBlock(self);
  37. } else {
  38. [self complete];
  39. }
  40. }
  41. - (void)cancel {
  42. [super cancel];
  43. [self complete];
  44. }
  45. - (void)complete {
  46. [self willChangeValueForKey:@"isExecuting"];
  47. [self willChangeValueForKey:@"isFinished"];
  48. self.executing = NO;
  49. self.finished = YES;
  50. [self didChangeValueForKey:@"isExecuting"];
  51. [self didChangeValueForKey:@"isFinished"];
  52. }
  53. @end