0

I have an element I want removed from Firebase that exists in multiple paths.

What is considered good practice to accomplish this in one transaction?

I searched and only thing I could find was, for example, this :

let dictionary = [ path1 : nil,
                   path2 : nil ]

DatabaseReference.updateChilds(dictionary)

Also, using NSNull() instead of nill didn't work either

But this throws an exception since I cannot use an update method to remove children.

Help would be appreciated.

Edit:

The exception I'm getting:

*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Invalid update: invalid number of items in section 0. The number of items contained in an existing section after the update (1) must be equal to the number of items contained in that section before the update (1), plus or minus the number of items inserted or deleted from that section (0 inserted, 1 deleted) and plus or minus the number of items moved into or out of that section (0 moved in, 0 moved out).

The object is deleted once a button is clicked, and it activates a segue that is connected to this function:

@IBAction func unwindFromDeleteSegue(segue: UIStoryboardSegue)
    {

        if (prodToLoad != nil)
        {
            for  cell in ProductsCollection.visibleCells as! [ProductsCollectionViewCell]
            {
                if ( cell.productUniqueID == prodToLoad?.UniqueID())
                {
                    let indexPath = self.ProductsCollection.indexPath(for: cell)
                    _ = self.ProductsCollection.numberOfItems(inSection: 0)
                    ProductsCollection.deleteItems(at: [indexPath!]) 
                }
            }
        }

        ProductsCollection.reloadData()
    }

Edit:

Items is now properly deleted from collection, but still isn't deleted from Firebase...

Ofri
  • 289
  • 5
  • 17
  • Edited and added the exception :) – Ofri Aug 29 '18 at 17:04
  • The exception is coming from a table view or collection view – Callam Aug 29 '18 at 17:04
  • I am deleting an item from a collection view. So what should I do ? Refresh my collection after deletion ? – Ofri Aug 29 '18 at 17:06
  • You need to keep the data source consistent with the cells – https://stackoverflow.com/questions/46140824/invalid-update-invalid-number-of-items-in-section-0 – Callam Aug 29 '18 at 17:07
  • I would kinda like to mark this as a duplicate of [Using UpdateChildValues to delete from Firebase](https://stackoverflow.com/questions/38462074/using-updatechildvalues-to-delete-from-firebase/38466959#38466959) as the concept still applies. – Jay Aug 30 '18 at 10:43
  • @Jay This is not a duplicate because following the link you gave me didn't solve my problem – Ofri Aug 30 '18 at 14:19
  • Then the problem is unclear as your question states *Good practice of removing multiple paths in Firebase with Swift* and that link does exactly that. If you can refine your question so it's clear what you are asking, we'll take a look! – Jay Aug 30 '18 at 16:17
  • @Jay Better? :) – Ofri Aug 30 '18 at 16:59
  • Not really. It's not clear what the question is, based on the content. Are you having difficulty deleting objects for multiple paths or from multiple paths within a transaction or something else? Does it have to do with the collection? It's really two separate questions. There is no transaction code in the question (if a transaction is the issue) and stating *using NSNull() instead of nill didn't work either* without showing code or the result, and what you are trying to delete makes it difficult to understand the issue. i.e. 'doesn't work' is vague. – Jay Aug 30 '18 at 18:28
  • Oh and as it sits, *DatabaseReference.updateChilds(dictionary)* is not valid. You need to create a reference first to the node you want to modify as a child of your database i.e. *let myFbRef = Database.database().reference()* and then *myFbRef.child("users").delete... or .setValue.... etc*. There may be more code that defines that var but just wanted to add info for clarity. – Jay Aug 30 '18 at 18:33
  • Yeah of course :) I do have the relevant root reference – Ofri Aug 30 '18 at 19:08

1 Answers1

0

Looks like the exception you're seeing is a result of the children successfully being deleted. If you have a some type of reference observer populating your collection view, make sure you're updating the data source to reflect the deleted children.

Callam
  • 11,409
  • 2
  • 34
  • 32