Firebase Search: iOS, Swift

I wanted to know if this is possible, and how you can search in firebase. My application has a text box where you can fill in some specifications that are "saved" through the array.

eg:

var arrayOfSpecs = ["13'", "Black"]

Now I want to perform a search using two segmented options:

  • Find the name of one of the Elektronics specs 13 "or black" (this electronic device may have other specs, but must include at least one of these specs)

  • Find the name of one of the Elektronics that has only the specifications that I wrote, fewer specifications in order, but no more specifications than where the person was looking.

Example: search for black 13inch; searchResults can include a macbook with one spec: black, but it can also include a macbook Pro with two specs: black and 13inch, but it can't include a macbook Air with specs: black, 13 inches , "Retina"

SearchResult will be displayed as a table or will store the correct searchResults names in an array, which I would add in a UITableView

Below my database, I hope you understand what I'm looking for.

THX

enter image description here

+4
source share
3 answers

Change the JSON structure to

            Electroniks:{
                  Macbook:{..,
                           Specification:{
                               13inch : true,
                               black : true 
                              },
                            ...
                        },
                   iPhone:{..,
                           Specification:{
                               13inch : true,
                               black : true,
                               camera : true 
                              },
                            ...
                        },
                   iPad:{..,
                           Specification:{
                               xinch : true,
                               red : true,
                               camera : true 
                              },
                            ...
                        }
            }

, node , , , .queryEqualToValue

: - Nodes, , ,

  let parentRef = FIRDatabase.database().reference().child("Elektronics")
parentRef.queryOrderedByChild("Specification/black").queryEqualToValue(true).observeSingleEventOfType(.Value, withBlock: {(snap) in

        if let dict = snap.value as? [String:AnyObject]{

        for each in dict{

            print(each.0) //Would retrieve you every product name with colour black in specification i.e Macbook and iPhone
            print((each.1["Specification"] as! NSDictionary).count)//Number of specification
            parentRef.child("each.0").child("Specification").child("13inch").observeSingleEventOfType(.Value, withBlock: {(snapshot) in

            if snapshot.exists(){

                   print(each.0) //You found your product with exactly 13inch and black Specification.This is their name
                   }

              })

            }
        }else{

            print(snap.ref)

          }
        })
+2

, node . node childByAutoId.

products
  -abcde
    name: MacBook
    color: black
    display: retina
    port: Thunderbolt
    size: 13
  -fghij
    name: MacBook Air
    color: black
    display: standard
    port: USB
    size: 13
  -xxxxx
    name... etc etc

node.

specs
  black
   13
     abcde: true
     fghij: true
   15
     xxxx: true
  grey
   13
    yyyy: true
    zzzz: true
   15
    qqqq: true
  13
     abcde: true
     fghij: true
     yyyy: true
     zzzz: true

, , 13- Mac, SingleEventOfType.Value

specs/black/13

node, abcde fghij.

15- ,

specs/grey/15

node.

, 13-

specs/13

node

- - , , . .

specs
  -abcde
     black_13_retina_SSD_Thunderbolt
     13_retina_SSD_Thunderbolt
     retina_SSD_Thunderbolt
     SSD_Thunderbolt
     Thunderbolt
     .
     .
  -fgijk
     black_13_standard_SSD_USB
     13_standard_SSD_USB
     etc

SSD_Thunderbolt.

, : 13_ : 13 _

, 13_SSD, 3 4 , .

: , , . , , .

, .

0

You can also use an SQL solution instead of Key-Value. Firebase is a Key-Value database.

0
source

Source: https://habr.com/ru/post/1654587/


All Articles