Skip to content

Commit c45275b

Browse files
authored
Merge pull request #162 from getsentry/bugfix/async-operation
Fix use internal vars in async operation
2 parents 8431e75 + ac2acbc commit c45275b

File tree

1 file changed

+43
-43
lines changed

1 file changed

+43
-43
lines changed

Sources/RequestOperation.swift

Lines changed: 43 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -9,22 +9,22 @@
99
import Foundation
1010

1111
class RequestOperation: AsynchronousOperation {
12-
12+
1313
var task: URLSessionTask?
1414
var request: URLRequest?
15-
15+
1616
init(session: URLSession, request: URLRequest, finished: SentryEndpointRequestFinished? = nil) {
1717
super.init()
18-
18+
1919
self.request = request
2020
#if swift(>=3.0)
21-
task = session.dataTask(with: request) { data, response, error in
21+
task = session.dataTask(with: request) { [weak self] data, response, error in
2222
defer {
23-
self.completeOperation()
23+
self?.completeOperation()
2424
}
25-
25+
2626
var success = false
27-
27+
2828
// Returns success if we have data and 200 response code
2929
if let data = data, let response = response as? HTTPURLResponse {
3030
Log.Debug.log("status = \(response.statusCode)")
@@ -36,20 +36,20 @@ class RequestOperation: AsynchronousOperation {
3636
}
3737
if let error = error {
3838
Log.Error.log("error = \(error)")
39-
39+
4040
success = false
4141
}
42-
42+
4343
finished?(success)
4444
}
4545
#else
46-
task = session.dataTaskWithRequest(request) { data, response, error in
46+
task = session.dataTaskWithRequest(request) { [weak self] data, response, error in
4747
defer {
48-
self.completeOperation()
48+
self?.completeOperation()
4949
}
50-
50+
5151
var success = false
52-
52+
5353
// Returns success if we have data and 200 response code
5454
if let data = data, let response = response as? NSHTTPURLResponse {
5555
Log.Debug.log("status = \(response.statusCode)")
@@ -63,32 +63,32 @@ class RequestOperation: AsynchronousOperation {
6363
Log.Error.log("error = \(error)")
6464
success = false
6565
}
66-
66+
6767
finished?(success)
6868
}
6969
#endif
7070
}
71-
71+
7272
override func cancel() {
7373
if let task = task {
7474
task.cancel()
7575
}
7676
super.cancel()
7777
}
78-
78+
7979
override func main() {
8080
if let task = task { task.resume() }
8181
}
82-
82+
8383
}
8484

8585
#if swift(>=3.0)
8686
class AsynchronousOperation: Operation {
87-
87+
8888
override public var isAsynchronous: Bool { return true }
89-
89+
9090
private let stateLock = NSLock()
91-
91+
9292
private var _executing: Bool = false
9393
override private(set) public var isExecuting: Bool {
9494
get {
@@ -100,7 +100,7 @@ class AsynchronousOperation: Operation {
100100
didChangeValue(forKey: "isExecuting")
101101
}
102102
}
103-
103+
104104
private var _finished: Bool = false
105105
override private(set) public var isFinished: Bool {
106106
get {
@@ -112,37 +112,37 @@ class AsynchronousOperation: Operation {
112112
didChangeValue(forKey: "isFinished")
113113
}
114114
}
115-
115+
116116
/// Complete the operation
117117
///
118118
/// This will result in the appropriate KVN of isFinished and isExecuting
119119
public func completeOperation() {
120-
if isExecuting { _executing = false }
121-
if !isFinished { _finished = true }
120+
if isExecuting { isExecuting = false }
121+
if !isFinished { isFinished = true }
122122
}
123-
123+
124124
override public func start() {
125125
if isCancelled {
126-
_finished = true
126+
isFinished = true
127127
return
128128
}
129-
130-
_executing = true
131-
129+
130+
isExecuting = true
131+
132132
main()
133133
}
134-
134+
135135
override public func main() {
136136
fatalError("subclasses must override `main`")
137137
}
138138
}
139139
#else
140140
public class AsynchronousOperation : NSOperation {
141-
141+
142142
override public var asynchronous: Bool { return true }
143-
143+
144144
private let stateLock = NSLock()
145-
145+
146146
private var _executing: Bool = false
147147
override private(set) public var executing: Bool {
148148
get {
@@ -154,7 +154,7 @@ public class AsynchronousOperation : NSOperation {
154154
didChangeValueForKey("isExecuting")
155155
}
156156
}
157-
157+
158158
private var _finished: Bool = false
159159
override private(set) public var finished: Bool {
160160
get {
@@ -166,46 +166,46 @@ public class AsynchronousOperation : NSOperation {
166166
didChangeValueForKey("isFinished")
167167
}
168168
}
169-
169+
170170
/// Complete the operation
171171
///
172172
/// This will result in the appropriate KVN of isFinished and isExecuting
173-
173+
174174
public func completeOperation() {
175175
if executing {
176176
executing = false
177177
}
178-
178+
179179
if !finished {
180180
finished = true
181181
}
182182
}
183-
183+
184184
override public func start() {
185185
if cancelled {
186186
finished = true
187187
return
188188
}
189-
189+
190190
executing = true
191-
191+
192192
main()
193193
}
194-
194+
195195
override public func main() {
196196
fatalError("subclasses must override `main`")
197197
}
198198
}
199199
#endif
200200

201201
extension NSLock {
202-
202+
203203
/// Perform closure within lock.
204204
///
205205
/// An extension to `NSLock` to simplify executing critical code.
206206
///
207207
/// - parameter block: The closure to be performed.
208-
208+
209209
func withCriticalScope<T>( block: (Void) -> T) -> T {
210210
lock()
211211
let value = block()

0 commit comments

Comments
 (0)