SDAsyncBlockOperation.m 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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. @synchronized (self) {
  30. if (self.isCancelled) {
  31. self.finished = YES;
  32. return;
  33. }
  34. self.finished = NO;
  35. self.executing = YES;
  36. if (self.executionBlock) {
  37. self.executionBlock(self);
  38. } else {
  39. self.executing = NO;
  40. self.finished = YES;
  41. }
  42. }
  43. }
  44. - (void)cancel {
  45. @synchronized (self) {
  46. [super cancel];
  47. if (self.isExecuting) {
  48. self.executing = NO;
  49. self.finished = YES;
  50. }
  51. }
  52. }
  53. - (void)complete {
  54. @synchronized (self) {
  55. if (self.isExecuting) {
  56. self.finished = YES;
  57. self.executing = NO;
  58. }
  59. }
  60. }
  61. - (void)setFinished:(BOOL)finished {
  62. [self willChangeValueForKey:@"isFinished"];
  63. _finished = finished;
  64. [self didChangeValueForKey:@"isFinished"];
  65. }
  66. - (void)setExecuting:(BOOL)executing {
  67. [self willChangeValueForKey:@"isExecuting"];
  68. _executing = executing;
  69. [self didChangeValueForKey:@"isExecuting"];
  70. }
  71. - (BOOL)isConcurrent {
  72. return YES;
  73. }
  74. @end