質問編集履歴

1

文法のの修正、コードの追加

2020/05/28 14:39

投稿

Chiaki1111
Chiaki1111

スコア15

test CHANGED
@@ -1 +1 @@
1
- 【SwiftUI】【FireStore】前のViewのあるキーワードを取得して、それをキーにFireStoreからデータを取り出、次のViewに表示させたい
1
+ 【SwiftUI】【FireStore】FireStoreデータを取ながらViewの遷移をしたい
test CHANGED
@@ -1,3 +1,227 @@
1
+ ### 前提・実現したいこと
2
+
3
+
4
+
5
+ 下記のようなデータ構成でFirestoreにデータが格納してあり、
6
+
7
+ 下記のような遷移を実現したいです。
8
+
9
+ 感覚的にはよく使いそうな遷移だと思うのですが、どのように実現するのが良いのか分からず、
10
+
11
+ どなたかにご教示いただきたいです。
12
+
13
+
14
+
15
+
16
+
17
+ 【データの構成】
18
+
19
+ Collection("Theme")→document()→各documentにCollection("Photo")が含まれる
20
+
21
+
22
+
23
+ 【実現したいViewの遷移】
24
+
25
+ Themeの情報が含まれたView→そのThemeのPhotoが表示されるView
26
+
27
+
28
+
29
+
30
+
31
+ ### 該当のソースコード
32
+
33
+
34
+
1
- 表題の件、色々試してみましたが、うまく表示させることができませんでした。
35
+ こんなコードを試してみましたが、表示さませんでした。
2
-
36
+
3
- ような手法でも良いのでこれを実現できるコードを教えていただけませんしょうか
37
+ 法でなくても、実現したいViewの遷移ができれば教えていただきたい
38
+
39
+
40
+
41
+ ```SwiftUI
42
+
43
+
44
+
45
+ //Themeの情報が含まれたView
46
+
47
+ import SwiftUI
48
+
49
+
50
+
51
+ struct ThemeView: View {
52
+
53
+ @ObservedObject var themeList = ThemeObserver()
54
+
55
+
56
+
57
+ var body: some View {
58
+
59
+ VStack{
60
+
61
+ List(themeList.status){ theme in
62
+
63
+ NavigationLink(destination: PhotoView(theme: self.theme)){
64
+
65
+ ThemeView(oneTheme: theme)
66
+
67
+ }
68
+
69
+ }
70
+
71
+ }
72
+
73
+ }
74
+
75
+ }
76
+
77
+
78
+
79
+ //UserのPhotoが表示されるView
80
+
81
+ struct PhotoView: View {
82
+
83
+ var theme : ThemeEntity
84
+
85
+ var ObservedObject var photoList = PhotoObserver()
86
+
87
+ var body: some View {
88
+
89
+ photoList.pickup(theme: theme)
90
+
91
+
92
+
93
+ return ScrollView{//ScrollViewを外すとなぜか表示されるようになる
94
+
95
+ VStack{
96
+
97
+ ForEach(self.status,id: .self){photo in
98
+
99
+ Text(photo.title)//例
100
+
101
+ }
102
+
103
+ }
104
+
105
+ }
106
+
107
+ }
108
+
109
+ }
110
+
111
+
112
+
113
+ //FireStoreからThemeを取得するObservableObject
114
+
115
+ class ThemeObserver:ObservableObject {
116
+
117
+ @Published var status = [ThemeEntity]()
118
+
119
+
120
+
121
+ init() {
122
+
123
+ let db = Firestore.firestore()
124
+
125
+ db.collection("Theme").addSnapshotListener{(snap, error) in
126
+
127
+ if error != nil {
128
+
129
+ print("(String(describing: error?.localizedDescription))")
130
+
131
+ }
132
+
133
+ for i in snap!.documentChanges {
134
+
135
+ let id = i.document.documentID
136
+
137
+ let themeImageURL = i.document.get("themeImageURL") as? String ?? ""
138
+
139
+ ...
140
+
141
+
142
+
143
+ self.status.append(ThemeEntity(id: id, themeImageURL: themeImageURL, ...))
144
+
145
+ }
146
+
147
+ }
148
+
149
+ }
150
+
151
+ }
152
+
153
+ }
154
+
155
+
156
+
157
+
158
+
159
+
160
+
161
+ //FIreStoreからfuncでPhotoを取得するObservableObject
162
+
163
+ import Foundation
164
+
165
+ import FirebaseFirestore
166
+
167
+ import SwiftUI
168
+
169
+
170
+
171
+ class PhotoObserver: ObservableObject{
172
+
173
+ @Published var status = [PhotoEntity]()
174
+
175
+
176
+
177
+ func pickup(theme: ThemeEntity) {
178
+
179
+ let db = Firestore.firestore()
180
+
181
+
182
+
183
+ db.collection("Theme").document("(oneTheme.id)").collection("Photos").addSnapshotListener{(snap, error) in
184
+
185
+ if error != nil {
186
+
187
+ print("(String(describing: error?.localizedDescription))")
188
+
189
+ }
190
+
191
+
192
+
193
+ var tempStatus: [PhotoEntity] = []
194
+
195
+
196
+
197
+ for i in snap!.documentChanges {
198
+
199
+ let id = i.document.documentID
200
+
201
+ let title = i.document.get("title") as? String ?? ""
202
+
203
+ ....
204
+
205
+
206
+
207
+ tempStatus.append(PhotoEntity(id: id, title: title,...))
208
+
209
+ }
210
+
211
+ self.status = tempStatus
212
+
213
+ }
214
+
215
+ }
216
+
217
+ }
218
+
219
+
220
+
221
+
222
+
223
+
224
+
225
+
226
+
227
+ ```