FIRCLSFABAsyncOperation.m 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. // Copyright 2019 Google
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #import "Crashlytics/Shared/FIRCLSOperation/FIRCLSFABAsyncOperation.h"
  15. #import "Crashlytics/Shared/FIRCLSOperation/FIRCLSFABAsyncOperation_Private.h"
  16. @interface FIRCLSFABAsyncOperation () {
  17. BOOL _internalExecuting;
  18. BOOL _internalFinished;
  19. }
  20. @property(nonatomic, strong) NSRecursiveLock *lock;
  21. @end
  22. @implementation FIRCLSFABAsyncOperation
  23. - (instancetype)init {
  24. self = [super init];
  25. if (!self) {
  26. return nil;
  27. }
  28. _internalExecuting = NO;
  29. _internalFinished = NO;
  30. _lock = [[NSRecursiveLock alloc] init];
  31. _lock.name = [NSString stringWithFormat:@"com.google.firebase.crashlytics.%@-lock", [self class]];
  32. ;
  33. return self;
  34. }
  35. #pragma mark - NSOperation Overrides
  36. - (BOOL)isConcurrent {
  37. return YES;
  38. }
  39. - (BOOL)isAsynchronous {
  40. return YES;
  41. }
  42. - (BOOL)isExecuting {
  43. [self.lock lock];
  44. BOOL result = _internalExecuting;
  45. [self.lock unlock];
  46. return result;
  47. }
  48. - (BOOL)isFinished {
  49. [self.lock lock];
  50. BOOL result = _internalFinished;
  51. [self.lock unlock];
  52. return result;
  53. }
  54. - (void)start {
  55. if ([self checkForCancellation]) {
  56. return;
  57. }
  58. [self markStarted];
  59. [self main];
  60. }
  61. #pragma mark - Utilities
  62. - (void)changeValueForKey:(NSString *)key inBlock:(void (^)(void))block {
  63. [self willChangeValueForKey:key];
  64. block();
  65. [self didChangeValueForKey:key];
  66. }
  67. - (void)lock:(void (^)(void))block {
  68. [self.lock lock];
  69. block();
  70. [self.lock unlock];
  71. }
  72. - (BOOL)checkForCancellation {
  73. if ([self isCancelled]) {
  74. [self markDone];
  75. return YES;
  76. }
  77. return NO;
  78. }
  79. #pragma mark - State Management
  80. - (void)unlockedMarkFinished {
  81. [self changeValueForKey:@"isFinished"
  82. inBlock:^{
  83. self->_internalFinished = YES;
  84. }];
  85. }
  86. - (void)unlockedMarkStarted {
  87. [self changeValueForKey:@"isExecuting"
  88. inBlock:^{
  89. self->_internalExecuting = YES;
  90. }];
  91. }
  92. - (void)unlockedMarkComplete {
  93. [self changeValueForKey:@"isExecuting"
  94. inBlock:^{
  95. self->_internalExecuting = NO;
  96. }];
  97. }
  98. - (void)markStarted {
  99. [self lock:^{
  100. [self unlockedMarkStarted];
  101. }];
  102. }
  103. - (void)markDone {
  104. [self lock:^{
  105. [self unlockedMarkComplete];
  106. [self unlockedMarkFinished];
  107. }];
  108. }
  109. #pragma mark - Protected
  110. - (void)finishWithError:(NSError *)error {
  111. if (self.asyncCompletion) {
  112. self.asyncCompletion(error);
  113. }
  114. [self markDone];
  115. }
  116. @end