swiftmongodbparse-platformpfquery

Swift Parse PFQuery is showing no results with "equalTo" or "contains"


I have an issue I cannot seem to resolve nor find the answer to. I am developing an iOS App in Swift using the Parse Library.

I have a Mongo DB thru Heroku and am able to write data to it using Parse with no issues. Now I need to search (query) the datastore. I need to be able to search for any name in the "column" called 'name', not just an exact title.

For example, "Ticket to Ride" is one name where "Ticket to Ride: Europe" is another. If a user searches for "Ticket to Ride", I need both to show in the query.

Right now, neither the "whereKey contains" nor "whereKey equalTo" is returning results.

let gamesQuery = PFQuery(className: "games")
print ("Searching for \(gameTitle)")
gamesQuery.whereKey("name", contains: gameTitle)
gamesQuery.findObjectsInBackground(block: { (objects, error) in
    if error != nil {
       print (error as Any)
       return
    }
    print ("Count : \(objects!.count)")
    if let games = objects{
       for  game in games {
           if let gameName = game["name"] as? String {
               print ("Game name :\(gameName)")
           }
       } 
    }            
})

I put some Print statements in place for debugging (and will be removing them and the count should this ever get to production.

This has to be something simple I am missing. Thanks in Advance


Solution

  • Try it this way:

        let gamesQuery = PFQuery(className: "games")
        print ("Searching for \(gameTitle)")
        gamesQuery.whereKey("name", matchesRegex: gameTitle, modifiers: "i")
        gamesQuery.findObjectsInBackground(block: { (objects, error) in
            if error != nil {
                print (error as Any)
                return
            }
            print ("Count : \(objects!.count)")
            if let games = objects{
                for  game in games {
                    if let gameName = game["name"] as? String {
                        print ("Game name :\(gameName)")
                    }
                }
    
            }
    
        })
    

    With the "i" modifier you ignore the text case, maybe thats the problem.