I have firebase data as below:
chats
|
|
+ - - -simplelogin48simplelogin50
|
|
|
+ - - simplelogin50simplelogin48
|
|
|
+ - - -simplelogin48simplelogin50
Now I want to fetch chat data which contains "simplelogin 48". Is it is possible?
No, Firebase does not support a contains operator in its queries. Firebase provides queryStartingAtValue
and queryEndingAtValue
operators for selecting a range of keys, but they are both based on the start of the key (or property value or priority if you've ordered on those). See this section of the Firebase iOS documentation that explains querying.
So by:
[[[[ref queryOrderedByKey] queryStartingAtValue:@"simplelogin48"] queryEndingAtValue:@"simplelogin48"]
You would get:
+ - - -simplelogin48simplelogin50
+ - - -simplelogin48simplelogin50
But not:
+ - - simplelogin50simplelogin48
The way to work around this is by creating a so-called index node, which in this case simply lists the chat ids for each user:
chats_by_user
|
+ - - simplelogin48
| |
| + - - simplelogin48simplelogin50
| |
| + - - simplelogin48simplelogin49
| |
| + - - simplelogin50simplelogin48
|
+ - - simplelogin49
| |
| + - - simplelogin48simplelogin49
|
+ - - simplelogin50
|
+ - - simplelogin50simplelogin48
|
+ - - simplelogin48simplelogin50
With this structure you can simply access chats_by_user/simplylogin48
to get all references to all chats for that user.