using swift and firebase I have a list of data like so
{
"posts" : {
"-KHbULJcKqt9bSpxNO9k" : {
"author" : "Michele",
"postText" : "tags as arrays?",
"tag" : [ "HELLO", "WHAT'S UP?", "TEST" ]
}
with many more post. I want to search by tag and if a post contains the search term as a tag return the whole post. Ive been able to do this with strings using queryEqualToValue:
but haven't been able to solve it for an array. Any point in the right direction is appreciated thank you.
This is what my save function looks like
func createNewPost(post: Dictionary<String, AnyObject>, tags: [String]) {
let firebaseNewPost = POST_REF.childByAutoId()
let firebaseTag = Firebase(url: "\(POST_REF)/\(firebaseNewPost.key)/tag")
print(firebaseTag)
firebaseNewPost.setValue(post)
for tag in tags {
let tagID = firebaseTag.childByAutoId()
tagID.setValue(tag)
}
}
Now I need help with searching through the data and retrieving certain post based on what a user searches for. This is what I was using when it was just one tag and it was a just a string.
func loadTaggedShit(searchTerm: String) {
DataService.dataService.POST_REF.queryOrderedByChild("tag").queryEqualToValue(seachTerm).observeEventType(.ChildAdded, withBlock: { snapshot in
self.posts = []
if let snapshots = snapshot.children.allObjects as? [FDataSnapshot] {
for snap in snapshots {
if let postDictionary = snap.value as? Dictionary<String, AnyObject> {
let key = snap.key
let post = Post(key: key, dictionary: postDictionary)
self.posts.insert(post, atIndex: 0)
}
}
}
self.tableView.reloadData()
})
}
also the data now looks like this
"posts":{
"-KHj_bDJmZJut7knKoUX" : {
"author" : "Michele",
"postText" : "what's up",
"tag" : {
"-KHj_bDJmZJut7knKoUY" : "HEY"
}
}
}