Repeated messages in chatView. how to clear view? - firebase

I have a chatView with a list of chatRow Views (messages)
each chatView has a snapshot listener with firebase, so I should get real time updates if I add a new message to the conversation
The problem I have is: when I add a new message my chatView shows ALL the messages I added before plus the new message, PLUS the same list again....if I add another message then the list repeats again
I assume I need to drop/refresh the previous views shown in the Foreach loop...how can I drop/refresh the view so it can receive refreshed NON repeated data?
struct ChatView: View {
#EnvironmentObject var chatModel: ChatsViewModel
let chat: Conversation
let user = UserService.shared.user
#State var messagesSnapshot = [Message]()
#State var newMessageInput = ""
var body: some View {
NavigationView {
VStack {
ScrollViewReader { scrollView in
ScrollView {
ForEach(chat.messages, id: \.id) { message in
if user.name == message.createdBy {
ChatRow(message: message, isMe: true)
} else {
ChatRow(message: message, isMe: false)
}
}
.onAppear(perform: {scrollView.scrollTo(chat.messages.count-1)})
}
}
Spacer()
//send a new message
ZStack {
Rectangle()
.foregroundColor(.white)
RoundedRectangle(cornerRadius: 20)
.stroke(Color("LightGrayColor"), lineWidth: 2)
.padding()
HStack {
TextField("New message...", text: $newMessageInput, onCommit: {
print("Send Message")
})
.padding(30)
Button(action: {
chatModel.sendMessageChat(newMessageInput, in: chat, chatid: chat.id ?? "")
print("Send message.")
}) {
Image(systemName: "paperplane")
.imageScale(.large)
.padding(30)
}
}
}
.frame(height: 70)
}
.navigationTitle("Chat")
}
}
}
function to add message to the conversation
func addMessagesToConv(conversation: Conversation, index: Int) {
var mensajesTotal = [Message]()
let ref = self.db.collection("conversations").document(conversation.id!).collection("messages")
.order(by: "date")
.addSnapshotListener { querySnapshotmsg, error in
if error == nil {
//loop throug the messages/docs
for msgDoc in querySnapshotmsg!.documents {
var m = Message() //emtpy struc message
m.createdBy = msgDoc["created_by"] as? String ?? ""
m.date = msgDoc["date"] as? Timestamp ?? Timestamp()
m.msg = msgDoc["msg"] as? String ?? ""
m.id = msgDoc.documentID //firebase auto id
mensajesTotal.append(m) //append this message to the total of messages
self.chats[index].messages.removeAll()
self.chats[index].messages = mensajesTotal
}
} else {
print("error: \(error!.localizedDescription)")
}
}
}

You've defined mensajesTotal outside of your snapshot listener. So, it's getting appended to every time.
To fix this, move this line:
var mensajesTotal = [Message]()
to inside the addSnapshotListener closure.

You have two options:
Clear mensajesTotal each time you get an update from the database, as #jnpdx's answer shows.
Process the more granular updates in querySnapshotmsg.documentChanges to perform increment updates in your UI, as also shown in the documentation on detecting changes between snapshots.
There is no difference in the data transferred between client and server between these approaches, so use whatever is easiest (that'd typically be #1) or most efficient on the UI (that's usually #2).

Related

SWIFT UI, when the content of one of my views updates from current view, the current view exits by itself

I have a chat in which there are 4 views.
Settings view
Recent Messages view, where you can see the last message you have received/sent
Contacts view where you just see the contacts you have
Chat log view
When I send message in chat log view, the content in Recent Messages is updated, because the last message is changed, when updating my chat log view exits to main view
Here is my Views:
the main view with tab bar from where 3 other views is accessed
Messages view: where the last messages is shown
Chat Log view
the chat log view exits when I do handleSend function in ChatLogViewModel
struct MainView: View {
#StateObject var vm = MainMessagesViewModel() //view model variable
#State private var isPresented = false //bool variable to show/hide login view
var body: some View {
NavigationView {
TabView(selection: .constant(0)) {
ContactsView()
.tabItem {
Label("მეგობრები", systemImage: "person.2")
}
.tag(0)
MessagesView().environmentObject(vm)
.tabItem {
Label("ჩატები", systemImage: "message")
}
.tag(1)
SettingsView().environmentObject(vm)
.tabItem {
Label("დაყენებები", systemImage: "gear")
}
.tag(2)
}
.fullScreenCover(isPresented: $isPresented, onDismiss: nil){ //iOS 16 style to call a view with #published variable (#state needed)
LogInView(didCompleteLogin: {
self.vm.isCurrentlyLogOut = false
self.vm.fetchCurrentUser()
})
}
.onReceive(vm.$isCurrentlyLogOut) { isCurrentlyLogOut in
self.isPresented = isCurrentlyLogOut
self.vm.fetchCurrentUser()
}
.onChange(of: isPresented) { isCurrentlyLogOut in
self.vm.isCurrentlyLogOut = isCurrentlyLogOut
self.vm.fetchCurrentUser()
}
}
}
}
class MainMessagesViewModel: ObservableObject {
#Published var user : User? //current user (optional because is nil on start)
#Published var isCurrentlyLogOut = false
#Published var recentMessages = [RecentMessage]() //array containing recent messages user has sent
private var firestoreListener : ListenerRegistration?
init() {
DispatchQueue.main.async {
self.isCurrentlyLogOut = FirebaseManager.shared.auth.currentUser?.uid == nil //figure out if there is no user logged in
}
fetchCurrentUser() //fetch a logged in user
fetchRecentMessages()
}
func fetchCurrentUser() { //fetching the user variable inside vm
guard let uid = FirebaseManager.shared.auth.currentUser?.uid else { return } //if there is a uid available create uid variable
FirebaseManager.shared.firestore.collection("users").document(uid).getDocument { snapshot, err in //retrieve from users/uid/ user's data, which is stored in snapshot
if let err = err { //error handler
print("Failed to fetch current user: ", err)
return
}
guard let data = snapshot?.data() else { return } //create a data variable if there is a data in snapshot
self.user = .init(data: data) //vm's user is initialized with struct's init()
}
}
private func fetchRecentMessages() {
guard let uid = FirebaseManager.shared.auth.currentUser?.uid else {return}
FirebaseManager.shared.firestore
.collection("recent_messages")
.document(uid)
.collection("messages")
.addSnapshotListener { querySnapshot, err in
if let err = err {
print(err)
return
}
querySnapshot?.documentChanges.forEach({ change in
let docId = change.document.documentID
let data = change.document.data()
if let index = self.recentMessages.firstIndex(where: {rm in
return rm.documentId == docId
}) {
self.recentMessages.remove(at: index)
}
self.recentMessages.insert(.init(documentId: docId, data: data), at: 0)
})
}
}
}
//MARK: - CHAT LOG VIEW
struct ChatLogView: View {
#ObservedObject var vm: ChatLogViewModel //view model variable
let interlocutor: User? //variable for interlocutor (provided from main view)
init(interlocutor: User?) { //initialize interlocutor
self.interlocutor = interlocutor
self.vm = .init(interlocutor: interlocutor) //initialize view model using interlocutor
}
var body: some View {
VStack{
messagesView
}
.navigationTitle(interlocutor?.email ?? "")
}
//MARK: - MESSAGES VIEW
private var messagesView: some View {
ScrollView {
ScrollViewReader { scrollViewProxy in
VStack {
ForEach(vm.chatMessages) { message in
VStack {
if message.fromId == FirebaseManager.shared.auth.currentUser?.uid{
HStack {
Spacer()
HStack {
Text(message.text)
.foregroundColor(.white)
}
.padding(.vertical, 5)
.padding(.horizontal, 15)
.background(Color.blue)
.cornerRadius(25)
}
.padding(.horizontal)
} else {
HStack {
HStack {
Text(message.text)
.foregroundColor(.black)
}
.padding(.vertical, 5)
.padding(.horizontal, 15)
.background(Color(.init(white: 0.94, alpha: 1)))
.cornerRadius(25)
Spacer()
}
.padding(.horizontal)
}
}
}
HStack {
Spacer()
}
.id("empty")
}
.onReceive(vm.$count) { _ in //when count of messages changes
withAnimation(.easeOut(duration: 0.5)) { //perform animation to scroll to the bottom of view
scrollViewProxy.scrollTo("empty", anchor: .bottom)
}
}
}
}
.safeAreaInset(edge: .bottom) { //insert a view to the bottom
chatBottomBar
}
}
//MARK: - CHAT BOTTOM BAR VIEW
private var chatBottomBar: some View {
HStack {
Image(systemName: "paperclip")
.padding(.leading, 5)
TextField("Message", text: $vm.chatText)
.padding(.vertical, 5)
.padding(.horizontal, 15)
.background(Color.white)
.cornerRadius(25)
Button {
vm.handleSend()
} label: {
Image(systemName: "arrow.up.circle.fill")
.font(.title)
}
}
.padding(.horizontal, 5)
.padding(.vertical, 7)
.background(Color(.init(white: 0.95, alpha: 0.99)))
}
}
class ChatLogViewModel: ObservableObject {
let interlocutor : User? //user whom you send a message
#Published var chatText = "" //var for a text of message
#Published var chatMessages = [ChatMessage]() //array of messages
#Published var count = 0 //var to track when a message is added to scroll down
init (interlocutor : User?) {
self.interlocutor = interlocutor //init a interlocutor using a interlocutor from contacts view
fetchMessages() //fetch messages from firestore to the app
}
func fetchMessages() {
guard let fromId = FirebaseManager.shared.auth.currentUser?.uid else {return} //create a fromID using current user's if available
guard let toId = interlocutor?.uid else {return} //create a toID using interlocutor's id
FirebaseManager.shared.firestore.collection("messages") //retrieve message data from path /messages/fromId/toId/ (ordered by time)
.document(fromId)
.collection(toId)
.order(by: "timestamp")
.addSnapshotListener { querySnapshot, err in //continiously listen to selected folder, the data will be in querySnapshot
if let err = err { //error handler
print("Failed to listen messages: ", err)
}
querySnapshot?.documentChanges.forEach({ change in //if there is a change of documents in querySnapshot
if change.type == .added {
let data = change.document.data() //create data variable
let docId = change.document.documentID //create docId variable
let chatMessage = ChatMessage(documentId: docId, data: data) //create chatMessage variable (initilize it with stucts's init using docId and data)
self.chatMessages.append(chatMessage) //add a message to messages array
}
})
DispatchQueue.main.async { //if message is added increase messages count (for scroll to bottom)
self.count += 1
}
}
}
func handleSend() { //function to send a message
guard let fromId = FirebaseManager.shared.auth.currentUser?.uid else {return} //if there is current user's id store it in fromId
guard let toId = interlocutor?.uid else {return} //if there is interculor store their uid in toId
let document = FirebaseManager.shared.firestore.collection("messages") //create a message document |FOR SENDER| in message/fromId/toId/
.document(fromId)
.collection(toId)
.document()
let messageData = [ //create a message data variable
"fromId" : fromId,
"toId" : toId,
"text" : chatText,
"timestamp" : Timestamp()
] as [String : Any]
document.setData(messageData) { err in //for the document created set data using created messageData
if let err = err { //error handler
print("Failed to send a message: ", err)
return
}
self.persistRecentMessage()
self.chatText = "" //when the handleSend() is called clean chatText variable as so textFiled
self.count += 1 //increase message count (for scroll down)
}
let recepientMessageDocument = FirebaseManager.shared.firestore.collection("messages") //create a message document |FOR RECEPINT| in messages/toId/fromId/
.document(toId)
.collection(fromId)
.document()
recepientMessageDocument.setData(messageData) { err in //for the document created set data using created messageData
if let err = err { //don't have to create another message data because it is the same
print("Failed to send a message: ", err)
return
}
}
}
private func persistRecentMessage() {
guard let uid = FirebaseManager.shared.auth.currentUser?.uid else {return} //create uid if uid of current user is available
guard let toId = interlocutor?.uid else {return}
let document = FirebaseManager.shared.firestore.collection("recent_messages") //create document to store the message to /recent_messages/uid/messages/toId
.document(uid)
.collection("messages")
.document(toId)
let data = [ //recent message data variable
"timestamp" : Timestamp(),
"text" : self.chatText,
"formId" : uid,
"toId" : toId,
"profilePicURL": interlocutor?.profilePictureUrl ?? "",
"email" : interlocutor?.email ?? ""
] as [String : Any]
document.setData(data) { err in //set recent message to created document
if let err = err {
print("Faild to store recent message: ", err)
return
}
}
let recepientData = [ //recent message data variable
"timestamp" : Timestamp(),
"text" : self.chatText,
"formId" : uid,
"toId" : toId,
"profilePicURL": "",
"email" : ""
] as [String : Any]
let recepientRecentMessageDocument = FirebaseManager.shared.firestore
.collection("recent_messages") //create a recent message document |FOR RECEPINT| in /recent_messages/toId/messages/uid
.document(toId)
.collection("messages")
.document(uid)
recepientRecentMessageDocument.setData(data) { err in //for the document created set data using created messageData
if let err = err { //dont have to create another message data because it is the same
print("Failed to send a message: ", err)
return
}
}
}
}
struct MessagesView: View {
#EnvironmentObject var vm : MainMessagesViewModel
var body: some View{
ScrollView {
ForEach (vm.recentMessages) { recentMessage in
VStack {
NavigationLink {
Text("")
} label: {
HStack {
WebImage(url: URL(string:recentMessage.profilePictureUrl))
.resizable()
.scaledToFill()
.frame(width: 64, height: 64)
.clipShape(Circle())
.padding(.horizontal)
VStack (alignment: .leading) {
Text(recentMessage.email)
Text(recentMessage.text)
}
Spacer()
Text("date")
}
.padding(.horizontal)
Divider()
}
}
}
}
.navigationBarTitle(Text("მეგობრები"), displayMode: .inline)
}
}
I need to chat log view do not exit when updating contents in messages view.
When I commented out self.persistRecentMessage() from func handleSend() function in ChatLogViewModel, disabling in this way updating of contents in Messages View, ChatLogView stopped exiting

Swiftui Force Update View

In my content view I have a home page with some text that says "Welcome, xxxx" where xxxx is the name fetched from a firebase database. This field can be changed in the settings page that is navigated to via a Navigation Link. When the name is changed and saved the name on the home page only updates when you force shutdown the app. How do I force update the view when you press the back button from settings.
This is how I display the field:
Text("Welcome, \(companyName)")
.font(.system(size: 23))
.bold()
.foregroundColor(Color("background"))
.padding(.bottom, 50)
This is how I set a value to companyName:
func SetData() {
var db = Firestore.firestore()
let user = Auth.auth().currentUser
let userName = user?.email ?? ""
let docRef = db.collection("CONTACT").document(userName)
docRef.getDocument { (document, error) in
if let document = document, document.exists {
//Setting Values
let data = document.data()
self.companyName = data?["companyName"] as? String ?? ""
} else {
print("Document does not exist")
}
}
}
There are several solutions to this, but you haven't provided enough code outlining what you have done to modify the variable companyName. The easiest solution would be to pass companyName as a binding value into the settings.
What I imagine here is that your HomeView is fetching the data on launch. In the settings, a change data request is made, but nothing is done to update the data in the HomeView. By using a binding variable we can ensure that the companyName connects to the source of truth in the HomeView, and so the function modifies the companyName which is precisely the company name on the HomeView vs. modifying potentially the value of companyName.
struct HomeView: View {
#State var companyName = "Microsoft"
var body: some View {
NavigationView {
NavigationLink(destination: SettingsView(companyName: $companyName)) {
Text("Tap to navigate to Settings")
}
}
}
}
struct SettingsView: View {
#Binding var companyName : String
var body: some View {
Button {
SetData()
} label: {
HStack {
Text("Tap to change!")
Text("\(companyName)!")
}
}
}
func SetData() {
var db = Firestore.firestore()
let user = Auth.auth().currentUser
let userName = user?.email ?? ""
let docRef = db.collection("CONTACT").document(userName)
docRef.getDocument { (document, error) in
if let document = document, document.exists {
//Setting Values
let data = document.data()
self.companyName = data?["companyName"] as? String ?? ""
} else {
print("Document does not exist")
}
}
}
}
If you have already done this at it doesn't somehow work, another solution is to add an .onAppear modifier to your HomeView.
struct HomeView: View {
#State var companyName = "Microsoft"
var body: some View {
VStack {
// code ...
}
.onAppear {
fetchData()
}
}
func fetchData() {
// code that returns companyFetchedName
self.companyName = companyFetchedName
}
}
Modify it on main queue, like
docRef.getDocument { (document, error) in
if let document = document, document.exists {
//Setting Values
let data = document.data()
DispatchQueue.main.async { // << here !!
self.companyName = data?["companyName"] as? String ?? ""
}
} else {
print("Document does not exist")
}
}

ObservedObject only passes its default value; not its assigned value. Why?

Scenario: Attempting to broadcast a variable value via an ObservableObject.
Problem: I'm only getting the default value; not the assigned value.
Here's the origin.
Button #1 starts a function to get data.
Button #2 retrieves the ObservedObject's revised value
I removed some of the vestigial code to make the presentation simpler:
struct ContentView: View {
#ObservedObject var networkManager = NetworkManager()
let fontCustom = Font.custom("Noteworthy", size: 23.0)
var body: some View {
ZStack {
// ...
// ...
HStack {
Button(
action: {
NetworkManager().getCalculatorIDs()
},
label: {
Text("1")
}
)
Button(
action: {
self.calculator.calculate("2");
print(self.networkManager.calculationID) // stop and check.
},
label: { Text("2") }
)
// ...
// ...
}
}
So I tap Button #1 then tap Button #2 to check if the ObservedObject has the generated id value.
I'm expecting an alphanumeric id value in the print().
Instead, I got the original value:
Royal Turkey
(lldb)
Here's the ObservableObject:
struct CalculationIdentifier: Decodable {
let id: String
let tokens: [String]
}
class NetworkManager: ObservableObject {
#Published var calculationID = "Royal Turkey"
#Published var isAlert = false
#Published var name = "Ric Lee"
let calculations = "https://calculator-frontend-challenge.herokuapp.com/Calculations"
func getCalculatorIDs() {
let urlRequest = URLRequest(url: URL(string: calculations)!)
let configuration = URLSessionConfiguration.ephemeral
let task = URLSession(configuration: configuration).dataTask(with: urlRequest) { data, _, error in
DispatchQueue.main.async {
do {
let result = try JSONDecoder().decode([CalculationIdentifier].self, from: data!)
if !result.isEmpty {
self.calculationID = (result[0] as CalculationIdentifier).id
print("Inside do{}. result = \(result)")
self.isAlert = true
} else {
print(#function, "Line:", #line, ": No Result")
}
} catch {
print(error)
}
}
}
task.resume()
}
}
BTW: Here's the local console output, the string value of 'id' should have been passed to the host as an ObservedObject value:
Inside do{}. result = [RicCalculator2.CalculationIdentifier(id: "d3dd3b1e-d9f6-4593-8c85-b8fd3d018383", tokens: [])]
So I do have a bona fide id value to send.
Why only the original value?
What am I missing?
...do I need to do a 'send' or something?
This
A. #ObservedObject var networkManager = NetworkManager()
and this
B. NetworkManager().getCalculatorIDs()
in your code are different objects, ie. you create one object as member, then other object on the stack, which does something, and then ask first object to return something - naturally if returns what it has on initialise.
Probably you assumed in case B
self.networkManager.getCalculatorIDs()

SwiftUI - Simple Firestore Query - Display Results

Ok, I'll preface this with the fact that I'm new to SwiftUI and programming. I'm a UX Designer. However, I'm trying to run a simple Firestore query and return the results to a list.
I've been able to write a function that writes the results to the console successfully, but I have no idea how to access the information that's within the function so that I can use it within the main view of the page.
I've started a simple view so that I can just focus on displaying Firestore data in a list. Here's my barebones code currently.
import SwiftUI
import FirebaseFirestore
struct FestivalListFB: View {
let db = Firestore.firestore()
func getVenues() {
let db = Firestore.firestore()
db.collectionGroup("Venues").getDocuments() {(querySnapshot, err) in
if let err = err {
print("Error getting documents \(err)")
} else {
for document in querySnapshot!.documents {
guard let venueEntry = document.get("venueTitle") as? String else {
continue
}
print(venueEntry)
}
}
}
}
var body: some View {
VStack {
List(0 ..<5) { item in
Text("Hello, World!")
}
}.onAppear(perform: getVenues)
}
}
Console displays:
"Refreshment Outpost
Holiday Sweets & Treats
Fife & Drum Tavern
L'Artisan des Glaces
Shi Wasu
...etc"
And of course, the body only displays "Hello World" 5 times within a list. How would I go about accessing the values in "venueEntry" so that I can display it within the list element?
I've included a image of my Firestore data structure as well. Ideally, I'd like to display the venues grouped by the "venueArea" they are located in.
For easier use, i created a model for you venue. See the below snippet how you can show your data in your View.
Your model:
class VenueObject: ObservableObject {
#Published var venueID: String
#Published var venueTitle: String
#Published var venueArea: String
init(id: String, title: String, area: String) {
venueID = id
venueTitle = title
venueArea = area
}
}
Your View:
struct FestivalListFB: View {
#State var data: [VenueObject] = []
let db = Firestore.firestore()
var body: some View {
VStack {
ForEach((self.data), id: \.self.venueID) { item in
Text("\(item.venueTitle)")
}
}.onAppear {
self.getVenues()
}
}
func getVenues() {
// Remove previously data to prevent duplicate data
self.data.removeAll()
self.db.collectionGroup("Venues").getDocuments() {(querySnapshot, err) in
if let err = err {
print("Error getting documents \(err)")
} else {
for document in querySnapshot!.documents {
let id = document.documentID
let title = document.get("venueTitle") as! String
let area = document.get("venueArea") as! String
self.data.append(VenueObject(id: id, title: title, area: area))
}
}
}
}
}

Firebase deleted data still there

i have a behavior that I can't understand.
I delete a node on firebase database and I still receive the data during observing .value. But in firebase database the node is deleted.
I have a node called users_shoppinglists. Here are all id's of the users nodes to observe stored. Then I iterate all the id's to observe and call a function that observes each ID.
When I need to delete a list I update a node called status on the shoppinglists node and delete all to this list related data via cloud functions.
But the data is still received during observe. It seems I receive the data again before it is completely deleted.
Iterate all id's:
func ObserveAllList() -> Void{
if currentUser == nil { return }
self.ShowActivityIndicator()
ref.child("users_shoppinglists").child(currentUser!.id!).observe(.value, with: { (usersListsSnap) in
if usersListsSnap.value is NSNull { self.HideActivityIndicator(); return }
for listSnap in usersListsSnap.children {
let list = listSnap as! DataSnapshot
self.ObserveSingleList(listID: list.key)
}
}) { (error) in
NSLog(error.localizedDescription)
let title = String.OnlineFetchRequestError
let message = error.localizedDescription
self.ShowAlertMessage(title: title, message: message)
return
}
}
Call function to observe each ID:
func ObserveSingleList(listID:String) -> Void {
self.ShowActivityIndicator()
ref.child("shoppinglists").child(listID).observeSingleEvent(of: .value, with: { (snapshot) in
if snapshot.value is NSNull { self.HideActivityIndicator(); return }
//Read listData
var newList = ShoppingList()
newList.id = snapshot.key
newList.name = snapshot.childSnapshot(forPath: "listName").value as? String
newList.owneruid = snapshot.childSnapshot(forPath: "owneruid").value as? String
newList.relatedStore = snapshot.childSnapshot(forPath: "relatedStore").value as? String
//Read List items
self.ref.child("listItems").child(listID).observe(.value, with: { (itemSnap) in
var newItems = [ShoppingListItem]()
for items in itemSnap.children {
let item = items as! DataSnapshot
var newItem = ShoppingListItem()
newItem.id = item.key
newItem.listID = listID
newItem.isSelected = item.childSnapshot(forPath: "isSelected").value as? Bool
newItem.itemName = item.childSnapshot(forPath: "itemName").value as? String
newItem.sortNumber = item.childSnapshot(forPath: "sortNumber").value as? Int
newItems.append(newItem)
}
newList.items = newItems
//Read List members
self.ref.child("shoppinglist_member").child(listID).observe(.value, with: { (memberSnap) in
var newMembers = [ShoppingListMember]()
for members in memberSnap.children {
let member = members as! DataSnapshot
var m = ShoppingListMember()
m.memberID = member.key
m.status = member.value as? String
newMembers.append(m)
}
newList.members = newMembers
DispatchQueue.main.async {
if let index = allShoppingLists.index(where: { $0.id == listID }){
allShoppingLists[index] = newList
} else {
allShoppingLists.append(newList)
}
self.HideActivityIndicator()
NotificationCenter.default.post(name: Notification.Name.ShoppingBuddyListDataReceived, object: nil, userInfo: nil)
}
}, withCancel: { (error) in
self.HideActivityIndicator()
NSLog(error.localizedDescription)
let title = String.OnlineFetchRequestError
let message = error.localizedDescription
self.ShowAlertMessage(title: title, message: message)
return
})
}, withCancel: { (error) in
self.HideActivityIndicator()
NSLog(error.localizedDescription)
let title = String.OnlineFetchRequestError
let message = error.localizedDescription
self.ShowAlertMessage(title: title, message: message)
return
})
}) { (error) in
self.HideActivityIndicator()
NSLog(error.localizedDescription)
let title = String.OnlineFetchRequestError
let message = error.localizedDescription
self.ShowAlertMessage(title: title, message: message)
return
}
}
Cloud function:
//****************************************************************************************************************/
// Handles an action when status value changed in users_shoppinglists node
//****************************************************************************************************************/
exports.handle_ListStatusUpdate = functions.database.ref('/shoppinglists/{listID}').onUpdate(event => {
var listData = event.data.val()
console.log('Status', listData.status)
//handle deleted by owner
if (String(listData.status) == 'deleted by owner') {
//Get all members to delete the list on their users_shoppinglists node
return admin.database().ref('shoppinglist_member').child(event.params.listID).once('value').then(listMember => {
var promises = []
listMember.forEach(function (member) {
promises.push(admin.database().ref('users_shoppinglists').child(member.key).child(event.params.listID).set(null).then(() => {
return admin.database().ref('shoppinglist_member').child(event.params.listID).set(null).then(() => {
// delete the original shopping list
return admin.database().ref('shoppinglists').child(event.params.listID).set(null).then(() => {
return admin.database().ref('listItems').child(event.params.listID).set(null).then(() => {
})
})
})
}))
})
})
}
});/*********************************************************************************************************** */
Had this issue on Simulator. It was not only .value but .childRemoved and .childChanged were not triggered at all (only .childAdded was working).
Tried on iPhone and it worked. Then I made "Erase All Content And Settings..." to Simulator and it started to work again on Simulator too.
My bet is that firebase cache gets dirty during development, while you add or remove observers in code and probably change structure in database and at some point it just stops reacting appropriately.

Resources