I have database structure look like this:
posts:
pid1:
timestamp: 111
pod:
title: "eggs"
pid2:
timestamp: 222
pod:
title: "milk"
pid3:
timestamp: 333
pod:
title: "bread"
users_posts:
uid1:
pid1: true
pid2: true
I want to get all the uid1
's posts where title
contains a specific substring.
For example:
input: substring: "gs", userId: uid1
output: pid1
What is the best way to do that?
This is what I do so far:
func queryPosts(withSearchText searchText: String, userId: String, completion: @escaping (Post) -> Void) {
REF_USER_POSTS.child(userId).observeSingleEvent(of: .value) { (snapshot) in
let ids = (snapshot.value as! [String: Any]).keys
for id in ids {
REF_POSTS.child(id).observeSingleEvent(of: .value) { (snapshot) in
if let dict = snapshot.value as? [String: Any],
let title = ((dict["pod"] as? [String: Any])?["title"] as? String)?,
title.contains(searchText) {
print("found result", id)
}
}
}
}
}