回答編集履歴
1
回答を修正した。
test
CHANGED
@@ -10,20 +10,160 @@
|
|
10
10
|
|
11
11
|
メインスレッドに戻る前に、データの読み込みが終わったかどう確認してみてください。
|
12
12
|
|
13
|
+
|
14
|
+
|
15
|
+
GCPの`DispatchSemaphore`を使えば、上手く行けるはずです。
|
16
|
+
|
17
|
+
以下は、APIコールを待っている処理の事例です。
|
18
|
+
|
19
|
+
|
20
|
+
|
13
21
|
```
|
14
22
|
|
15
|
-
|
23
|
+
import Foundation
|
16
24
|
|
17
|
-
|
25
|
+
class MyTest {
|
18
26
|
|
19
|
-
|
27
|
+
func runTheShow() {
|
20
28
|
|
21
|
-
|
29
|
+
DispatchQueue.global(qos: .utility).async {
|
22
30
|
|
31
|
+
let json: Teratail? = self.fetchQuestion()
|
32
|
+
|
23
|
-
|
33
|
+
DispatchQueue.main.async {
|
34
|
+
|
35
|
+
print(json)
|
24
36
|
|
25
37
|
}
|
26
38
|
|
39
|
+
}
|
40
|
+
|
41
|
+
}
|
42
|
+
|
43
|
+
|
44
|
+
|
45
|
+
func fetchQuestion() -> Teratail? {
|
46
|
+
|
47
|
+
var myresponse: Teratail? = nil
|
48
|
+
|
49
|
+
guard let url = URL(string: "https://teratail.com/api/v1/questions") else {return nil}
|
50
|
+
|
51
|
+
let semaphore = DispatchSemaphore(value: 0)
|
52
|
+
|
53
|
+
let task = URLSession.shared.dataTask(with: url) { (data, response, error) in
|
54
|
+
|
55
|
+
|
56
|
+
|
57
|
+
do {
|
58
|
+
|
59
|
+
guard let data = data else {return}
|
60
|
+
|
61
|
+
|
62
|
+
|
63
|
+
let json = try JSONDecoder().decode(Teratail.self, from: data)
|
64
|
+
|
65
|
+
|
66
|
+
|
67
|
+
myresponse = json
|
68
|
+
|
69
|
+
}
|
70
|
+
|
71
|
+
catch {
|
72
|
+
|
73
|
+
print("error:", error)
|
74
|
+
|
75
|
+
}
|
76
|
+
|
77
|
+
semaphore.signal()
|
78
|
+
|
79
|
+
}
|
80
|
+
|
81
|
+
|
82
|
+
|
83
|
+
task.resume()
|
84
|
+
|
85
|
+
_ = semaphore.wait(wallTimeout: .distantFuture)
|
86
|
+
|
87
|
+
return myresponse
|
88
|
+
|
89
|
+
}
|
90
|
+
|
27
91
|
}
|
28
92
|
|
93
|
+
|
94
|
+
|
95
|
+
struct Teratail: Decodable {
|
96
|
+
|
97
|
+
let meta: Meta
|
98
|
+
|
99
|
+
let questions: [Question]
|
100
|
+
|
101
|
+
}
|
102
|
+
|
103
|
+
|
104
|
+
|
105
|
+
struct Meta: Decodable {
|
106
|
+
|
107
|
+
let hit_num: Int
|
108
|
+
|
109
|
+
let limit: Int
|
110
|
+
|
111
|
+
let message: String
|
112
|
+
|
113
|
+
let page: Int
|
114
|
+
|
115
|
+
let total_page: Int
|
116
|
+
|
117
|
+
}
|
118
|
+
|
119
|
+
|
120
|
+
|
121
|
+
struct Question : Decodable {
|
122
|
+
|
123
|
+
let count_clip: Int
|
124
|
+
|
125
|
+
let count_pv : Int
|
126
|
+
|
127
|
+
let count_reply: Int
|
128
|
+
|
129
|
+
let created: String
|
130
|
+
|
131
|
+
let modified: String
|
132
|
+
|
133
|
+
let id: Int
|
134
|
+
|
135
|
+
let is_accepted: Bool
|
136
|
+
|
137
|
+
let is_beginner: Bool
|
138
|
+
|
139
|
+
let is_presentation: Bool
|
140
|
+
|
141
|
+
let tags: [String]
|
142
|
+
|
143
|
+
let title: String
|
144
|
+
|
145
|
+
let user: User
|
146
|
+
|
147
|
+
}
|
148
|
+
|
149
|
+
|
150
|
+
|
151
|
+
struct User: Decodable {
|
152
|
+
|
153
|
+
let display_name: String
|
154
|
+
|
155
|
+
let photo: String
|
156
|
+
|
157
|
+
let score: Int
|
158
|
+
|
159
|
+
}
|
160
|
+
|
161
|
+
|
162
|
+
|
163
|
+
let obj = MyTest()
|
164
|
+
|
165
|
+
obj.runTheShow()
|
166
|
+
|
167
|
+
|
168
|
+
|
29
169
|
```
|