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