How to chain ObservableObject? - asynchronous

I have a Game-object that may hold an image. Whenever an image URL is found for a game a new instance of GameImage-object should be created. It will then fetch the image and populate the UIImage property. When this happens the UI should be updated presenting the image.
class Game: ObservableObject {
#Published var image: GameImage?
}
class GameImage: ObservableObject {
let url: URL
#Published var image: UIImage?
private var cancellable: AnyCancellable?
init(url: URL) {
self.url = url
}
func fetch() {
self.cancellable = URLSession.shared.dataTaskPublisher(for: self.url)
.map { UIImage(data: $0.data) }
.replaceError(with: nil)
.receive(on: DispatchQueue.main)
.sink(receiveValue: { [weak self] (image) in
guard let self = self else { return }
self.image = image
print(self.url)
print(self.image)
})
}
func cancel() {
cancellable?.cancel()
}
deinit {
cancel()
}
}
struct ContentView: View {
#StateObject var game = Game()
var body: some View {
VStack {
if let image = game.image?.image {
Image(uiImage: image)
} else {
Text("No image.")
}
}
.onAppear(perform: {
guard let gameImageURL = URL(string: "https://cf.geekdo-images.com/itemrep/img/oVEpcbtyWkJjIjk1peTJo6hI1yk=/fit-in/246x300/pic4884996.jpg") else { return }
game.image = GameImage(url: gameImageURL)
game.image!.fetch()
})
}
}
The problem is. After fetch is done the debug console will show that image contains an UIImage. However the UI does not update to show the image. What am I missing here?

There is much more simpler solution than chaining ObservableObject, just separate dependent part into standalone subview... and all will work automatically.
Here is possible approach. Tested with Xcode 12 / iOS 14.
struct ContentView: View {
#StateObject var game = Game()
var body: some View {
VStack {
if nil != game.image {
GameImageView(vm: game.image!)
}
}
.onAppear(perform: {
guard let gameImageURL = URL(string: "https://cf.geekdo-images.com/itemrep/img/oVEpcbtyWkJjIjk1peTJo6hI1yk=/fit-in/246x300/pic4884996.jpg") else { return }
game.image = GameImage(url: gameImageURL)
game.image!.fetch()
})
}
}
struct GameImageView: View {
#ObservedObject var vm: GameImage
var body: some View {
if let image = vm.image {
Image(uiImage: image)
} else {
Text("No image.")
}
}
}

Related

Every time view is seen, the name keeps on adding to itself

I know that this sounds a bit dumb, but how do you call a function only once. I have a tab bar at the bottom of my app, and every time that it is called, the name that I got from my firebase database, keeps on being added. For example, the name in firebase is Bob. The app for the first time will display Bob. Then you would click on the settings, and go back to the home view. Then the app will say BobBob, and over and over again. How do I make this stop.
Code:
import SwiftUI
import Firebase
struct HomeView: View {
#State var name = ""
var body: some View {
NavigationView {
ZStack {
VStack{
Text("Welcome \(name)")
.font(.title)
Text("Upcoming Lessions/Reservations:")
.bold()
.padding()
Divider()
}
}
}
.navigationTitle("Home")
.onAppear(perform: {
downloadNameServerData()
})
}
private func downloadNameServerData() {
let db = Firestore.firestore()
db.collection("users").addSnapshotListener {(snap, err) in
if err != nil{
print("\(String(describing: err))")
return
}
for i in snap!.documentChanges {
_ = i.document.documentID
if let Name = i.document.get("Name") as? String {
DispatchQueue.main.async {
name.append(Name)
print("\(name)")
}
}
}
}
}
}
struct HomeView_Previews: PreviewProvider {
static var previews: some View {
HomeView()
}
}
import SwiftUI
import Firebase
struct HomeView: View {
#State var name = ""
var body: some View {
NavigationView {
ZStack {
VStack{
Text("Welcome \(name)")
.font(.title)
Text("Upcoming Lessions/Reservations:")
.bold()
.padding()
Divider()
}
}
}
.navigationTitle("Home")
.onAppear(perform: {
downloadNameServerData()
})
}
private func downloadNameServerData() {
if !name.isEmpty { return }
let db = Firestore.firestore()
db.collection("users").addSnapshotListener {(snap, err) in
if err != nil{
print("\(String(describing: err))")
return
}
for i in snap!.documentChanges {
_ = i.document.documentID
if let Name = i.document.get("Name") as? String {
DispatchQueue.main.async {
name = Name
print("\(name)")
}
}
}
}
}
}
struct HomeView_Previews: PreviewProvider {
static var previews: some View {
HomeView()
}
}
Did you consider only loading the name if you don't have one yet?
.onAppear(perform: {
if (name == null) downloadNameServerData()
})

Access Data from Firestore and Put it in SwiftUI EnvironmentObject

I am trying to use Firestore and get the data from the Firestore and then put it in EnvironmentObject. But it is not working out. I tried different approaches each ended with different errors.
Here is my code:
class FirebaseManager: ObservableObject {
#Published var tweets: [Tweet] = []
private let db: Firestore = Firestore.firestore()
init() {
db.collection("tweets")
.addSnapshotListener { snapshot, error in
if let error {
print(error.localizedDescription)
return
}
let tweets = snapshot?.documents.compactMap({ document in
try? document.data(as: Tweet.self)
})
if let tweets {
DispatchQueue.main.async {
self.tweets = tweets
}
}
}
}
}
Then I try to call FirebaseManager in the code below:
struct HomeTimelineScreen: View {
#EnvironmentObject var appState: AppState
#StateObject private var firebaseManager = FirebaseManager()
private var cancellables = Set<AnyCancellable>()
init() {
// ERROR Escaping closure captures mutating 'self' parameter
firebaseManager.$tweets.sink { tweets in
appState.tweets = tweets
}
// ERROR/WARNING ObservableObject of type AppState found. A View.environmentObject(_:) for AppState may be missing as an ancestor of this view.
firebaseManager.$tweets.assign(to: \.appState.tweets, on: self)
.store(in: &cancellables)
}
The EnvironmentObject AppState is injected in the TwitterApp main file as shown below:
#main
struct TwitterAppApp: App {
#ObservedObject var coordinator = Coordinator()
init() {
FirebaseApp.configure()
}
var body: some Scene {
WindowGroup {
NavigationStack(path: $coordinator.path) {
LandingScreen()
.navigationDestination(for: Route.self) { route in
switch route {
case .login:
LoginScreen().appLogoToolbar()
case .register:
RegistrationScreen().appLogoToolbar()
case .home:
HomeScreen()
case .detail(let tweet):
TweetDetailsScreen(tweet: tweet)
}
}
}.environmentObject(coordinator)
.environmentObject(AppState())
}
}
}
AppState:
import Foundation
class AppState: ObservableObject {
#Published var tweets: [Tweet] = []
}
UPDATE: My biggest issue is on how to re-render the TweetDetailScreen since it is also using the TweetCellView.
struct TweetDetailsScreen: View {
let tweet: Tweet
var body: some View {
List {
TweetCellView(tweet: tweet)
ForEach(1...20, id: \.self) { index in
Text("\(index)")
}
}
}
}
I took your advice and added single source of truth FirebaseManager and now it works as I want it to be.
struct TweetDetailsScreen: View {
#EnvironmentObject var firebaseManager: FirebaseManager
let tweet: Tweet
var body: some View {
List {
if let tweet = firebaseManager.findByDocumentId(tweet.documentID ?? "") {
TweetCellView(tweet: tweet)
}
ForEach(1...20, id: \.self) { index in
Text("\(index)")
}
}
}
}
class FirebaseManager: ObservableObject {
#Published var tweets: [Tweet] = []
private let db: Firestore = Firestore.firestore()
init() {
db.collection("tweets")
.addSnapshotListener { snapshot, error in
if let error {
print(error.localizedDescription)
return
}
let tweets = snapshot?.documents.compactMap({ document in
try? document.data(as: Tweet.self)
})
if let tweets {
DispatchQueue.main.async {
self.tweets = tweets
}
}
}
}
func findByDocumentId(_ documentId: String) -> Tweet? {
guard let index = tweets.firstIndex(where: { $0.documentID == documentId }) else { return nil }
return tweets[index]
}
}

Updates To Firebase Firestore Document Popping View Off Navigation Stack SwiftUI

I am working on a SwiftUI app and using Firebase Firestore as a backend. I am noticing an odd behavior when I update a document in Firestore that results in a view being popped off the Navigation stack each time a field is updated. The Navigation Stack is as follows.
MarketplaceView with List Containing ListingRowViews
Tapping a row takes you to a ListingDetailView.
Tapping a Buy Now button in ListingDetailView takes you to a ConfirmationView.
When updates are made to the document and 1 or 2 is present, the view updates and all behaves as expected. When 3 (ConfirmationView) is present, any updates to the document result in the ConfirmationView being popped off the stack back to 2 the ListingDetailView. Below is the app architecture.
ListingRepository - Creates a snapshotListener for all Listings in Firestore.
class ListingRepository: ObservableObject {
let db = Firestore.firestore()
private var snapshotListener: ListenerRegistration?
#Published var listings = [Listing]()
private var cancellables = Set<AnyCancellable>()
init() {
startSnapshotListener()
}
func startSnapshotListener() {
// Add a SnapshotListener to the Listing Collection.
self.snapshotListener = db.collection(FirestoreCollection.listings).addSnapshotListener { (querySnapshot, error) in
if let error = error {
print("Error getting documents: \(error)")
} else {
// Check to make sure the Collection contains Documents
guard let documents = querySnapshot?.documents else {
print("No Listings.")
return
}
// Documents exist.
self.listings = documents.compactMap { listing in
do {
return try listing.data(as: Listing.self)
} catch {
print(error)
}
return nil
}
}
}
}
}
MarketplaceViewModel - Subscribes to Listings from ListingRepository and creates ListingRowViewModels.
class MarketplaceViewModel: ObservableObject {
// Properties
#Published var listingRepository: ListingRepository = Resolver.resolve()
// Published Properties
#Published var listingRowViewModels = [ListingRowViewModel]()
// Combine Cancellable
private var cancellables = Set<AnyCancellable>()
// Intitalizer
init() {
self.startCombine()
}
// Starting Combine
func startCombine() {
listingRepository
.$listings
.receive(on: RunLoop.main)
.map { listings in
listings
.map { listing in
ListingRowViewModel(listing: listing)
}
}
.assign(to: \.listingRowViewModels, on: self)
.store(in: &cancellables)
}
}
MarketplaceView - Creates the List full of ListingDetailViews
struct MarketplaceView: View {
#ObservedObject var marketplaceViewModel: MarketplaceViewModel = Resolver.resolve()
var body: some View {
return NavigationView {
List {
ForEach(self.marketplaceViewModel.listingRowViewModels, id: \.id) { listingRowViewModel in
NavigationLink(destination: ListingDetailView(listingDetailViewModel: ListingDetailViewModel(listing: listingRowViewModel.listing))
) {
ListingRowView(listingRowViewModel: listingRowViewModel)
}
} // ForEach
.navigationTitle("Marketplace")
} // NavigationView
} // View
}
}
ListingRowViewModel - View model for each row.
class ListingRowViewModel: ObservableObject, Identifiable {
// Properties
var id: String = ""
// Published Properties
#Published var listing: Listing
// Combine Cancellable
private var cancellables = Set<AnyCancellable>()
// Initializer
init(listing: Listing) {
self.listing = listing
self.startCombine()
}
// Starting Combine
func startCombine() {
$listing
.receive(on: RunLoop.main)
.compactMap { listing in
listing.id
}
.assign(to: \.id, on: self)
.store(in: &cancellables)
}
}
ListingRowView - View for each row.
struct ListingRowView: View {
#ObservedObject var listingRowViewModel: ListingRowViewModel
var body: some View {
Text(self.listingRowViewModel.listingId)
} // View
}
ListingDetailViewModel - View model for the detail view.
class ListingDetailViewModel: ObservableObject, Identifiable {
var listing: Listing
// Initializer
init(listing: Listing) {
self.listing = listing
}
}
ListingDetailView - Detail View For Listing
struct ListingDetailView: View {
var listingDetailViewModel: ListingDetailViewModel
var body: some View {
VStack {
Text(self.listingDetailViewModel.listing.id)
NavigationLink(destination: ConfirmationView(confirmationViewModel: ConfirmationViewModel(listing: listing))) {
Text("Buy Now")
}
}
.navigationTitle("Listing Info")
} // View
}
ConfirmationViewModel - View model for Confirmation View
class ConfirmationlViewModel: ObservableObject, Identifiable {
var listing: Listing
// Initializer
init(listing: Listing) {
self.listing = listing
}
}
ConfirmationView - Confirmation View
struct ConfirmationView: View {
var confirmationViewModel: ConfirmationViewModel
var body: some View {
VStack {
Text(self.confirmationViewModel.listing.id)
Button(action: {
self.confirm()
}, label: {
Text("Confirm")})
}
.navigationTitle("Order Confirmation")
} // View
}
Any help would be greatly appreciated.

finding a nil value in session when accessing value in view

So i have a sessionStore:
class SessionStore: ObservableObject {
var handle: AuthStateDidChangeListenerHandle?
#Published var isLoggedIn = false
#Published var userInSession: User?
func listenAuthenticationState() {
handle = Auth.auth().addStateDidChangeListener({(auth, user) in
if let user = user {
let firestoreGetUser = Firestore.firestore().collection("users").document(user.uid)
firestoreGetUser.getDocument{(document, error) in
if let dict = document?.data() {
guard let decodedUser = try? User.init(fromDictionary: dict) else { return }
self.userInSession = decodedUser
print("decoded user = \(decodedUser)")
}
}
self.isLoggedIn = true
print("user logged in")
} else {
self.isLoggedIn = false
self.userInSession = nil
print("no one logged in")
}
})
}
func logout() {
do {
try Auth.auth().signOut()
} catch {
}
}
func unbind() {
if let handle = handle {
Auth.auth().removeStateDidChangeListener(handle)
}
}
deinit {
unbind()
}
}
Its working as expected, I am able to sign in etc.
I have the following to pull the current user data:
import Foundation
import Firebase
import FirebaseAuth
import FirebaseFirestore
class ProfileViewModel: ObservableObject {
var uid: String = ""
var email: String = ""
var username: String = ""
var profileURL: String = ""
var bio: String = ""
var occupation: String = ""
var city: String = ""
func LoadAUser(userId: String) {
Firestore.firestore().collection("users").document(userId).getDocument{(snapshot, error) in
guard let snap = snapshot else {
print("error fetching data")
return
}
let dict = snap.data()
guard let decodedUser = try? User.init(fromDictionary: dict!) else { return }
print("decoded user - load user - \(decodedUser)")
}
}
}
In my view im trying to call it like:
import SwiftUI
struct ProfileView: View {
#EnvironmentObject var session: SessionStore
#ObservedObject var profileViewModel = ProfileViewModel()
func loadUserData() {
profileViewModel.LoadAUser(userId: session.userInSession!.uid)
}
var body: some View {
VStack {
Text("Edit Profile")
.fontWeight(.semibold)
.font(.system(.title, design: .rounded))
.foregroundColor(Color("startColor"))
Spacer()
VStack(alignment: .leading) {
Text("view")
}.padding()
.onAppear(perform: loadUserData)
}
}
struct ProfileView_Previews: PreviewProvider {
static var previews: some View {
ProfileView()
}
}
Im using .onAppear(perform: loadUserData) which is causing an issue - Thread1: Fatal error: Unexpectedly found nil while unwrapping
I also tried:
init() {
profileViewModel.LoadAUser(userId: session.userInSession!.uid)
}
But this also causes the same error.
The thing is I should only be able to get to this view if I'm logged in as this already works:
struct InitialView: View {
#EnvironmentObject var session: SessionStore
func listen() {
session.listenAuthenticationState()
}
var body: some View {
Group {
if session.isLoggedIn {
MainView()
} else {
NavigationView {
SignUpView()
}
}
}.onAppear(perform: listen)
}
}
I have an initialView()
struct InitialView: View {
#EnvironmentObject var session: SessionStore
func listen() {
session.listenAuthenticationState()
}
var body: some View {
Group {
if session.isLoggedIn {
MainView()
} else {
NavigationView {
SignUpView()
}
}
}.onAppear(perform: listen)
}
}
which takes you to the MainView() which has tabs to control which screen you can navigate to, then from here i can go to ProfileView()
Anyway by the logic of provided code it is more correct to activate isLoggedIn in
let firestoreGetUser = Firestore.firestore().collection("users").document(user.uid)
firestoreGetUser.getDocument{(document, error) in
if let dict = document?.data() {
guard let decodedUser = try? User.init(fromDictionary: dict) else { return }
self.userInSession = decodedUser
print("decoded user = \(decodedUser)")
self.isLoggedIn = true // << here !!
print("user logged in")
}
}
So whats worked for me is passing in Auth instead of session data:
func loadUserData() {
profileViewModel.LoadAUser(userId: Auth.auth().currentUser!.uid)
}

Data not showing in Swiftui using Firebase

Can someone tell me what I am doing wrong? I am using Swiftui and firebase database. I am not seeing any error or any data on the screen. I did install the Pods and checked the security rules as well in console. I tried couple other methods, but this was exactly same from youtube tutorials except the collection name and fields.
import SwiftUI
import Firebase
struct Calories: View {
#ObservedObject var data = getData()
var body: some View {
NavigationView{
ZStack(alignment: .top){
GeometryReader{_ in
// Home View....
Text("Home")
}.background(Color("Color").edgesIgnoringSafeArea(.all))
CustomSearchBar(data: self.$data.datas).padding(.top)
}.navigationBarTitle("")
.navigationBarHidden(true)
}
}
}
struct Calories_Previews: PreviewProvider {
static var previews: some View {
Calories()
}
}
struct CustomSearchBar : View {
#State var txt = ""
#Binding var data : [dataType]
var body : some View{
VStack(spacing: 0){
HStack{
TextField("Search", text: self.$txt)
if self.txt != ""{
Button(action: {
self.txt = ""
}) {
Text("Cancel")
}
.foregroundColor(.black)
}
}.padding()
if self.txt != ""{
if self.data.filter({$0.item.lowercased().contains(self.txt.lowercased())}).count == 0{
Text("No Results Found").foregroundColor(Color.black.opacity(0.5)).padding()
}
else{
List(self.data.filter{$0.item.lowercased().contains(self.txt.lowercased())}){i in
NavigationLink(destination: Detail(data: i)) {
Text(i.item)
}
}.frame(height: UIScreen.main.bounds.height / 5)
}
}
}.background(Color.white)
.padding()
}
}
class getData : ObservableObject{
#Published var datas = [dataType]()
init() {
let db = Firestore.firestore()
db.collection("HSCal").getDocuments { (snap, err) in
if err != nil{
print((err?.localizedDescription)!)
return
}
for i in snap!.documents{
let id = i.documentID
let item = i.get("item") as! String
let cal = i.get("cal") as! String
self.datas.append(dataType(id: id, item: item, cal: cal))
}
}
}
}
struct dataType : Identifiable {
var id : String
var item : String
var cal : String
}
struct Detail : View {
var data : dataType
var body : some View{
Text(data.item)
}
}
did you put app bundle?
try in
struct Calories: View {
#EnvironmentObject var List: getData()
....
}
call
Calories().environmentObject(DataList)
declare somewhere
var DataList = getData()

Resources