Proyectos de Subversion Iphone Microlearning

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 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
 
15
#import "Crashlytics/Shared/FIRCLSOperation/FIRCLSFABAsyncOperation.h"
16
 
17
#import "Crashlytics/Shared/FIRCLSOperation/FIRCLSFABAsyncOperation_Private.h"
18
 
19
@interface FIRCLSFABAsyncOperation () {
20
  BOOL _internalExecuting;
21
  BOOL _internalFinished;
22
}
23
 
24
@property(nonatomic, strong) NSRecursiveLock *lock;
25
 
26
@end
27
 
28
@implementation FIRCLSFABAsyncOperation
29
 
30
- (instancetype)init {
31
  self = [super init];
32
  if (!self) {
33
    return nil;
34
  }
35
 
36
  _internalExecuting = NO;
37
  _internalFinished = NO;
38
 
39
  _lock = [[NSRecursiveLock alloc] init];
40
  _lock.name = [NSString stringWithFormat:@"com.google.firebase.crashlytics.%@-lock", [self class]];
41
  ;
42
 
43
  return self;
44
}
45
 
46
#pragma mark - NSOperation Overrides
47
- (BOOL)isConcurrent {
48
  return YES;
49
}
50
 
51
- (BOOL)isAsynchronous {
52
  return YES;
53
}
54
 
55
- (BOOL)isExecuting {
56
  [self.lock lock];
57
  BOOL result = _internalExecuting;
58
  [self.lock unlock];
59
 
60
  return result;
61
}
62
 
63
- (BOOL)isFinished {
64
  [self.lock lock];
65
  BOOL result = _internalFinished;
66
  [self.lock unlock];
67
 
68
  return result;
69
}
70
 
71
- (void)start {
72
  if ([self checkForCancellation]) {
73
    return;
74
  }
75
 
76
  [self markStarted];
77
 
78
  [self main];
79
}
80
 
81
#pragma mark - Utilities
82
- (void)changeValueForKey:(NSString *)key inBlock:(void (^)(void))block {
83
  [self willChangeValueForKey:key];
84
  block();
85
  [self didChangeValueForKey:key];
86
}
87
 
88
- (void)lock:(void (^)(void))block {
89
  [self.lock lock];
90
  block();
91
  [self.lock unlock];
92
}
93
 
94
- (BOOL)checkForCancellation {
95
  if ([self isCancelled]) {
96
    [self markDone];
97
    return YES;
98
  }
99
 
100
  return NO;
101
}
102
 
103
#pragma mark - State Management
104
- (void)unlockedMarkFinished {
105
  [self changeValueForKey:@"isFinished"
106
                  inBlock:^{
107
                    self->_internalFinished = YES;
108
                  }];
109
}
110
 
111
- (void)unlockedMarkStarted {
112
  [self changeValueForKey:@"isExecuting"
113
                  inBlock:^{
114
                    self->_internalExecuting = YES;
115
                  }];
116
}
117
 
118
- (void)unlockedMarkComplete {
119
  [self changeValueForKey:@"isExecuting"
120
                  inBlock:^{
121
                    self->_internalExecuting = NO;
122
                  }];
123
}
124
 
125
- (void)markStarted {
126
  [self lock:^{
127
    [self unlockedMarkStarted];
128
  }];
129
}
130
 
131
- (void)markDone {
132
  [self lock:^{
133
    [self unlockedMarkComplete];
134
    [self unlockedMarkFinished];
135
  }];
136
}
137
 
138
#pragma mark - Protected
139
- (void)finishWithError:(NSError *)error {
140
  if (self.asyncCompletion) {
141
    self.asyncCompletion(error);
142
  }
143
  [self markDone];
144
}
145
 
146
@end