質問編集履歴
3
記載ミスの修正
test
CHANGED
File without changes
|
test
CHANGED
@@ -207,61 +207,3 @@
|
|
207
207
|
###補足情報(言語/FW/ツール等のバージョンなど)
|
208
208
|
|
209
209
|
【swift3】【Xcode8.2.1】
|
210
|
-
|
211
|
-
|
212
|
-
|
213
|
-
|
214
|
-
|
215
|
-
###セマフォ(デッドロック)
|
216
|
-
|
217
|
-
```Swift
|
218
|
-
|
219
|
-
import UIKit
|
220
|
-
|
221
|
-
import Alamofire
|
222
|
-
|
223
|
-
|
224
|
-
|
225
|
-
class ViewController: UIViewController {
|
226
|
-
|
227
|
-
|
228
|
-
|
229
|
-
let semaphore = DispatchSemaphore(value: 0)
|
230
|
-
|
231
|
-
|
232
|
-
|
233
|
-
override func viewDidLoad() {
|
234
|
-
|
235
|
-
super.viewDidLoad()
|
236
|
-
|
237
|
-
// Do any additional setup after loading the view, typically from a nib.
|
238
|
-
|
239
|
-
test()
|
240
|
-
|
241
|
-
}
|
242
|
-
|
243
|
-
|
244
|
-
|
245
|
-
func test() {
|
246
|
-
|
247
|
-
Alamofire.request("https://qiita.com/api/v2/items", method: .get).responseString { response in
|
248
|
-
|
249
|
-
guard let object = response.result.value else { return }
|
250
|
-
|
251
|
-
print(object)
|
252
|
-
|
253
|
-
self.semaphore.signal()
|
254
|
-
|
255
|
-
}
|
256
|
-
|
257
|
-
print("waiting...")
|
258
|
-
|
259
|
-
semaphore.wait()
|
260
|
-
|
261
|
-
print("finish")
|
262
|
-
|
263
|
-
}
|
264
|
-
|
265
|
-
}
|
266
|
-
|
267
|
-
```
|
2
デッドロック
test
CHANGED
File without changes
|
test
CHANGED
@@ -212,13 +212,23 @@
|
|
212
212
|
|
213
213
|
|
214
214
|
|
215
|
-
###
|
215
|
+
###セマフォ(デッドロック)
|
216
216
|
|
217
217
|
```Swift
|
218
218
|
|
219
|
+
import UIKit
|
220
|
+
|
221
|
+
import Alamofire
|
222
|
+
|
223
|
+
|
224
|
+
|
219
225
|
class ViewController: UIViewController {
|
220
226
|
|
227
|
+
|
228
|
+
|
221
|
-
|
229
|
+
let semaphore = DispatchSemaphore(value: 0)
|
230
|
+
|
231
|
+
|
222
232
|
|
223
233
|
override func viewDidLoad() {
|
224
234
|
|
@@ -226,57 +236,31 @@
|
|
226
236
|
|
227
237
|
// Do any additional setup after loading the view, typically from a nib.
|
228
238
|
|
229
|
-
DispatchQueue(label: "test").async {
|
230
|
-
|
231
|
-
|
239
|
+
test()
|
232
|
-
|
233
|
-
|
240
|
+
|
234
|
-
|
235
|
-
self.test2()
|
236
|
-
|
237
|
-
DispatchQueue(label: "test").async {
|
238
|
-
|
239
|
-
self.test3()
|
240
|
-
|
241
|
-
|
241
|
+
}
|
242
|
-
|
243
|
-
|
242
|
+
|
244
|
-
|
245
|
-
|
243
|
+
|
246
|
-
|
247
|
-
|
244
|
+
|
248
|
-
|
249
|
-
|
250
|
-
|
251
|
-
func test
|
245
|
+
func test() {
|
252
|
-
|
253
|
-
|
246
|
+
|
254
|
-
|
255
|
-
}
|
256
|
-
|
257
|
-
|
258
|
-
|
259
|
-
func test2() {
|
260
|
-
|
261
|
-
print("test2-")
|
262
|
-
|
263
|
-
}
|
264
|
-
|
265
|
-
|
266
|
-
|
267
|
-
func test3() {
|
268
|
-
|
269
|
-
Alamofire.request("https://qiita.com/api/v2/items", method: .get).response
|
247
|
+
Alamofire.request("https://qiita.com/api/v2/items", method: .get).responseString { response in
|
270
248
|
|
271
249
|
guard let object = response.result.value else { return }
|
272
250
|
|
251
|
+
print(object)
|
252
|
+
|
273
|
-
|
253
|
+
self.semaphore.signal()
|
274
|
-
|
254
|
+
|
275
|
-
}
|
255
|
+
}
|
256
|
+
|
276
|
-
|
257
|
+
print("waiting...")
|
258
|
+
|
259
|
+
semaphore.wait()
|
260
|
+
|
261
|
+
print("finish")
|
262
|
+
|
277
|
-
}
|
263
|
+
}
|
278
|
-
|
279
|
-
|
280
264
|
|
281
265
|
}
|
282
266
|
|
1
GCDサンプル追加?
test
CHANGED
File without changes
|
test
CHANGED
@@ -207,3 +207,77 @@
|
|
207
207
|
###補足情報(言語/FW/ツール等のバージョンなど)
|
208
208
|
|
209
209
|
【swift3】【Xcode8.2.1】
|
210
|
+
|
211
|
+
|
212
|
+
|
213
|
+
|
214
|
+
|
215
|
+
###GCD?
|
216
|
+
|
217
|
+
```Swift
|
218
|
+
|
219
|
+
class ViewController: UIViewController {
|
220
|
+
|
221
|
+
|
222
|
+
|
223
|
+
override func viewDidLoad() {
|
224
|
+
|
225
|
+
super.viewDidLoad()
|
226
|
+
|
227
|
+
// Do any additional setup after loading the view, typically from a nib.
|
228
|
+
|
229
|
+
DispatchQueue(label: "test").async {
|
230
|
+
|
231
|
+
self.test1()
|
232
|
+
|
233
|
+
DispatchQueue(label: "test").asyncAfter(deadline: .now() + 5) {
|
234
|
+
|
235
|
+
self.test2()
|
236
|
+
|
237
|
+
DispatchQueue(label: "test").async {
|
238
|
+
|
239
|
+
self.test3()
|
240
|
+
|
241
|
+
}
|
242
|
+
|
243
|
+
}
|
244
|
+
|
245
|
+
}
|
246
|
+
|
247
|
+
}
|
248
|
+
|
249
|
+
|
250
|
+
|
251
|
+
func test1() {
|
252
|
+
|
253
|
+
print("test1")
|
254
|
+
|
255
|
+
}
|
256
|
+
|
257
|
+
|
258
|
+
|
259
|
+
func test2() {
|
260
|
+
|
261
|
+
print("test2-")
|
262
|
+
|
263
|
+
}
|
264
|
+
|
265
|
+
|
266
|
+
|
267
|
+
func test3() {
|
268
|
+
|
269
|
+
Alamofire.request("https://qiita.com/api/v2/items", method: .get).responseJSON { response in
|
270
|
+
|
271
|
+
guard let object = response.result.value else { return }
|
272
|
+
|
273
|
+
//この形だと値を返せない?
|
274
|
+
|
275
|
+
}
|
276
|
+
|
277
|
+
}
|
278
|
+
|
279
|
+
|
280
|
+
|
281
|
+
}
|
282
|
+
|
283
|
+
```
|