| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146 |
- // Copyright 2019 Google
- //
- // Licensed under the Apache License, Version 2.0 (the "License");
- // you may not use this file except in compliance with the License.
- // You may obtain a copy of the License at
- //
- // http://www.apache.org/licenses/LICENSE-2.0
- //
- // Unless required by applicable law or agreed to in writing, software
- // distributed under the License is distributed on an "AS IS" BASIS,
- // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- // See the License for the specific language governing permissions and
- // limitations under the License.
- #import "Crashlytics/Shared/FIRCLSOperation/FIRCLSFABAsyncOperation.h"
- #import "Crashlytics/Shared/FIRCLSOperation/FIRCLSFABAsyncOperation_Private.h"
- @interface FIRCLSFABAsyncOperation () {
- BOOL _internalExecuting;
- BOOL _internalFinished;
- }
- @property(nonatomic, strong) NSRecursiveLock *lock;
- @end
- @implementation FIRCLSFABAsyncOperation
- - (instancetype)init {
- self = [super init];
- if (!self) {
- return nil;
- }
- _internalExecuting = NO;
- _internalFinished = NO;
- _lock = [[NSRecursiveLock alloc] init];
- _lock.name = [NSString stringWithFormat:@"com.google.firebase.crashlytics.%@-lock", [self class]];
- ;
- return self;
- }
- #pragma mark - NSOperation Overrides
- - (BOOL)isConcurrent {
- return YES;
- }
- - (BOOL)isAsynchronous {
- return YES;
- }
- - (BOOL)isExecuting {
- [self.lock lock];
- BOOL result = _internalExecuting;
- [self.lock unlock];
- return result;
- }
- - (BOOL)isFinished {
- [self.lock lock];
- BOOL result = _internalFinished;
- [self.lock unlock];
- return result;
- }
- - (void)start {
- if ([self checkForCancellation]) {
- return;
- }
- [self markStarted];
- [self main];
- }
- #pragma mark - Utilities
- - (void)changeValueForKey:(NSString *)key inBlock:(void (^)(void))block {
- [self willChangeValueForKey:key];
- block();
- [self didChangeValueForKey:key];
- }
- - (void)lock:(void (^)(void))block {
- [self.lock lock];
- block();
- [self.lock unlock];
- }
- - (BOOL)checkForCancellation {
- if ([self isCancelled]) {
- [self markDone];
- return YES;
- }
- return NO;
- }
- #pragma mark - State Management
- - (void)unlockedMarkFinished {
- [self changeValueForKey:@"isFinished"
- inBlock:^{
- self->_internalFinished = YES;
- }];
- }
- - (void)unlockedMarkStarted {
- [self changeValueForKey:@"isExecuting"
- inBlock:^{
- self->_internalExecuting = YES;
- }];
- }
- - (void)unlockedMarkComplete {
- [self changeValueForKey:@"isExecuting"
- inBlock:^{
- self->_internalExecuting = NO;
- }];
- }
- - (void)markStarted {
- [self lock:^{
- [self unlockedMarkStarted];
- }];
- }
- - (void)markDone {
- [self lock:^{
- [self unlockedMarkComplete];
- [self unlockedMarkFinished];
- }];
- }
- #pragma mark - Protected
- - (void)finishWithError:(NSError *)error {
- if (self.asyncCompletion) {
- self.asyncCompletion(error);
- }
- [self markDone];
- }
- @end
|