質問編集履歴
2
回答者が答えやすいように質問とエラーをよりミニマルでシンプルになるよう修正
test
CHANGED
File without changes
|
test
CHANGED
@@ -18,27 +18,21 @@
|
|
18
18
|
|
19
19
|
```
|
20
20
|
|
21
|
-
|
21
|
+
|
22
|
-
|
23
|
-
|
24
|
-
|
22
|
+
|
25
|
-
|
23
|
+
警告①Constant 'lastTwoRecords' inferred to have type '()', which may be unexpected
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
24
|
+
|
30
|
-
|
31
|
-
|
32
|
-
|
25
|
+
|
26
|
+
|
33
|
-
|
27
|
+
警告②Expression of type '[RecordData]' is unused
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
28
|
+
|
38
|
-
|
39
|
-
|
40
|
-
|
29
|
+
|
30
|
+
|
41
|
-
|
31
|
+
エラー③Value of tuple type '()' has no member 'map'
|
32
|
+
|
33
|
+
|
34
|
+
|
35
|
+
```
|
42
36
|
|
43
37
|
|
44
38
|
|
@@ -46,276 +40,258 @@
|
|
46
40
|
|
47
41
|
|
48
42
|
|
43
|
+
### 該当のソースコード
|
44
|
+
|
45
|
+
|
46
|
+
|
47
|
+
```ここに言語を入力
|
48
|
+
|
49
|
+
import Foundation
|
50
|
+
|
51
|
+
import Macaw
|
52
|
+
|
53
|
+
import Firebase
|
54
|
+
|
55
|
+
import FirebaseFirestore
|
56
|
+
|
57
|
+
|
58
|
+
|
59
|
+
class ChartView: MacawView {
|
60
|
+
|
61
|
+
|
62
|
+
|
63
|
+
// 最下部のコメントアウトしてある仮データ+中部でコメントアウトされているcreateData() + 以下コメントアウトされているcreateData()を使えば問題なくグラフ描写は可能
|
64
|
+
|
65
|
+
// static let lastTwoRecords = createData()
|
66
|
+
|
67
|
+
|
68
|
+
|
69
|
+
// ここで警告①が表示される
|
70
|
+
|
71
|
+
static let lastTwoRecords = createData { (recordDataArray) in
|
72
|
+
|
73
|
+
// ここで警告②が表示される
|
74
|
+
|
75
|
+
return recordDataArray
|
76
|
+
|
77
|
+
}
|
78
|
+
|
79
|
+
static let maxValue = 20
|
80
|
+
|
81
|
+
static let maxValueinHeight = 20
|
82
|
+
|
83
|
+
static let lineWidth: Double = 385
|
84
|
+
|
85
|
+
|
86
|
+
|
87
|
+
static let dataDivisor = Double(maxValue/maxValueinHeight)
|
88
|
+
|
89
|
+
|
90
|
+
|
91
|
+
// ここでエラー③が表示される
|
92
|
+
|
93
|
+
static let adjustedData: [Double] = lastTwoRecords.map({ Double($0.score) / dataDivisor })
|
94
|
+
|
95
|
+
static var animations: [Animation] = []
|
96
|
+
|
97
|
+
|
98
|
+
|
99
|
+
required init?(coder aDecoder: NSCoder) {
|
100
|
+
|
101
|
+
super.init(node: ChartView.createChart(), coder: aDecoder)
|
102
|
+
|
103
|
+
backgroundColor = .clear
|
104
|
+
|
105
|
+
|
106
|
+
|
107
|
+
}
|
108
|
+
|
109
|
+
|
110
|
+
|
111
|
+
|
112
|
+
|
113
|
+
// 〜グラフ描写のための長い記述が続くため中略〜
|
114
|
+
|
115
|
+
|
116
|
+
|
117
|
+
|
118
|
+
|
119
|
+
// private static func createData() -> [RecordData] {
|
120
|
+
|
121
|
+
private static func createData(completion: @escaping (_ recordDataArray: [RecordData]) -> Void) {
|
122
|
+
|
123
|
+
let todayDate = Date()
|
124
|
+
|
125
|
+
let dateFormatter = DateFormatter()
|
126
|
+
|
127
|
+
dateFormatter.dateFormat = DateFormatter.dateFormat(fromTemplate: "y/M/d", options: 0, locale: Locale.current)
|
128
|
+
|
129
|
+
let editedTodayDate = dateFormatter.string(from: todayDate)
|
130
|
+
|
131
|
+
|
132
|
+
|
133
|
+
let calendar = Calendar.current
|
134
|
+
|
135
|
+
let yesterday = calendar.date(byAdding: .day, value: -1, to: calendar.startOfDay(for: todayDate))
|
136
|
+
|
137
|
+
let editedYesterday = dateFormatter.string(from: yesterday!)
|
138
|
+
|
139
|
+
|
140
|
+
|
141
|
+
dateFormatter.dateFormat = DateFormatter.dateFormat(fromTemplate: "M/d", options: 0, locale: Locale.current)
|
142
|
+
|
143
|
+
let editedTodayDateDisplay = dateFormatter.string(from: todayDate)
|
144
|
+
|
145
|
+
let editedYesterdayDisplay = dateFormatter.string(from: yesterday!)
|
146
|
+
|
147
|
+
|
148
|
+
|
149
|
+
var todayScore: Int16 = 0
|
150
|
+
|
151
|
+
var yesterdayScore: Int16 = 0
|
152
|
+
|
153
|
+
|
154
|
+
|
155
|
+
let db = Firestore.firestore()
|
156
|
+
|
157
|
+
let context = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext
|
158
|
+
|
159
|
+
var recordDataArray: [RecordData] = []
|
160
|
+
|
161
|
+
let dispatchGroup = DispatchGroup()
|
162
|
+
|
163
|
+
|
164
|
+
|
165
|
+
let docRefToday = db.collection("recordData").whereField("recordDate", isEqualTo: editedTodayDate).order(by: "savedTime", descending: true)
|
166
|
+
|
167
|
+
dispatchGroup.enter()
|
168
|
+
|
169
|
+
docRefToday.getDocuments { (snapshot, error) in
|
170
|
+
|
171
|
+
|
172
|
+
|
173
|
+
if snapshot?.documents.first?.data() == nil {
|
174
|
+
|
175
|
+
todayScore = 0
|
176
|
+
|
177
|
+
dispatchGroup.leave()
|
178
|
+
|
179
|
+
} else {
|
180
|
+
|
181
|
+
|
182
|
+
|
183
|
+
todayScore = Int16((snapshot?.documents.first?.data()["score"]! as! NSString).integerValue)
|
184
|
+
|
185
|
+
dispatchGroup.leave()
|
186
|
+
|
187
|
+
}
|
188
|
+
|
189
|
+
}
|
190
|
+
|
191
|
+
|
192
|
+
|
193
|
+
let docRefYesterday = db.collection("recordData").whereField("recordDate", isEqualTo: editedYesterday).order(by: "savedTime", descending: true)
|
194
|
+
|
195
|
+
|
196
|
+
|
197
|
+
dispatchGroup.enter()
|
198
|
+
|
199
|
+
docRefYesterday.getDocuments { (snapshot, error) in
|
200
|
+
|
201
|
+
|
202
|
+
|
203
|
+
if snapshot?.documents.first?.data() == nil {
|
204
|
+
|
205
|
+
yesterdayScore = 0
|
206
|
+
|
207
|
+
dispatchGroup.leave()
|
208
|
+
|
209
|
+
} else {
|
210
|
+
|
211
|
+
|
212
|
+
|
213
|
+
yesterdayScore = Int16((snapshot?.documents.first?.data()["score"]! as! NSString).integerValue)
|
214
|
+
|
215
|
+
dispatchGroup.leave()
|
216
|
+
|
217
|
+
}
|
218
|
+
|
219
|
+
}
|
220
|
+
|
221
|
+
|
222
|
+
|
223
|
+
dispatchGroup.notify(queue: .main) {
|
224
|
+
|
225
|
+
|
226
|
+
|
227
|
+
let newRecordDataOne = RecordData(context: context)
|
228
|
+
|
229
|
+
newRecordDataOne.recordDate = editedYesterdayDisplay
|
230
|
+
|
231
|
+
newRecordDataOne.score = yesterdayScore
|
232
|
+
|
233
|
+
recordDataArray.append(newRecordDataOne)
|
234
|
+
|
235
|
+
|
236
|
+
|
237
|
+
let newRecordDataTwo = RecordData(context: context)
|
238
|
+
|
239
|
+
newRecordDataTwo.recordDate = editedTodayDateDisplay
|
240
|
+
|
241
|
+
newRecordDataTwo.score = todayScore
|
242
|
+
|
243
|
+
recordDataArray.append(newRecordDataTwo)
|
244
|
+
|
245
|
+
|
246
|
+
|
247
|
+
completion(recordDataArray)
|
248
|
+
|
249
|
+
|
250
|
+
|
251
|
+
}
|
252
|
+
|
253
|
+
|
254
|
+
|
255
|
+
// 仮データ
|
256
|
+
|
257
|
+
|
258
|
+
|
259
|
+
// let newRecordDataOne = RecordData(context: context)
|
260
|
+
|
261
|
+
// newRecordDataOne.recordDate = editedYesterdayDisplay
|
262
|
+
|
263
|
+
// newRecordDataOne.score = 18
|
264
|
+
|
265
|
+
// recordDataArray.append(newRecordDataOne)
|
266
|
+
|
267
|
+
//
|
268
|
+
|
269
|
+
// let newRecordDataTwo = RecordData(context: context)
|
270
|
+
|
271
|
+
// newRecordDataTwo.recordDate = editedTodayDateDisplay
|
272
|
+
|
273
|
+
// newRecordDataTwo.score = 15
|
274
|
+
|
275
|
+
// recordDataArray.append(newRecordDataTwo)
|
276
|
+
|
277
|
+
|
278
|
+
|
279
|
+
// return recordDataArray
|
280
|
+
|
281
|
+
|
282
|
+
|
283
|
+
}
|
284
|
+
|
285
|
+
|
286
|
+
|
287
|
+
}
|
288
|
+
|
289
|
+
|
290
|
+
|
49
291
|
```
|
50
292
|
|
51
293
|
|
52
294
|
|
53
|
-
### 該当のソースコード
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
```ここに言語を入力
|
58
|
-
|
59
|
-
import Foundation
|
60
|
-
|
61
|
-
import Macaw
|
62
|
-
|
63
|
-
import Firebase
|
64
|
-
|
65
|
-
import FirebaseFirestore
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
class ChartView: MacawView {
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
// 最下部のコメントアウトしてある仮データ+中部でコメントアウトされているcreateData() + 以下コメントアウトされているcreateData()を使えば問題なくグラフ描写は可能
|
74
|
-
|
75
|
-
// static let lastTwoRecords = createData()
|
76
|
-
|
77
|
-
static let lastTwoRecords = createData { (recordDataArray) in
|
78
|
-
|
79
|
-
return recordDataArray
|
80
|
-
|
81
|
-
}
|
82
|
-
|
83
|
-
static let maxValue = 20
|
84
|
-
|
85
|
-
static let maxValueinHeight = 20
|
86
|
-
|
87
|
-
static let lineWidth: Double = 385
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
static let dataDivisor = Double(maxValue/maxValueinHeight)
|
92
|
-
|
93
|
-
static let adjustedData: [Double] = lastTwoRecords.map({ Double($0.score) / dataDivisor })
|
94
|
-
|
95
|
-
static var animations: [Animation] = []
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
required init?(coder aDecoder: NSCoder) {
|
100
|
-
|
101
|
-
super.init(node: ChartView.createChart(), coder: aDecoder)
|
102
|
-
|
103
|
-
backgroundColor = .clear
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
}
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
// 〜グラフ描写のための長い記述が続くため中略〜
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
// private static func createData() -> [RecordData] {
|
120
|
-
|
121
|
-
private static func createData(completion: @escaping (_ recordDataArray: [RecordData]) -> Void) {
|
122
|
-
|
123
|
-
//今日の日付を取得し表示(年あり)
|
124
|
-
|
125
|
-
let todayDate = Date()
|
126
|
-
|
127
|
-
let dateFormatter = DateFormatter()
|
128
|
-
|
129
|
-
dateFormatter.dateFormat = DateFormatter.dateFormat(fromTemplate: "y/M/d", options: 0, locale: Locale.current)
|
130
|
-
|
131
|
-
//dateFormatter.dateFormat = DateFormatter.dateFormat(fromTemplate: "y/M/d", options: 0, locale: Locale(identifier: "ja_JP"))
|
132
|
-
|
133
|
-
let editedTodayDate = dateFormatter.string(from: todayDate)
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
-
//直近2日分の日付を取得(データ取得用(年あり))
|
138
|
-
|
139
|
-
let calendar = Calendar.current
|
140
|
-
|
141
|
-
let yesterday = calendar.date(byAdding: .day, value: -1, to: calendar.startOfDay(for: todayDate))
|
142
|
-
|
143
|
-
let editedYesterday = dateFormatter.string(from: yesterday!)
|
144
|
-
|
145
|
-
|
146
|
-
|
147
|
-
//直近2日分の年なし表示用日付を取得
|
148
|
-
|
149
|
-
dateFormatter.dateFormat = DateFormatter.dateFormat(fromTemplate: "M/d", options: 0, locale: Locale.current)
|
150
|
-
|
151
|
-
let editedTodayDateDisplay = dateFormatter.string(from: todayDate)
|
152
|
-
|
153
|
-
let editedYesterdayDisplay = dateFormatter.string(from: yesterday!)
|
154
|
-
|
155
|
-
|
156
|
-
|
157
|
-
//今日の最新scoreデータを取得
|
158
|
-
|
159
|
-
var todayScore: Int16 = 0
|
160
|
-
|
161
|
-
var yesterdayScore: Int16 = 0
|
162
|
-
|
163
|
-
|
164
|
-
|
165
|
-
//直近7日分のデータをFirestoreから取得
|
166
|
-
|
167
|
-
let db = Firestore.firestore()
|
168
|
-
|
169
|
-
let context = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext
|
170
|
-
|
171
|
-
var recordDataArray: [RecordData] = []
|
172
|
-
|
173
|
-
let dispatchGroup = DispatchGroup()
|
174
|
-
|
175
|
-
|
176
|
-
|
177
|
-
//データを検索(recordDataのrecordDateが今日の日付でsavedTimeが最新のもの)
|
178
|
-
|
179
|
-
let docRefToday = db.collection("recordData").whereField("recordDate", isEqualTo: editedTodayDate).order(by: "savedTime", descending: true)
|
180
|
-
|
181
|
-
dispatchGroup.enter()
|
182
|
-
|
183
|
-
docRefToday.getDocuments { (snapshot, error) in
|
184
|
-
|
185
|
-
//TotalScoreを取得
|
186
|
-
|
187
|
-
//記録がユーザーによって保存されなかった日の場合はデータがないためTotalScoreには0をセット
|
188
|
-
|
189
|
-
if snapshot?.documents.first?.data() == nil {
|
190
|
-
|
191
|
-
todayScore = 0
|
192
|
-
|
193
|
-
dispatchGroup.leave()
|
194
|
-
|
195
|
-
} else {
|
196
|
-
|
197
|
-
//データがあった場合はTotalScoreを取得
|
198
|
-
|
199
|
-
todayScore = Int16((snapshot?.documents.first?.data()["score"]! as! NSString).integerValue)
|
200
|
-
|
201
|
-
dispatchGroup.leave()
|
202
|
-
|
203
|
-
}
|
204
|
-
|
205
|
-
}
|
206
|
-
|
207
|
-
|
208
|
-
|
209
|
-
//データを検索(recordDataのrecordDateが昨日の日付でsavedTimeが最新のもの)
|
210
|
-
|
211
|
-
let docRefYesterday = db.collection("recordData").whereField("recordDate", isEqualTo: editedYesterday).order(by: "savedTime", descending: true)
|
212
|
-
|
213
|
-
|
214
|
-
|
215
|
-
dispatchGroup.enter()
|
216
|
-
|
217
|
-
docRefYesterday.getDocuments { (snapshot, error) in
|
218
|
-
|
219
|
-
//TotalScoreを取得
|
220
|
-
|
221
|
-
//記録がユーザーによって保存されなかった日の場合はデータがないためTotalScoreには0をセット
|
222
|
-
|
223
|
-
if snapshot?.documents.first?.data() == nil {
|
224
|
-
|
225
|
-
yesterdayScore = 0
|
226
|
-
|
227
|
-
dispatchGroup.leave()
|
228
|
-
|
229
|
-
} else {
|
230
|
-
|
231
|
-
//データがあった場合はTotalScoreを取得
|
232
|
-
|
233
|
-
yesterdayScore = Int16((snapshot?.documents.first?.data()["score"]! as! NSString).integerValue)
|
234
|
-
|
235
|
-
dispatchGroup.leave()
|
236
|
-
|
237
|
-
}
|
238
|
-
|
239
|
-
}
|
240
|
-
|
241
|
-
|
242
|
-
|
243
|
-
dispatchGroup.notify(queue: .main) {
|
244
|
-
|
245
|
-
|
246
|
-
|
247
|
-
//最後がtodayになるように古いものから追加していく)
|
248
|
-
|
249
|
-
|
250
|
-
|
251
|
-
let newRecordDataOne = RecordData(context: context)
|
252
|
-
|
253
|
-
newRecordDataOne.recordDate = editedYesterdayDisplay
|
254
|
-
|
255
|
-
newRecordDataOne.score = yesterdayScore
|
256
|
-
|
257
|
-
recordDataArray.append(newRecordDataOne)
|
258
|
-
|
259
|
-
|
260
|
-
|
261
|
-
let newRecordDataTwo = RecordData(context: context)
|
262
|
-
|
263
|
-
newRecordDataTwo.recordDate = editedTodayDateDisplay
|
264
|
-
|
265
|
-
newRecordDataTwo.score = todayScore
|
266
|
-
|
267
|
-
recordDataArray.append(newRecordDataTwo)
|
268
|
-
|
269
|
-
|
270
|
-
|
271
|
-
completion(recordDataArray)
|
272
|
-
|
273
|
-
|
274
|
-
|
275
|
-
}
|
276
|
-
|
277
|
-
|
278
|
-
|
279
|
-
// 仮データ
|
280
|
-
|
281
|
-
|
282
|
-
|
283
|
-
// let newRecordDataOne = RecordData(context: context)
|
284
|
-
|
285
|
-
// newRecordDataOne.recordDate = editedYesterdayDisplay
|
286
|
-
|
287
|
-
// newRecordDataOne.score = 18
|
288
|
-
|
289
|
-
// recordDataArray.append(newRecordDataOne)
|
290
|
-
|
291
|
-
//
|
292
|
-
|
293
|
-
// let newRecordDataTwo = RecordData(context: context)
|
294
|
-
|
295
|
-
// newRecordDataTwo.recordDate = editedTodayDateDisplay
|
296
|
-
|
297
|
-
// newRecordDataTwo.score = 15
|
298
|
-
|
299
|
-
// recordDataArray.append(newRecordDataTwo)
|
300
|
-
|
301
|
-
|
302
|
-
|
303
|
-
// return recordDataArray
|
304
|
-
|
305
|
-
|
306
|
-
|
307
|
-
}
|
308
|
-
|
309
|
-
|
310
|
-
|
311
|
-
}
|
312
|
-
|
313
|
-
|
314
|
-
|
315
|
-
```
|
316
|
-
|
317
|
-
|
318
|
-
|
319
295
|
|
320
296
|
|
321
297
|
### 試したこと
|
1
試したことの追記
test
CHANGED
File without changes
|
test
CHANGED
@@ -30,7 +30,7 @@
|
|
30
30
|
|
31
31
|
|
32
32
|
|
33
|
-
②Expression of type '[RecordData]' is unused
|
33
|
+
②Expression of type '[RecordData]' is unused
|
34
34
|
|
35
35
|
|
36
36
|
|
@@ -326,12 +326,16 @@
|
|
326
326
|
|
327
327
|
|
328
328
|
|
329
|
-
①Completion Handler
|
330
|
-
|
331
|
-
|
329
|
+
①NSCondition
|
330
|
+
|
331
|
+
②NotificationCenter
|
332
332
|
|
333
333
|
③DispatchSemaphore
|
334
334
|
|
335
|
+
④Completion Handler
|
336
|
+
|
337
|
+
⑤DispatchGroup
|
338
|
+
|
335
339
|
|
336
340
|
|
337
341
|
の3つを始め様々な方法を試しましたがどれも望んだ結果が得られませんでした。
|