'Firestore : How can I get whole data in Firestore?
This ViewModel is about saving data in Firestore with specific data types.
import Foundation
import Firebase
import FirebaseStorage
import FirebaseStorageSwift
import UIKit
class UploadViewModel : ObservableObject {
@Published var loading : Bool = false
func storeImageWithUrl(images : [UIImage], completion : @escaping (_ urls : [String]) -> ()) {
self.loading = true
var count = 0
var urls : [String] = []
for image in images {
let ref = Storage.storage().reference(withPath: UUID().uuidString)
guard let imageData = image.jpegData(compressionQuality: 0.5) else {return}
ref.putData(imageData, metadata: nil) { metaData, error in
if let error = error {
print("failed to push image cause of error")
return
}
ref.downloadURL { url, error in
if let error = error {
print("error to make url")
return
}
guard let url = url else {return}
count += 1
urls.append(url.absoluteString)
if count == images.count {
completion(urls)
}
}
}
}
}
func storeItemInformation(title : String, description : String, category : String, contactInfo : String, price : String, imageUrls : [String], timeStamp : Date = Date(), saved : Bool = false, seller : String, completion : @escaping (_ result : Bool) -> ()) {
let uid = AuthService.instance.makeUid()
guard let userData = ["title": title, "description" : description, "category" : category, "price" : price, "imageURL" : imageUrls, "timestamp" : timeStamp, "contactInfo" : contactInfo, "saved" : saved, "seller" : seller] as? [String : Any] else { return }
Firestore.firestore()
.collection("Wholeitems")
.document(category)
.collection(uid)
.document(title)
.setData(userData) { error in
if let error = error {
print("Error to save whole Data")
completion(false)
return
}
print("Success to save whole data")
self.loading = false
completion(true)
}
}
}
Below code is about fetching data from Firestore with ItemModel.
import Foundation
import SwiftUI
import Firebase
import FirebaseAuth
class FeedViewModel : ObservableObject {
@Published var feeds : [ItemModel] = []
init() {
fetchItems()
print(feeds)
}
func fetchItems() {
Firestore.firestore()
.collection("Wholeitems")
.getDocuments { snapshot, error in
if let error = error {
print("error to get data")
return
}
if let snapshot = snapshot {
DispatchQueue.main.async {
self.feeds = snapshot.documents.map({ d in
return ItemModel(id: d.documentID,
category: d["category"] as? String ?? "",
contactInfo: d["contactInfo"] as? String ?? "",
description: d["description"] as? String ?? "",
price: d["price"] as? String ?? "",
timestamp: d["timestamp"] as? String ?? "",
title: d["title"] as? String ?? "",
saved : d["saved"] as? Bool ?? false,
seller: d["seller"] as? String ?? "",
imageURL: d["imageURL"] as? [String] ?? []
)
})
}
}
}
}
}
The problem is that I don't know why the feeds array is empty.
I tried to get all of the documents in the Firestore collection named Wholeitems. But, the array is empty.
Could you help me? Thanks!
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
