Firebase observe new added data even when the function isn't called - swift

I have a function that observes data from my Firebase database. This data will be inserted in an array, so it can be send to my tableviewcell Viewcontroller. All the data will be put correct in the tabelviewcell, but I have a problem when I update my Firebase database. Everytime I change a value in the database it will immediately update my tableView even when my function is not called. I am not sure what I am doing wrong and how to prevent this.
This is my function observe:
Database.database().reference().child("posts").child("\(postId)").child("comments").observe(.value, with: { snapshot in
if let snapshots = snapshot.children.allObjects as? [DataSnapshot] {
for snap in snapshots {
if let postDict = snap.value as? Dictionary<String, AnyObject> {
let key = snap.key
let post = Comment.transformComment(dict: postDict)
self.comments.insert(post, at: 0)
self.tableView.reloadData()
}
}
}
})
Array:
var comments: [Comment] = []
extension Comment {
static func transformComment(dict: [String: Any]) -> Comment {
let comment = Comment()
comment.commentText = dict["commentText"] as? String
comment.uid = dict["uid"] as? String
comment.timestamp = dict["timestamp"] as? Int
comment.likeCount = dict["likeCount"] as? Int
comment.childByAutoId = dict["childByAutoId"] as? String
comment.id = dict["postId"] as? String
return comment
}
}
Tablevieww Functions:
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if comments.count == 0 {
self.tableView.setEmptyMessage("No comments yet!")
} else {
self.tableView.restore()
}
return comments.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "Comment", for: indexPath) as! CommentTableViewCell
let comment = comments[indexPath.row]
cell.comment = comment
cell.delegate = self
return cell
}

To listen once replace
.child("comments").observe(.value, with: { snapshot in
With
.child("comments").observeSingleEvent(of: .value) { snapshot in
Or
.child("comments").observe(.childChanged) { snapshot in
to listen to added childs

Related

Cell casting throws exception

I am trying to load information to a tableView, and I get an exception because some information in the cell isn't initialized when I cast to it. and this is my code :
The code for the view containing the tableView:
private func populateActiveChats()
{
let loggedOnUserID = Auth.auth().currentUser?.uid
let ref = Constants.refs.databaseChatsLite.child(loggedOnUserID!)
ref.observe(.value, with:
{ (snapshot) in
for child in snapshot.children.allObjects as! [DataSnapshot]
{
if (self.chatsDictionary.keys.contains(child.key) == false)
{
let chatValueDictionary = child.value as? NSDictionary
self.AddChatToCollections(chatAsDictionary: chatValueDictionary)
self.DispatchQueueFunc()
}
}
})
}
func AddChatToCollections(chatAsDictionary: NSDictionary!)
{
if chatAsDictionary == nil
{
return
}
let contactName =
chatAsDictionary[Constants.Chat.ChatRoomsLite.CONTACT_NAME] as! String
let newMsgs = chatAsDictionary[Constants.Chat.ChatRoomsLite.NUM_OF_UNREAD_MSGS] as! Int
let contactID = chatAsDictionary[Constants.Chat.ChatRoomsLite.CONTACT_ID] as! String
let chatToAdd = PrivateChatLiteObject(chattingWith: contactName, ContactID: contactID, unreadMessages: newMsgs, LastMSG: "")
chatsDictionary[contactID] = chatToAdd
chatsIndex.append(contactID)
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
{
let cell = ChatsTableView.dequeueReusableCell(withIdentifier: "chat_room_cell", for: indexPath) as! PrivateChatUITableViewCell
let indexedID = chatsIndex[indexPath.row]
cell.ContactName.text = chatsDictionary[indexedID]?.GetContactName()
cell.ContactID = chatsDictionary[indexedID]?.GetContactID()
return cell
}
And in my PrivateChatUITableViewCell:
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
{
let cell = ChatsTableView.dequeueReusableCell(withIdentifier: "chat_room_cell", for: indexPath) as! PrivateChatUITableViewCell
//cell.ContactImageView = UIImageView.loadImageUsingUrlString(contactImg)
let indexedID = chatsIndex[indexPath.row]
cell.ContactName.text = chatsDictionary[indexedID]?.GetContactName()
cell.ContactID = chatsDictionary[indexedID]?.GetContactID()
//cell.PopulateCell()
return cell
}
public func PopulateCell()
{
let currentID = Constants.refs.currentUserInformation?.uid
Constants.refs.databaseChatsLite.child(currentID!).child(ContactID!).observeSingleEvent(of: .value, with: {(snapshot) in ...})
}
The code crashes when it reaches the Constants.refs.databaseChatsLite.child(currentID!).child(ContactID!)
line because ContactID isn't initialized.
This is being called when casting cell to PrivateChatUITableViewCell
I haven't changed my code and this used to work, so I am not sure what changed or what I am doing wrong. Where should my code be fixed?

Swift, Accessing data for a table view

So, I load my firebase node, and then append the data into an array to use in a table view, but for some reason i cannot access the data inside of planitTitles, unless i am within this closure. Please, any workaround ? I feel like i have achieved this before. Thanks
func loadFirebase(){
let ref = Database.database().reference()
let planits = ref.child("planits")
planits.observe( .value, with: { (planitsSnapshot) in
for child in planitsSnapshot.children {
let planSnap = child as! DataSnapshot
let planDict = planSnap.value as! [String: Any]
if self.keyForThisPlanit.contains(planSnap.key){
let title = planDict["Planit Title"] as! String
self.planitTitles.append(title)
}
}
})
print(self.planitTitles)
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
guard let cell = tableView.dequeueReusableCell(withIdentifier: "friendUpdatesCell") as? ViewUpdatesTVCell else { return UITableViewCell() }
cell.randomPlanitNumber.text = planitTitles[indexPath.row]
// CRASHES HERE WITH ERROR OUT OF INDEX
return cell
}
You need to reload the table after the for loop
var planitTitles = [String]()
//
func loadFirebase(){
let ref = Database.database().reference()
let planits = ref.child("planits")
planits.observe( .value, with: { (planitsSnapshot) in
self.planitTitles.removeAll() // you may want to clear here to avoid duplications
for child in planitsSnapshot.children {
let planSnap = child as! DataSnapshot
let planDict = planSnap.value as! [String: Any]
if self.keyForThisPlanit.contains(planSnap.key){
let title = planDict["Planit Title"] as! String
self.planitTitles.append(title)
}
}
print(self.planitTitles)
self.tableView.reloadData()
})
}
func numberOfRows(inSection section: Int) -> Int {
return planitTitles.count // to prevent cellForRowAt indexOutOfBounds crash
}

Filtering data snapshot Swift Firebase

I'm trying to return data from the database which I've done successfully. The problem is I don't want all data to be featured, but only the ones with the current UID which is stored in the JSON tree.
Here is my JSON tree, there is only one current UID but there will be many.
user_posts-
LDLc60j71FBvJGeYn5i
description: "Write here"
photoUrl: "https://firebasestorage.googleapis.com/v0/b/blo..."
uid: "zQRxvM3cwzQMewUtVamk8JQrEFJ3"
Here is my current code returning all data from database folder:
var posts2 = [user_posts]()
func loadPosts(){
Database.database().reference().child("user_posts").observe(.childAdded) {(snapshot: DataSnapshot) in
if let dict = snapshot.value as? [String: Any] {
let descriptionText = dict["description"] as! String
let photoUrlString = dict["photoUrl"] as! String
let post = user_posts(descriptionText: descriptionText, photoUrlString: photoUrlString)
self.posts2.append(post)
self.myPageTableView.reloadData()
}
}
}
override func viewDidLoad() {
super.viewDidLoad()
self.hideKeyboardWhenTappedAround()
myPageTableView.dataSource = self
myPageTableView.delegate = self
loadPosts()
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int
{
return posts2.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
{
let cell = tableView.dequeueReusableCell(withIdentifier:"myPageCell", for: indexPath)
as! myPageTableViewCell
cell.myPageDescription?.text = posts2[indexPath.row].description
let photoUrl = posts2[indexPath.row].photoUrl
let url = URL(string: photoUrl)
cell.myPageImage.sd_setImage(with: url, placeholderImage: nil)
return cell
}
}
To load only the posts for a specific user, you'll want to use a Firebase query:
let uid = Auth.auth().currentUser.uid
let posts = Database.database().reference().child("user_posts")
let query = posts.queryOrdered(byChild: "").queryEqual(toValue: uid)
query.observe(.childAdded) {(snapshot: DataSnapshot) in
...
Also see the Firebase documentation on ordering and filtering data.

Swift - How can I group value array (from dictonary) to multiple section

I am a beginner in Swift. How can I group array list from dictionary? I tried, but it show all list into one section. I can't group, list, sort and show list by the same group.
Image 1
But I can do like this,
Image 2
Here's the code for Todolist array
import Foundation
import Firebase
import FirebaseDatabase
struct TodoList {
var title:String!
var content:String!
var username:String!
var dateLabel:String!
var ref : FIRDatabaseReference?
var key: String!
var picNoteStringUrl : String!
var userImageViewStringUrl : String!
var postId: String!
init(title:String,content:String,username:String,picNoteStringUrl : String,userImageViewStringUrl : String,postId: String,dateLabel:String,key:String="") {
self.title=title
self.content=content
self.username = username
self.dateLabel = dateLabel
self.key=key
self.userImageViewStringUrl = userImageViewStringUrl
self.picNoteStringUrl = picNoteStringUrl
self.postId = postId
self.ref=FIRDatabase.database().reference()
}
init(snapshot:FIRDataSnapshot) {
let value = snapshot.value as? [String: AnyObject]
title = value?["title"] as! String
content = value?["content"] as! String
username = value?["username"] as! String
postId = value?["postId"] as! String
picNoteStringUrl = value?["picNoteStringUrl"] as! String
userImageViewStringUrl = value?["userImageViewStringUrl"] as! String
dateLabel = value?["dateLabel"] as! String
key = snapshot.key
ref = snapshot.ref
}
func toAnyObject() -> [String: AnyObject] {
return ["title": title as AnyObject, "content": content as AnyObject,"username": username as AnyObject,"picNoteStringUrl":picNoteStringUrl as AnyObject,"userImageViewStringUrl": userImageViewStringUrl as AnyObject,"postId":postId as AnyObject,"dateLabel" : dateLabel as AnyObject]
}
}
And here's my code for TableViewController
class TodoListTableViewController: UITableViewController{
var storageRef: FIRStorageReference!
var databaseRef : FIRDatabaseReference!
var todoArray:[TodoList] = []
override func viewDidLoad() {
super.viewDidLoad()
if FIRAuth.auth()?.currentUser==nil{
let vc = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "Home")
self.present(vc,animated: true,completion: nil)
}
else{
let uid = FIRAuth.auth()?.currentUser?.uid
let databaseRef = FIRDatabase.database().reference().child("allTasks").child(uid!)
databaseRef.observe(.value, with: { (snapshot) in
var newItems = [TodoList]()
for item in snapshot.children {
let newTodo = TodoList(snapshot: item as! FIRDataSnapshot)
let letter = newTodo.dateLabel
newItems.insert(newTodo, at: 0)
}
self.todoArray = newItems
DispatchQueue.main.async(execute: {
self.tableView.reloadData()
})
}) { (error) in
print(error.localizedDescription)
}
}
}
override func numberOfSections(in tableView: UITableView) -> Int {
return todoArray.count
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
let todoLine = todoArray[section]
return todoArray.count
}
override func tableView(_ tableView: UITableView, titleForFooterInSection section: Int) -> String? {
let todoLine = todoArray[section]
return todoLine.dateLabel
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! TodoTableViewCell
cell.todoItemName.text = self.todoArray[indexPath.row].title
cell.todoDescription.text = self.todoArray[indexPath.row].content
cell.usernameLabel.text = self.todoArray[indexPath.row].username
let picNoteStringUrl = self.todoArray[indexPath.row].picNoteStringUrl
let userImageViewStringUrl = self.todoArray[indexPath.row].userImageViewStringUrl
FIRStorage.storage().reference(forURL: picNoteStringUrl!).data(withMaxSize: 10 * 1024 * 1024, completion: { (data, error) in
if error == nil {
DispatchQueue.main.async(execute: {
if let picNoteStringUrl = UIImage(data:data!) {
cell.picNote.image = picNoteStringUrl
print("testpass",picNoteStringUrl)
}
})
}else {
print(error!.localizedDescription,"555")
}
})
FIRStorage.storage().reference(forURL: userImageViewStringUrl!).data(withMaxSize: 10 * 1024 * 1024, completion: { (data, error) in
if error == nil {
DispatchQueue.main.async(execute: {
if let userImageViewStringUrl = UIImage(data:data!) {
cell.userImageView.image = userImageViewStringUrl
print("testpass",userImageViewStringUrl)
}
})
}else {
print(error!.localizedDescription,"555")
}
})
return cell
}
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
}
override func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
return true
}
override func tableView(_ tableView: UITableView, editActionsForRowAt indexPath:IndexPath) -> [UITableViewRowAction]? {
let delete = UITableViewRowAction(style: .default, title: "\u{267A}\n Delete") { action, index in
print("more button tapped")
let ref = self.todoArray[indexPath.row].ref
ref?.removeValue()
self.todoArray.remove(at: indexPath.row)
tableView.deleteRows(at: [indexPath], with: .fade)
}
delete.backgroundColor = UIColor.red
let check = UITableViewRowAction(style: .default, title: "\u{2611}\n check") { action, index in
print("edit button tapped")
}
check.backgroundColor = UIColor.orange
return [check, delete]
}
}
}
}
You have to organize your data by section first. I don't see that happening since you simply add all received items into one array.
Based on the screenshot you have provided and the project, it looks as if you are trying to display todo items by date where each section is for a different date. And as far as I can tell, your date value is in the dateLabel property.
If all of the above is correct, then you would need to convert the dateLabel property, which is a String, to an actual Date value so that you can work with the individual dates. Or, depending on how the date string is set up, you might be able to do the same thing by getting just the date component of the string. For example, if your date strings are like "2017-03-31 10:55am" or something, just getting the "2017-03-31" part should allow you to organize the todo items so that all items for the same date can be easily identified.
Once you do that, you have to set up some sort of a structure - if you go with date strings, then a dictionary might work - where you can identify all todo items for a given date. For example, if you have just the date extracted as a string (like "2017-03-31") then you could set up something like this:
var dates = [String]()
var todoItems = [String:[TodoList]]()
The above means that for one string value (which would be a date), you'd have an array of TodoList items. The dates array would be just a convenience so that you can sort the date strings the way you want.
Once you have that, you can modify your table delegate methods to get the count of items in dates to get the sections and the relevant TodoList for each row. Like this:
override func numberOfSections(in tableView: UITableView) -> Int {
return dates.count
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
let date = dates[section]
let array = todoItems[date]
return array.count
}
Hopefully, the above makes sense :)

fatal error: Index out of range, when passing FirebaseData to tablviewCell

so I am working a food list project that I want to pass mealname/mealpic/mealDescription to my tableViewCell from FirebaseDatabase.
it will be something looks like this enter image description here
here's my FirebaesDatabase Structure
enter image description here
and here's my code in the view controller
override func viewDidLoad() {
super.viewDidLoad()
WeekMenu.delegate = self
WeekMenu.dataSource = self
let url = GIDSignIn.sharedInstance().currentUser.profile.imageURL(withDimension: 100)
let data = try? Data(contentsOf: url!)
userpic.image = UIImage(data:data!)
DataService.ds.REF_MENUDATA.observe(.value, with: { (snapshot) in
if let snapshot = snapshot.children.allObjects as? [FIRDataSnapshot]
{
for snap in snapshot {
print("SNAP: \(snap)")
if let menuDict = snap.value as? Dictionary<String, AnyObject> {
let date = snap.key
let menu = menuContnet(weekmenudate: date, menuData: menuDict as! Dictionary<String, String>)
self.menus.append(menu)
}
}
}
})
self.WeekMenu.reloadData()
}
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 5
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let menu = menus [indexPath.row]
if let cell: WeekMenu = tableView.dequeueReusableCell(withIdentifier: "WeekMenu") as! WeekMenu {
cell.configureCell(menu: menu)
return cell
}
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
performSegue(withIdentifier: "ToVari", sender: nil)
}
It crashed at runtime and throws an "Index out of range" error at line let menu = menus [indexPath.row]
I know this Index out of range error question is kind of FAQ
but I did some research myself and they don't really help
could anyone possibly point out what might went wrong ?
thanks
UPDATE
now I successfully pass what I have in FirebaseDatabase into my tableview.
but I have 5 nodes from Day1 to Day5 but my tableview shows only one (Day3)
in my
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return menus.count
}
why is that happening ?
my class of content is like the following
class menuContnet {
private var _menuKey: String!
private var _mealDes : String!
private var _mealPic : String!
private var _mealname : String!
private var _menuRef: FIRDatabaseReference!
var menuKey: String{
return _menuKey
}
var mealDes : String {
return _mealDes
}
var mealname : String {
return _mealname
}
var mealPic : String {
return _mealPic
}
init (mealDes: String, mealname: String, mealPic: String) {
self._mealDes = mealDes
self._mealname = mealname
self._mealPic = mealPic
}
init (menuKey: String, menuData : Dictionary <String, String>) {
self._menuKey = menuKey
if let mealname = menuData["mealname"] {
self._mealname = mealname
}
if let mealPic = menuData["mealPic"] {
self._mealPic = mealPic
}
if let mealDes = menuData["mealDes"] {
self._mealDes = mealDes
}
_menuRef = DataService.ds.REF_MENUS.child(_menuKey)
}
}
UPDATE
if let snapshot = snapshot.children.allObjects as? [FIRDataSnapshot]
{
for snap in snapshot {
print ("SNAP: \(snap.key)")
if let menuDict = snap.value as? Dictionary<String, String> {
let key = snap.key
let menu = menuContnet(menuKey: key, menuData: menuDict)
print (key)
self.menus.append(menu)
self.WeekMenu.reloadData()
so at print ("SNAP: (snap.key)") i get all nodes from Day1 to Day 5
but at print (key) i get only Day3
i think the "if let menuDict" statement is causing the problem
and if i get rid of the "if" and make my code looks like this
let menuDict = snap.value as? Dictionary<String, String>
let key = snap.key
let menu = menuContnet(menuKey: key, menuData: menuDict)
print (key)
self.menus.append(menu)
self.WeekMenu.reloadData()
it crashed at runtime throwing "unexpectedly found nil while unwrapping an Optional value" error
I think I'm getting there, I will keep doing some more research
any tips is appreciated
Try :-
DataService.ds.REF_MENUDATA.observe(.value, with: { (snapshot) in
if let snapshot = snapshot.children.allObjects as? [FIRDataSnapshot]
{
for snap in snapshot {
print("SNAP: \(snap)")
if let menuDict = snap.value as? Dictionary<String, AnyObject> {
let date = snap.key
let menu = menuContnet(weekmenudate: date, menuData: menuDict as! Dictionary<String, String>)
self.menus.append(menu)
self.WeekMenu.reloadData() // This will reload your table to show your newly appended cell one at a time.
}
/*
if menus.count == snap.childrenCount {
self.WeekMenu.reloadData() //Will only reload your table after your entire requested database has been retrieved
}
*/
}
}
})
And change your :-
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return menus.count ?? 0
}