Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Firebase Query snapshot nil?

I'm creating a simple chat app to learn Swift and Firebase. I have a query that checks for a chat room's messages to load into a tableView. The query returns a snapshot but I'm having trouble getting the data to store and load it properly. I created a dictionary from the snapshot so I can get the individual child values but they are returning nil:

query.observeSingleEvent(of: .childAdded, with: { snapshot in

            print(snapshot)

            let indexPaths = self.messages.enumerated().map {
                IndexPath(row: $0.0, section: 0)
            }

            guard let messagesDict = snapshot.value as? [String:AnyObject] else { return }
            guard let sender = messagesDict["sender"] as? String else { return }
            guard let text = messagesDict["text"] as? String else { return }
            guard let timestamp = messagesDict["timestamp"] as? Int else { return }

            let message = Message.init(key: snapshot.key, sender: sender as! String, text: text as! String, timestamp: timestamp)

            self.messages.append(message)

            self.tableView.reloadData()
        })

po messagesDict returns data at the breakpoint:

▿ 1 element
  ▿ 0 : 2 elements
    - key : "-KotqLUUucaRagTRt967"
    ▿ value : 3 elements
      ▿ 0 : 2 elements
        - key : sender
        - value : eGTYRSo81JefgasYLRHUFHUTnEC3
      ▿ 1 : 2 elements
        - key : text
        - value : test
      ▿ 2 : 2 elements
        - key : timestamp
        - value : 1499914135546

The app gets to the next breakpoint where I set guard let sender but the app hits return and stops executing the rest of the block of code. How do I properly access sender, text, and timestamp elements so I can set them to variables and store them in the array for my table?

Any help would be greatly appreciated. I've been stuck on this for a while and I could use another pair of eyes!

Thanks!!

Additional info:

Query:

let query = rootRef.child("chat-messages/\(chatKey)")

Database structure:

{
  "chat-messages" : {
    "-KotqKI4zF9cS5XMR9WU" : {
      "-KotqLUUucaRagTRt967" : {
        "sender" : "eGTYRSo81JefgasYLRHUFHUTnEC3",
        "text" : "test",
        "timestamp" : 1499914135546
      }
    },
    "-KotqvBoFFzOTY6Q1fO7" : {
      "-KotqwGAJ85NuAp-Lh3Z" : {
        "sender" : "eGTYRSo81JefgasYLRHUFHUTnEC3",
        "text" : "test",
        "timestamp" : 1499914290279
      }
    },
    "-KotsNgF0ou5MIDn2l5K" : {
      "-KotsOSXw7OS_3xuXZgP" : {
        "sender" : "eGTYRSo81JefgasYLRHUFHUTnEC3",
        "text" : "test",
        "timestamp" : 1499914671997
      },
      "-KotsPqrCpipMLulNNFl" : {
        "sender" : "eGTYRSo81JefgasYLRHUFHUTnEC3",
        "text" : "test44",
        "timestamp" : 1499914677715
      }
    },
    "-Kotsv1Vv19dEF2q7XQu" : {
      "-KotsvYO9JvLY8gdTnA0" : {
        "sender" : "eGTYRSo81JefgasYLRHUFHUTnEC3",
        "text" : "test",
        "timestamp" : 1499914811675
      },
      "-KotsvxKXzIwbyvbwLPp" : {
        "sender" : "eGTYRSo81JefgasYLRHUFHUTnEC3",
        "text" : "test",
        "timestamp" : 1499914813335
      },
      "-Kotsw9vLiEf6KquU__e" : {
        "sender" : "eGTYRSo81JefgasYLRHUFHUTnEC3",
        "text" : "test",
        "timestamp" : 1499914814205
      },
      "-KotswHnzmfkEXih1O7y" : {
        "sender" : "eGTYRSo81JefgasYLRHUFHUTnEC3",
        "text" : "test",
        "timestamp" : 1499914814709
      },
      "-KotszHKBt254jZnUMAl" : {
        "sender" : "eGTYRSo81JefgasYLRHUFHUTnEC3",
        "text" : "test46456",
        "timestamp" : 1499914826967
      }
    },
    "-KoyheS01YxWExSNxl_H" : {
      "-Koyhf3G-FPEDrxQOcpU" : {
        "sender" : "eGTYRSo81JefgasYLRHUFHUTnEC3",
        "text" : "test",
        "timestamp" : 1499995746607
      }
    },
    "-Koyjc_aBaF1kFE-8Bll" : {
      "-Koyjd9PnO6sPOhS7P27" : {
        "sender" : "eGTYRSo81JefgasYLRHUFHUTnEC3",
        "text" : "test",
        "timestamp" : 1499996263096
      },
      "-KoyjdzSEi5twTVQMGOu" : {
        "sender" : "eGTYRSo81JefgasYLRHUFHUTnEC3",
        "text" : "test",
        "timestamp" : 1499996266491
      },
      "-Koyje2C9E524kV7nhLY" : {
        "sender" : "eGTYRSo81JefgasYLRHUFHUTnEC3",
        "text" : "test",
        "timestamp" : 1499996266730
      }
    },
    "-KpfUGw45F1FU2IrR87A" : {
      "sender" : "eGTYRSo81JefgasYLRHUFHUTnEC3",
      "text" : "test",
      "timestamp" : 1500746948396
    },
    "-KpfUHQGuTDi4yZutS5K" : {
      "sender" : "eGTYRSo81JefgasYLRHUFHUTnEC3",
      "text" : "test",
      "timestamp" : 1500746950368
    },
    "-KpfUHdSXeTzauATsDAP" : {
      "sender" : "eGTYRSo81JefgasYLRHUFHUTnEC3",
      "text" : "test",
      "timestamp" : 1500746951277
    },
    "-Kpfia0fXsoqpRtIr1kU" : {
      "sender" : "eGTYRSo81JefgasYLRHUFHUTnEC3",
      "text" : "gerg",
      "timestamp" : 1500750962815
    }
  },
  "chat-participants" : {
    "-KotqKI4zF9cS5XMR9WU" : {
      "eGTYRSo81JefgasYLRHUFHUTnEC3" : {
        "username" : "tester"
      }
    },
    "-KotqvBoFFzOTY6Q1fO7" : {
      "eGTYRSo81JefgasYLRHUFHUTnEC3" : {
        "username" : "tester"
      }
    },
    "-KotsNgF0ou5MIDn2l5K" : {
      "eGTYRSo81JefgasYLRHUFHUTnEC3" : {
        "username" : "tester"
      }
    },
    "-Kotsv1Vv19dEF2q7XQu" : {
      "eGTYRSo81JefgasYLRHUFHUTnEC3" : {
        "username" : "tester"
      }
    },
    "-KoyheS01YxWExSNxl_H" : {
      "eGTYRSo81JefgasYLRHUFHUTnEC3" : {
        "username" : "tester"
      }
    },
    "-Koyjc_aBaF1kFE-8Bll" : {
      "eGTYRSo81JefgasYLRHUFHUTnEC3" : {
        "username" : "tester"
      }
    },
    "-KpfLBjQdPiX2kSrl1yF" : {
      "eGTYRSo81JefgasYLRHUFHUTnEC3" : {
        "username" : "tester"
      }
    }
  },
  "chats" : {
    "-KotqKI4zF9cS5XMR9WU" : {
      "currentName" : "test",
      "date" : 1499914130657
    },
    "-KotqvBoFFzOTY6Q1fO7" : {
      "currentName" : "test",
      "date" : 1499914285904
    },
    "-KotsNgF0ou5MIDn2l5K" : {
      "currentName" : "test",
      "date" : 1499914668844
    },
    "-Kotsv1Vv19dEF2q7XQu" : {
      "currentName" : "test",
      "date" : 1499914809570
    },
    "-KoyheS01YxWExSNxl_H" : {
      "currentName" : "test",
      "date" : 1499995744095
    },
    "-Koyjc_aBaF1kFE-8Bll" : {
      "currentName" : "test",
      "date" : 1499996260740
    },
    "-KpfLBjQdPiX2kSrl1yF" : {
      "currentName" : "test3222",
      "date" : 1500744567786
    }
  },
  "user-chats" : {
    "eGTYRSo81JefgasYLRHUFHUTnEC3" : {
      "-KotqKI4zF9cS5XMR9WU" : {
        "chatKey" : "-KotqKI4zF9cS5XMR9WU",
        "currentName" : "test"
      },
      "-KotqvBoFFzOTY6Q1fO7" : {
        "chatKey" : "-KotqvBoFFzOTY6Q1fO7",
        "currentName" : "test"
      },
      "-KotsNgF0ou5MIDn2l5K" : {
        "chatKey" : "-KotsNgF0ou5MIDn2l5K",
        "currentName" : "test"
      },
      "-Kotsv1Vv19dEF2q7XQu" : {
        "chatKey" : "-Kotsv1Vv19dEF2q7XQu",
        "currentName" : "test"
      },
      "-KoyheS01YxWExSNxl_H" : {
        "chatKey" : "-KoyheS01YxWExSNxl_H",
        "currentName" : "test"
      },
      "-Koyjc_aBaF1kFE-8Bll" : {
        "chatKey" : "-Koyjc_aBaF1kFE-8Bll",
        "currentName" : "test"
      },
      "-KpfLBjQdPiX2kSrl1yF" : {
        "chatKey" : "-KpfLBjQdPiX2kSrl1yF",
        "currentName" : "test3222"
      }
    }
  },
  "user-friends" : {
    "36HDn5XyxjMCcMluJJ9KPBOCDMm1" : {
      "eGTYRSo81JefgasYLRHUFHUTnEC3" : {
        "username" : "tester"
      }
    },
    "eGTYRSo81JefgasYLRHUFHUTnEC3" : {
      "36HDn5XyxjMCcMluJJ9KPBOCDMm1" : {
        "username" : "tester2"
      }
    }
  },
  "user-invites" : {
    "eGTYRSo81JefgasYLRHUFHUTnEC3" : {
      "36HDn5XyxjMCcMluJJ9KPBOCDMm1" : {
        "username" : "tester2"
      }
    }
  },
  "users" : {
    "36HDn5XyxjMCcMluJJ9KPBOCDMm1" : {
      "email" : "[email protected]",
      "username" : "tester2"
    },
    "eGTYRSo81JefgasYLRHUFHUTnEC3" : {
      "email" : "[email protected]",
      "username" : "tester"
    }
  }
}
like image 462
winston Avatar asked Jul 23 '17 14:07

winston


1 Answers

Your dictionary look like :

▿ 1 element
  ▿ 0 : 2 elements
    - key : "-KotqLUUucaRagTRt967"
    ▿ value : 3 elements
      ▿ 0 : 2 elements
        - key : sender
        - value : eGTYRSo81JefgasYLRHUFHUTnEC3
      ▿ 1 : 2 elements
        - key : text
        - value : test
      ▿ 2 : 2 elements
        - key : timestamp
        - value : 1499914135546

But in fact it should look like (If you want to access to dictionary["sender"]) :

▿ 2 elements
  ▿ 0 : 2 elements
    - key : sender
    - value : eGTYRSo81JefgasYLRHUFHUTnEC3
  ▿ 1 : 2 elements
    - key : text
    - value : test
  ▿ 2 : 2 elements
    - key : timestamp
    - value : 1499914135546

First solution :

You need to add .child("-KotqLUUucaRagTRt967") in your query.

OR

Second solution :

You need to do something like that :

query.observe(.childAdded, with: { snapshot in
    for child in snapshot.children {
        guard let value = child.value as? NSDictionary else {
            return
        }

        guard let sender = value["sender"] as? String else {
            return
        }

        // You can user the sender
    }
})

UPDATE :

query.observe(.childAdded, with: { snapshot in
    for child in snapshot.children.allObjects as! [FIRDataSnapshot] {
        if let value = child.value as? [String:Any], let sender = value["sender"] as? String {  
            // You can user the sender
        }
    }
})

NOTE

I replaced observeSingleEvent with observe, as Frank van Puffelen said, it's an uncommon combination for .childAdded.

like image 99
Pipiks Avatar answered Sep 29 '22 09:09

Pipiks