質問編集履歴
1
文法のの修正、コードの追加
title
CHANGED
@@ -1,1 +1,1 @@
|
|
1
|
-
【SwiftUI】【FireStore】
|
1
|
+
【SwiftUI】【FireStore】FireStoreのデータを取得しながらViewの遷移をしたい
|
body
CHANGED
@@ -1,2 +1,114 @@
|
|
1
|
+
### 前提・実現したいこと
|
2
|
+
|
3
|
+
下記のようなデータ構成でFirestoreにデータが格納してあり、
|
4
|
+
下記のような遷移を実現したいです。
|
5
|
+
感覚的にはよく使いそうな遷移だと思うのですが、どのように実現するのが良いのか分からず、
|
6
|
+
どなたかにご教示いただきたいです。
|
7
|
+
|
8
|
+
|
9
|
+
【データの構成】
|
10
|
+
Collection("Theme")→document()→各documentにCollection("Photo")が含まれる
|
11
|
+
|
12
|
+
【実現したいViewの遷移】
|
13
|
+
Themeの情報が含まれたView→そのThemeのPhotoが表示されるView
|
14
|
+
|
15
|
+
|
16
|
+
### 該当のソースコード
|
17
|
+
|
1
|
-
|
18
|
+
こんなコードを試してみましたが、表示されませんでした。
|
2
|
-
|
19
|
+
この方法でなくても、実現したいViewの遷移ができれば教えていただきたいです。
|
20
|
+
|
21
|
+
```SwiftUI
|
22
|
+
|
23
|
+
//Themeの情報が含まれたView
|
24
|
+
import SwiftUI
|
25
|
+
|
26
|
+
struct ThemeView: View {
|
27
|
+
@ObservedObject var themeList = ThemeObserver()
|
28
|
+
|
29
|
+
var body: some View {
|
30
|
+
VStack{
|
31
|
+
List(themeList.status){ theme in
|
32
|
+
NavigationLink(destination: PhotoView(theme: self.theme)){
|
33
|
+
ThemeView(oneTheme: theme)
|
34
|
+
}
|
35
|
+
}
|
36
|
+
}
|
37
|
+
}
|
38
|
+
}
|
39
|
+
|
40
|
+
//UserのPhotoが表示されるView
|
41
|
+
struct PhotoView: View {
|
42
|
+
var theme : ThemeEntity
|
43
|
+
var ObservedObject var photoList = PhotoObserver()
|
44
|
+
var body: some View {
|
45
|
+
photoList.pickup(theme: theme)
|
46
|
+
|
47
|
+
return ScrollView{//ScrollViewを外すとなぜか表示されるようになる
|
48
|
+
VStack{
|
49
|
+
ForEach(self.status,id: .self){photo in
|
50
|
+
Text(photo.title)//例
|
51
|
+
}
|
52
|
+
}
|
53
|
+
}
|
54
|
+
}
|
55
|
+
}
|
56
|
+
|
57
|
+
//FireStoreからThemeを取得するObservableObject
|
58
|
+
class ThemeObserver:ObservableObject {
|
59
|
+
@Published var status = [ThemeEntity]()
|
60
|
+
|
61
|
+
init() {
|
62
|
+
let db = Firestore.firestore()
|
63
|
+
db.collection("Theme").addSnapshotListener{(snap, error) in
|
64
|
+
if error != nil {
|
65
|
+
print("(String(describing: error?.localizedDescription))")
|
66
|
+
}
|
67
|
+
for i in snap!.documentChanges {
|
68
|
+
let id = i.document.documentID
|
69
|
+
let themeImageURL = i.document.get("themeImageURL") as? String ?? ""
|
70
|
+
...
|
71
|
+
|
72
|
+
self.status.append(ThemeEntity(id: id, themeImageURL: themeImageURL, ...))
|
73
|
+
}
|
74
|
+
}
|
75
|
+
}
|
76
|
+
}
|
77
|
+
}
|
78
|
+
|
79
|
+
|
80
|
+
|
81
|
+
//FIreStoreからfuncでPhotoを取得するObservableObject
|
82
|
+
import Foundation
|
83
|
+
import FirebaseFirestore
|
84
|
+
import SwiftUI
|
85
|
+
|
86
|
+
class PhotoObserver: ObservableObject{
|
87
|
+
@Published var status = [PhotoEntity]()
|
88
|
+
|
89
|
+
func pickup(theme: ThemeEntity) {
|
90
|
+
let db = Firestore.firestore()
|
91
|
+
|
92
|
+
db.collection("Theme").document("(oneTheme.id)").collection("Photos").addSnapshotListener{(snap, error) in
|
93
|
+
if error != nil {
|
94
|
+
print("(String(describing: error?.localizedDescription))")
|
95
|
+
}
|
96
|
+
|
97
|
+
var tempStatus: [PhotoEntity] = []
|
98
|
+
|
99
|
+
for i in snap!.documentChanges {
|
100
|
+
let id = i.document.documentID
|
101
|
+
let title = i.document.get("title") as? String ?? ""
|
102
|
+
....
|
103
|
+
|
104
|
+
tempStatus.append(PhotoEntity(id: id, title: title,...))
|
105
|
+
}
|
106
|
+
self.status = tempStatus
|
107
|
+
}
|
108
|
+
}
|
109
|
+
}
|
110
|
+
|
111
|
+
|
112
|
+
|
113
|
+
|
114
|
+
```
|