0

This is my code to populate my 'Consumables' array (called in my viewDidLoad):

   //---------------------- POPULATE CONSUMABLE ARRAY --------------------------------//
    private func populateConsumableArray(){
        //let the object populate itself.
       self.ref?.child("Consumables").observe(.childAdded, with: { snapshot in

            let dataChange = snapshot.value as? [String:AnyObject]
            let aRequest = Consumable(aDict: dataChange!)
            self.consumableArray.append(aRequest)

            self.consumableTable.reloadData()
        })
    }

The Consumable object class is shown below

public class Consumable{
    private var type:String
    private var count:String
    private var sku:String

    init (aDict: [String: AnyObject]){
        self.type = aDict["Type"] as! String
        self.count = aDict["Count"] as! String
        self.sku = aDict["SKU"] as! String
    }

The data populates my table view just fine... Below is a picture of the code working... enter image description here

enter image description here

As you can see by the two images above, the code loads the data just fine... The array is populated just fine as well, not shown because it's not directly related to the problem. Below is a picture of the database structure: enter image description here

Now when a new consumable is added via my add function the child added only grabs the first attribute added to the consumable item. (also not included, because I'm sure that is working fine, since it populates the firebase database online, which I will show)

The first attribute being 'Type', I switched the order of how things are added to firebase, added 'Count' first and count ended up being the only attribute grabbed. See the image below for what I mean...

Adding a test consumable: enter image description here

Now you can see that 'Type' is the only attribute being grabbed and stored in the dataChange dictionary, rather than 'Type', 'Count' and 'Sku'. enter image description here

This is what my firebase database looks like at the above breakpoint so I know that consumables are being added just fine to firebase, it's just a problem with grabbing them when a new child is added to the 'Consumable' parent: enter image description here

And then, of course, the failure occurs in my Consumable object init function, since 'Count' and 'Sku' are not in the passed dictionary. See below for the fault: enter image description here

If I close and reload the application, the table view loads with all the data, even the data I previously added that crashed the app. So my question, why does my populateConsumableArray function not grab all the children of the "Consumables" parent?

For the heck of it, I'll add my addConsumableToFirebase() function below, just in case that is the problem for some reason... enter image description here

1 Answer 1

1

Before I start please post code as text not as pictures, so we can actually copy and paste it if needed... (you mostly did but the issue was indeed in the last pic you posted lol)

Now, looking at the Firebase documentation looks like:

setValue(_:) says:

The effect of the write will be visible immediately and the corresponding events will be triggered. Synchronization of the data to the Firebase Database servers will also be started.

Which is why you're seeing it call immediately with only the Type.

Instead try the provided function for updating multiple value at the same time:

func updateChildValues(_ values: [AnyHashable : Any])

Edit: Removed a part about probably causing a retain cycle since you don't capture self weakly, but as pointed out in the comments, it doesn't apply here.

Sign up to request clarification or add additional context in comments.

2 Comments

Just a quick note. Firebase database closures are not escaping and don't require the use of weak. Also, in the question, self is not retaining a reference to the closure as in self.closure = this_closure. self.ref is a reference to the path to Firebase, not to the closure itself. The Firebase Docs show the function structure of observe.
ah this is why I said likely, didn't have full context/nor do I know how firebase works - ill edit thanks for clarification

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.