1

In my app I have certain items in my firebase database like this:

Categories
   Category1
      Item1
         id: item1
         name: Item 1 name
         barcode: 473088034839
      item2
         id: item2
         name: Item 2 name
         barcode: 564084724885

These items are in a collectionview. I have another view where I'm using a barcodeScanner to scan the barcode of the products in my database. The barcode scanner works great and I'm able to print the barcode of scanned products to the console, and it matches the codes in "barcode" in my database.

The problem is I'm trying to get the name if the item I'm scanning. With the following code I'm able to find/match the barcode I'm scanning with the code in my database:

let someBarcode = Database.database().reference().child("Categories").queryOrdered(byChild: "barcode").queryEqual(toValue: code)
print(someBarcode)

code is the string of numbers I get from scanning a barcode. After finding the correct barcode in my database, how can I then retrieve the current Items id and/or name?

ked
  • 2,426
  • 21
  • 24
codeski
  • 153
  • 7

2 Answers2

1

Firebase Realtime Database queries work on a flat list. They can order/filter on a property at a fixed path under each direct child under the location you query on.

So in your scenario you could query across all categories on a fixed property of each category, or you can query a specific category for items with a specific property value. But you can't search across all categories for the ones that contain an item with a certain property value.

The solution is to create an data structure that allows the specific query, typically either by flattening the list you already have, or by adding an additional structure to allow the lookup.

For more on this, see:

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • Thanks, I will look more into it. What I’ve understood from those posts so far is that a possible solution would be to create a new db structure “barcodes” at the top level with children with keys of all my items in all categories, and the values would be the codes, would this be correct? – codeski Feb 01 '20 at 16:58
  • Great I'm getting closer now. I tried let item = Database.database().reference().child("barcodes").queryEqual(toValue: code) to get the key of the value/barcode that I scan, but when printing "item" it says (/barcodes { ep = 7054950030184; sp = 7054950030184; }) which is the correct barcodes, but why is it printed twice, why does it say ep/sp = and how can I get the key (item name)? – codeski Feb 01 '20 at 20:48
0

With Franks help I managed to create a new node in my firebase DB with the barcode codes:

Barcodes
item1 : code1
item2 : code2
item3 : code3

Then I used the following function to

func scanner(_ controller: BarcodeScannerViewController, didCaptureCode code: String, type: String) {

   // get product id from Barcodes
    Database.database().reference().child("barcodes")
    .observeSingleEvent(of: .value) { (snapshot) in
        guard let dict = snapshot.value as? [String: Any] else {
            self.showNotFoundMessage()
            return
        }
        for (key, value) in dict {
            let valueString = "\(value)"
            if valueString == code {
                self.getProduct(id: key)
                return
            }
        }

        self.showNotFoundMessage()
    }
}
codeski
  • 153
  • 7