skip to Main Content

I am working on an app in swift and need help. I am trying to have a search object on the screen and when you type letters it will bring a result from the realtime database using firebase. Please help. I don’t know how I can code that. Please Please Please Help.

2

Answers


  1. You have to be a bit more specific about what exactly is it that you are trying to do. Firstly – what technologies are you using? JavaScript? What framewroks? React? Angular? Vuejs? Vanilla JavaScript? Is it a restful application you are doing or server rendered?

    Depending on your answers to the above questions the code can vary greatly. Here I can give you a general solution though:

    • first create the search box and a button with an action that sends a request (mostly HTML)
    • that request can be an AJAX GET request to look for results in the database, based on a certain search keyword. I recommend a library called axios to help you with that. (sending and resolving promises with JavaScript)
    • handle the response of that request and if the response is valid (with status 200) then update your search results. (mostly JavaScript)
    Login or Signup to reply.
  2. I just did this a few weeks ago for my app you are in luck! I am using swift programming language btw.

    So according to firebase you should not use firebase to search for documents in the database. Firebase Documentation on this.. Instead you should use something else like "Algolia", "Elastic", or "typeSense". I chose to use Algolia and it took me about a day to implementing my app. Super easy.

    But if you really want to use firebase (which is not recommended) you could use this:

        func loadPost(lastDoc: QueryDocumentSnapshot?, completion: @escaping (QueryDocumentSnapshot?) -> Void) {
        let db: Query
        // Check to see if previous documents have been loaded or not.
        if let lastDoc = lastDoc {
            db = Firestore.firestore().collection("collection name")
                .order(by: "document name you are wanting to get. example: postTitle, username, date, etc")
                // Amount of documents you want to retrieve from your colleciton
                .limit(to: 10)
                // Check certain conditions that you want to retrieve.
                // In this example we are only retrieving the posts that have 100 reports or greater.
                .whereField("reportCount", isGreaterThan: 100)
                // Start after the previous loaded document so we don't waste data by reloading documents that have already been loaded.
                .start(afterDocument: lastDoc)
            
            // If there are no previous loaded documents then you just start from the beginning of your collection colleciton.
        } else {
            db = Firestore.firestore().collection("posts")
                .order(by: "document name", descending: true)
                .limit(to: 10)
                .whereField("document name", isGreaterThan: 100)
        }
        // finally get the documents you want from firebase
        db.getDocuments { query, error in
            
            // Check for errors
            if let query = query, error == nil {
                
                // Not really sure why you need a dispatchgroup, but its kind of like a completionHandler for a for loop.
                let group = DispatchGroup()
                for doc in query.documents {
                    group.enter()
                    // run the code you need to run:
                    
                    
                    // After the code is finished running you need to call group.leave()
                    group.leave()
                    }
                // After the for loop is completely done call group.notify which is basically like a completionHandler
                group.notify(queue: .main) {
                    // notify whatever needs to be notified.
                    
                    // finally insert the last document loaded so when we need to load data again it only loads documents that have not been loaded.
                    completion(query.documents.last)
                }
            } else {
                completion(nil)
            }
        }
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search