質問編集履歴
4
整理
test
CHANGED
File without changes
|
test
CHANGED
@@ -1,3 +1,7 @@
|
|
1
|
+
(復元要請がありましたので復元いたします)
|
2
|
+
|
3
|
+
|
4
|
+
|
1
5
|
以前にも同じ質問をしたのですが、
|
2
6
|
|
3
7
|
https://teratail.com/questions/138971
|
3
みさなさんがわかるように整理
test
CHANGED
@@ -1 +1 @@
|
|
1
|
-
|
1
|
+
[Firebase][ios][Xcode]RealtimeDataBaseで、リレーション先の値の取得がうまくいかない
|
test
CHANGED
@@ -1 +1,335 @@
|
|
1
|
+
以前にも同じ質問をしたのですが、
|
2
|
+
|
3
|
+
https://teratail.com/questions/138971
|
4
|
+
|
5
|
+
非同期処理を実施しておらず、
|
6
|
+
|
7
|
+
質問以前の段階でしが、
|
8
|
+
|
9
|
+
非同期処理を理解できたので(おそらく)
|
10
|
+
|
11
|
+
もう一度不明点を整理しました。
|
12
|
+
|
13
|
+
仕様
|
14
|
+
|
15
|
+
ユーザーは
|
16
|
+
|
17
|
+
イベントを投稿して
|
18
|
+
|
19
|
+
そのイベントに対してコメントをつけられる。
|
20
|
+
|
21
|
+
RealtimeDataBaseには、EventとCommentとして保存される
|
22
|
+
|
23
|
+
イベントとコメントはtableviewCellで並ぶ
|
24
|
+
|
25
|
+
コメントはイベントと紐づいたものとして取得したい
|
26
|
+
|
27
|
+
そんなイメージのアプリです
|
28
|
+
|
29
|
+
ツリー
|
30
|
+
|
31
|
+
```ここに言語を入力
|
32
|
+
|
33
|
+
{
|
34
|
+
|
35
|
+
Events{
|
36
|
+
|
37
|
+
0{
|
38
|
+
|
39
|
+
eventName:"hogehoge"
|
40
|
+
|
41
|
+
Comments{
|
42
|
+
|
43
|
+
0:true
|
44
|
+
|
45
|
+
}
|
46
|
+
|
47
|
+
}
|
48
|
+
|
49
|
+
1{
|
50
|
+
|
51
|
+
eventName:"hogehoge"
|
52
|
+
|
53
|
+
Comments{
|
54
|
+
|
55
|
+
1:true
|
56
|
+
|
57
|
+
2:true
|
58
|
+
|
59
|
+
}
|
60
|
+
|
61
|
+
}
|
62
|
+
|
63
|
+
}
|
64
|
+
|
65
|
+
Comments{
|
66
|
+
|
67
|
+
0{
|
68
|
+
|
69
|
+
text:"hogehoge"
|
70
|
+
|
71
|
+
Events{
|
72
|
+
|
73
|
+
0:true
|
74
|
+
|
75
|
+
}
|
76
|
+
|
77
|
+
}
|
78
|
+
|
79
|
+
1{
|
80
|
+
|
81
|
+
text:"hogehoge"
|
82
|
+
|
83
|
+
Events{
|
84
|
+
|
85
|
+
1:true
|
86
|
+
|
87
|
+
}
|
88
|
+
|
89
|
+
}
|
90
|
+
|
91
|
+
2{
|
92
|
+
|
93
|
+
text:"hogehoge"
|
94
|
+
|
95
|
+
Events{
|
96
|
+
|
97
|
+
1:true
|
98
|
+
|
99
|
+
}
|
100
|
+
|
101
|
+
}
|
102
|
+
|
103
|
+
}
|
104
|
+
|
105
|
+
}
|
106
|
+
|
107
|
+
```
|
108
|
+
|
109
|
+
前回の質問でアドバイスいただいた非同期処理
|
110
|
+
|
111
|
+
コールバックを使った
|
112
|
+
|
113
|
+
Eventのみの取得はできるようになりました。
|
114
|
+
|
115
|
+
しかし本命である、Eventに紐づいたCommentの取得がうまくできません。
|
116
|
+
|
117
|
+
EventのIDを使って、Commnt{Event}を取得したいのですが
|
118
|
+
|
119
|
+
うまくいきません。
|
120
|
+
|
121
|
+
CommentManager.swift
|
122
|
+
|
123
|
+
```ここに言語を入力
|
124
|
+
|
125
|
+
import UIKit
|
126
|
+
|
127
|
+
import Firebase
|
128
|
+
|
129
|
+
class CommentManager: NSObject {
|
130
|
+
|
131
|
+
|
132
|
+
|
133
|
+
static let sharedInstance = CommentManager() //ここでエラーが出てしまう
|
134
|
+
|
135
|
+
|
136
|
+
|
137
|
+
var comments: [Comment] = []
|
138
|
+
|
139
|
+
|
140
|
+
|
141
|
+
var items = [NSDictionary]()
|
142
|
+
|
143
|
+
|
144
|
+
|
145
|
+
var getEventID: String
|
146
|
+
|
147
|
+
init(getEventID: String) {
|
148
|
+
|
149
|
+
self.getEventID = getEventID
|
150
|
+
|
151
|
+
}
|
152
|
+
|
153
|
+
|
154
|
+
|
155
|
+
|
156
|
+
|
157
|
+
|
158
|
+
|
159
|
+
func fetchComments(callback: @escaping () -> Void) {
|
160
|
+
|
161
|
+
|
162
|
+
|
163
|
+
let firebase = Database.database().reference(fromURL: "hogehoge").child("Events")
|
164
|
+
|
165
|
+
var tempItems = [NSDictionary]()
|
166
|
+
|
167
|
+
|
168
|
+
|
169
|
+
firebase.child(getEventID).child("Comments").observe(.value){ (snapshot,error) in
|
170
|
+
|
171
|
+
|
172
|
+
|
173
|
+
for item in (snapshot.children){
|
174
|
+
|
175
|
+
let key: String = (item as AnyObject).key
|
176
|
+
|
177
|
+
firebase.child("Comments").child(key).observe(.value){ (snapshot,error) in
|
178
|
+
|
179
|
+
let value = snapshot.value as? NSDictionary
|
180
|
+
|
181
|
+
tempItems.append(value!)
|
182
|
+
|
183
|
+
}
|
184
|
+
|
185
|
+
}
|
186
|
+
|
187
|
+
self.items = tempItems
|
188
|
+
|
189
|
+
self.items = self.items.reversed()
|
190
|
+
|
191
|
+
callback()
|
192
|
+
|
193
|
+
|
194
|
+
|
195
|
+
}
|
196
|
+
|
197
|
+
|
198
|
+
|
199
|
+
}
|
200
|
+
|
201
|
+
}
|
202
|
+
|
203
|
+
```
|
204
|
+
|
205
|
+
CommentListViewController.swift
|
206
|
+
|
207
|
+
```ここに言語を入力
|
208
|
+
|
209
|
+
import UIKit
|
210
|
+
|
211
|
+
import Firebase
|
212
|
+
|
213
|
+
class CommentOfEventListViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
|
214
|
+
|
215
|
+
|
216
|
+
|
217
|
+
let commentManager = CommentManager.sharedInstance
|
218
|
+
|
219
|
+
|
220
|
+
|
221
|
+
var getEventID = String()
|
222
|
+
|
223
|
+
省略
|
224
|
+
|
225
|
+
override func viewDidLoad() {
|
226
|
+
|
227
|
+
super.viewDidLoad()
|
228
|
+
|
229
|
+
commentManager.items = [NSDictionary]()
|
230
|
+
|
231
|
+
|
232
|
+
|
233
|
+
省略
|
234
|
+
|
235
|
+
|
236
|
+
|
237
|
+
commentListTable.delegate = self
|
238
|
+
|
239
|
+
commentListTable.dataSource = self
|
240
|
+
|
241
|
+
|
242
|
+
|
243
|
+
getCommentsRdbData()
|
244
|
+
|
245
|
+
commentListTable.reloadData()
|
246
|
+
|
247
|
+
}
|
248
|
+
|
249
|
+
|
250
|
+
|
251
|
+
func numberOfSections(in tableView: UITableView) -> Int{
|
252
|
+
|
253
|
+
return 1
|
254
|
+
|
255
|
+
}
|
256
|
+
|
257
|
+
|
258
|
+
|
259
|
+
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
|
260
|
+
|
261
|
+
//return 10
|
262
|
+
|
263
|
+
return commentManager.items.count
|
264
|
+
|
265
|
+
}
|
266
|
+
|
267
|
+
|
268
|
+
|
269
|
+
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
|
270
|
+
|
271
|
+
|
272
|
+
|
273
|
+
let cell = tableView.dequeueReusableCell(withIdentifier: "commentCell", for: indexPath)
|
274
|
+
|
275
|
+
cell.selectionStyle = UITableViewCellSelectionStyle.none
|
276
|
+
|
277
|
+
|
278
|
+
|
279
|
+
let dict = commentManager.items[(indexPath as NSIndexPath).row]
|
280
|
+
|
281
|
+
|
282
|
+
|
283
|
+
let commentTextView = cell.viewWithTag(1) as! UITextView
|
284
|
+
|
285
|
+
commentTextView.text = dict["text"] as? String
|
286
|
+
|
287
|
+
|
288
|
+
|
289
|
+
return cell
|
290
|
+
|
291
|
+
}
|
292
|
+
|
293
|
+
|
294
|
+
|
295
|
+
|
296
|
+
|
297
|
+
func getCommentsRdbData(){
|
298
|
+
|
299
|
+
UIApplication.shared.isNetworkActivityIndicatorVisible = true
|
300
|
+
|
301
|
+
|
302
|
+
|
303
|
+
//getEventIDをCommentManager.swiftのinitに渡したい
|
304
|
+
|
305
|
+
let comment = CommentManager(getEventID: getEventID)
|
306
|
+
|
307
|
+
//comment.fetchComments()
|
308
|
+
|
309
|
+
//getEventIDを渡しながらコールバックしたいのですが・・・書き方がわからない
|
310
|
+
|
311
|
+
commentManager.fetchComments { () in
|
312
|
+
|
313
|
+
self.eventListTable.reloadData()
|
314
|
+
|
315
|
+
}
|
316
|
+
|
317
|
+
|
318
|
+
|
319
|
+
UIApplication.shared.isNetworkActivityIndicatorVisible = false
|
320
|
+
|
321
|
+
}
|
322
|
+
|
323
|
+
|
324
|
+
|
325
|
+
}
|
326
|
+
|
327
|
+
```
|
328
|
+
|
1
|
-
|
329
|
+
CommentListViewController.swiftにEventIDは渡せているので
|
330
|
+
|
331
|
+
それをCommentManagerにわたして
|
332
|
+
|
333
|
+
紐づいたCommentを見つけたいです。
|
334
|
+
|
335
|
+
アドバイスよろしくお願いします
|
2
修正
test
CHANGED
@@ -1 +1 @@
|
|
1
|
-
質問自身に根幹からの不備があり削除要請をしたのですが、通らなかったので削除させていただきます
|
1
|
+
質問自身に根幹からの不備があり削除要請をしたのですが、通らなかったので文章を削除させていただきます
|
test
CHANGED
@@ -1 +1 @@
|
|
1
|
-
質問自身に根幹からの不備があり削除要請をしたのですが、通らなかったので削除させていただきます
|
1
|
+
質問自身に根幹からの不備があり削除要請をしたのですが、通らなかったので文章を削除させていただきます
|
1
根幹から間違っていたため
test
CHANGED
@@ -1 +1 @@
|
|
1
|
-
|
1
|
+
質問自身に根幹からの不備があり削除要請をしたのですが、通らなかったので削除させていただきます
|
test
CHANGED
@@ -1,415 +1 @@
|
|
1
|
-
以前にも同じ質問をしたのですが、
|
2
|
-
|
3
|
-
https://teratail.com/questions/138971
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
非同期処理を実施しておらず、
|
8
|
-
|
9
|
-
質問以前の段階でしが、
|
10
|
-
|
11
|
-
非同期処理を理解できたので(おそらく)
|
12
|
-
|
13
|
-
もう一度不明点を整理しました。
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
仕様
|
18
|
-
|
19
|
-
ユーザーは
|
20
|
-
|
21
|
-
イベントを投稿して
|
22
|
-
|
23
|
-
そのイベントに対してコメントをつけられる。
|
24
|
-
|
25
|
-
RealtimeDataBaseには、EventとCommentとして保存される
|
26
|
-
|
27
|
-
イベントとコメントはtableviewCellで並ぶ
|
28
|
-
|
29
|
-
コメントはイベントと紐づいたものとして取得したい
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
そんなイメージのアプリです
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
ツリー
|
38
|
-
|
39
|
-
```ここに言語を入力
|
40
|
-
|
41
|
-
{
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
Events{
|
46
|
-
|
47
|
-
0{
|
48
|
-
|
49
|
-
eventName:"hogehoge"
|
50
|
-
|
51
|
-
Comments{
|
52
|
-
|
53
|
-
0:true
|
54
|
-
|
55
|
-
}
|
56
|
-
|
57
|
-
}
|
58
|
-
|
59
|
-
1{
|
60
|
-
|
61
|
-
eventName:"hogehoge"
|
62
|
-
|
63
|
-
Comments{
|
64
|
-
|
65
|
-
1:true
|
66
|
-
|
67
|
-
2:true
|
68
|
-
|
69
|
-
}
|
70
|
-
|
71
|
-
}
|
72
|
-
|
73
|
-
}
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
Comments{
|
78
|
-
|
79
|
-
0{
|
80
|
-
|
81
|
-
text:"hogehoge"
|
82
|
-
|
83
|
-
Events{
|
84
|
-
|
85
|
-
0:true
|
86
|
-
|
87
|
-
}
|
88
|
-
|
89
|
-
}
|
90
|
-
|
91
|
-
1{
|
92
|
-
|
93
|
-
text:"hogehoge"
|
94
|
-
|
95
|
-
Events{
|
96
|
-
|
97
|
-
1:true
|
98
|
-
|
99
|
-
}
|
100
|
-
|
101
|
-
}
|
102
|
-
|
103
|
-
2{
|
104
|
-
|
105
|
-
text:"hogehoge"
|
106
|
-
|
107
|
-
Events{
|
108
|
-
|
109
|
-
1:true
|
110
|
-
|
111
|
-
}
|
112
|
-
|
113
|
-
}
|
114
|
-
|
115
|
-
}
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
}
|
120
|
-
|
121
|
-
```
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
前回の質問でアドバイスいただいた非同期処理
|
126
|
-
|
127
|
-
コールバックを使った
|
128
|
-
|
129
|
-
Eventのみの取得はできるようになりました。
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
しかし本命である、Eventに紐づいたCommentの取得がうまくできません。
|
134
|
-
|
135
|
-
EventのIDを使って、Commnt{Event}を取得したいのですが
|
136
|
-
|
137
|
-
うまくいきません。
|
138
|
-
|
139
|
-
|
140
|
-
|
141
|
-
|
142
|
-
|
143
|
-
CommentManager.swift
|
144
|
-
|
145
|
-
```ここに言語を入力
|
146
|
-
|
147
|
-
import UIKit
|
148
|
-
|
149
|
-
import Firebase
|
150
|
-
|
151
|
-
|
152
|
-
|
153
|
-
class CommentManager: NSObject {
|
154
|
-
|
155
|
-
|
156
|
-
|
157
|
-
static let sharedInstance = CommentManager() //ここでエラーが出てしまう
|
158
|
-
|
159
|
-
|
160
|
-
|
161
|
-
//複数のcommentを保持する配列comments
|
162
|
-
|
163
|
-
var comments: [Comment] = []
|
164
|
-
|
165
|
-
|
166
|
-
|
167
|
-
var items = [NSDictionary]() //データベースからひっぱってきたものを入れる箱
|
168
|
-
|
169
|
-
|
170
|
-
|
171
|
-
var getEventID: String //CommentListViewControllerからgetEventIDを受け取りたい
|
172
|
-
|
173
|
-
init(getEventID: String) {
|
174
|
-
|
175
|
-
self.getEventID = getEventID
|
176
|
-
|
177
|
-
}
|
178
|
-
|
179
|
-
|
180
|
-
|
181
|
-
|
182
|
-
|
183
|
-
|
184
|
-
|
185
|
-
|
186
|
-
|
187
|
-
func fetchComments(callback: @escaping () -> Void) {
|
188
|
-
|
189
|
-
|
190
|
-
|
191
|
-
let firebase = Database.database().reference(fromURL: "hogehoge").child("Events")
|
192
|
-
|
193
|
-
var tempItems = [NSDictionary]()
|
194
|
-
|
195
|
-
|
196
|
-
|
197
|
-
firebase.child(getEventID).child("Comments").observe(.value){ (snapshot,error) in
|
198
|
-
|
199
|
-
|
200
|
-
|
201
|
-
for item in (snapshot.children){
|
202
|
-
|
203
|
-
// コメントのkey
|
204
|
-
|
205
|
-
let key: String = (item as AnyObject).key
|
206
|
-
|
207
|
-
// コメントのキーから Comments の情報を取得する
|
208
|
-
|
209
|
-
firebase.child("Comments").child(key).observe(.value){ (snapshot,error) in
|
210
|
-
|
211
|
-
let value = snapshot.value as? NSDictionary
|
212
|
-
|
213
|
-
tempItems.append(value!)
|
214
|
-
|
215
|
-
}
|
216
|
-
|
217
|
-
}
|
218
|
-
|
219
|
-
self.items = tempItems
|
220
|
-
|
221
|
-
self.items = self.items.reversed()
|
222
|
-
|
223
|
-
callback()
|
224
|
-
|
225
|
-
|
226
|
-
|
227
|
-
}
|
228
|
-
|
229
|
-
|
230
|
-
|
231
|
-
}
|
232
|
-
|
233
|
-
|
234
|
-
|
235
|
-
}
|
236
|
-
|
237
|
-
```
|
238
|
-
|
239
|
-
|
240
|
-
|
241
|
-
|
242
|
-
|
243
|
-
CommentListViewController.swift
|
244
|
-
|
245
|
-
```ここに言語を入力
|
246
|
-
|
247
|
-
import UIKit
|
248
|
-
|
249
|
-
import Firebase
|
250
|
-
|
251
|
-
|
252
|
-
|
253
|
-
class CommentOfEventListViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
|
254
|
-
|
255
|
-
|
256
|
-
|
257
|
-
//データベースからひっぱってきたものを入れる箱
|
258
|
-
|
259
|
-
let commentManager = CommentManager.sharedInstance
|
260
|
-
|
261
|
-
|
262
|
-
|
263
|
-
|
264
|
-
|
265
|
-
//前のページからEventのIDを受け取り
|
266
|
-
|
267
|
-
var getEventID = String()
|
268
|
-
|
269
|
-
|
270
|
-
|
271
|
-
省略
|
272
|
-
|
273
|
-
|
274
|
-
|
275
|
-
override func viewDidLoad() {
|
276
|
-
|
277
|
-
super.viewDidLoad()
|
278
|
-
|
279
|
-
|
280
|
-
|
281
|
-
//データをサーバーから呼んでくる
|
282
|
-
|
283
|
-
commentManager.items = [NSDictionary]()
|
284
|
-
|
285
|
-
|
286
|
-
|
287
|
-
省略
|
288
|
-
|
289
|
-
|
290
|
-
|
291
|
-
commentListTable.delegate = self
|
292
|
-
|
293
|
-
commentListTable.dataSource = self
|
294
|
-
|
295
|
-
|
296
|
-
|
297
|
-
|
298
|
-
|
299
|
-
//データべーすから受け取り
|
300
|
-
|
301
|
-
getCommentsRdbData()
|
302
|
-
|
303
|
-
|
304
|
-
|
305
|
-
commentListTable.reloadData()
|
306
|
-
|
307
|
-
|
308
|
-
|
309
|
-
}
|
310
|
-
|
311
|
-
|
312
|
-
|
313
|
-
//セクション数
|
314
|
-
|
315
|
-
func numberOfSections(in tableView: UITableView) -> Int{
|
316
|
-
|
317
|
-
return 1
|
318
|
-
|
319
|
-
}
|
320
|
-
|
321
|
-
//セルの数
|
322
|
-
|
323
|
-
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
|
324
|
-
|
325
|
-
//return 10
|
326
|
-
|
327
|
-
return commentManager.items.count
|
328
|
-
|
329
|
-
}
|
330
|
-
|
331
|
-
|
332
|
-
|
333
|
-
//セルの内容
|
334
|
-
|
335
|
-
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
|
336
|
-
|
337
|
-
|
338
|
-
|
339
|
-
//セルの内容
|
340
|
-
|
341
|
-
let cell = tableView.dequeueReusableCell(withIdentifier: "commentCell", for: indexPath)
|
342
|
-
|
343
|
-
cell.selectionStyle = UITableViewCellSelectionStyle.none
|
344
|
-
|
345
|
-
|
346
|
-
|
347
|
-
//getCommentsRdbData()が呼ばれると items配列 にデータベースの値が入る
|
348
|
-
|
349
|
-
let dict = commentManager.items[(indexPath as NSIndexPath).row] //indexpathのrow番目
|
350
|
-
|
351
|
-
|
352
|
-
|
353
|
-
//comments
|
354
|
-
|
355
|
-
let commentTextView = cell.viewWithTag(1) as! UITextView
|
356
|
-
|
357
|
-
commentTextView.text = dict["text"] as? String
|
358
|
-
|
359
|
-
|
360
|
-
|
361
|
-
return cell
|
362
|
-
|
363
|
-
}
|
364
|
-
|
365
|
-
|
366
|
-
|
367
|
-
|
368
|
-
|
369
|
-
//realtimeDataBaseからデータを受け取る
|
370
|
-
|
371
|
-
func getCommentsRdbData(){
|
372
|
-
|
373
|
-
UIApplication.shared.isNetworkActivityIndicatorVisible = true
|
374
|
-
|
375
|
-
|
376
|
-
|
377
|
-
//getEventIDをCommentManager.swiftのinitに渡したい
|
378
|
-
|
379
|
-
let comment = CommentManager(getEventID: getEventID)
|
380
|
-
|
381
|
-
//comment.fetchComments()
|
382
|
-
|
383
|
-
//getEventIDを渡しながらコールバックしたいのですが・・・書き方がわからない
|
384
|
-
|
385
|
-
commentManager.fetchComments { () in
|
386
|
-
|
387
|
-
self.eventListTable.reloadData()
|
388
|
-
|
389
|
-
}
|
390
|
-
|
391
|
-
|
392
|
-
|
393
|
-
UIApplication.shared.isNetworkActivityIndicatorVisible = false
|
394
|
-
|
395
|
-
}
|
396
|
-
|
397
|
-
|
398
|
-
|
399
|
-
|
400
|
-
|
401
|
-
}
|
402
|
-
|
403
|
-
```
|
404
|
-
|
405
|
-
|
406
|
-
|
407
|
-
|
1
|
+
質問自身に根幹からの不備があり削除要請をしたのですが、通らなかったので削除させていただきます
|
408
|
-
|
409
|
-
それをCommentManagerにわたして
|
410
|
-
|
411
|
-
紐づいたCommentを見つけたいです。
|
412
|
-
|
413
|
-
|
414
|
-
|
415
|
-
アドバイスよろしくお願いします
|