15 Ağustos 2017

String Search in NoSQL Firebase Database

Today I’m going to show how to obtain data from the Firebase database by substring search in Objective-c programming language.

Firebase database is a NoSql database as you know. So it supplies high performance for key-value data. But you may need “LIKE” query sometimes. Firebase database doesn't have  any query such as “LIKE” yet but it provides two other queries for substring searches from the beginning and the end of the string.

We will use these 2 queries:

Let’s assume we have a database like :
// -root
//  -users
//   -user1
//     name
//     age
//     ...
//   -user2
//     name
//     age
//     ...
// ....

// Let's try to get users whos name starts with 'ja'
NSString *searchString = @"ja";
NSString *endingString = [NSString stringWithFormat:@"%@\uf8ff", searchString];

// Create the query
FIRDatabaseQuery *query = [[[[[[FIRDatabase database] reference] child:@"users"]

// Run the query
[query observeSingleEventOfType:FIRDataEventTypeValue withBlock:^(FIRDataSnapshot * _Nonnull snapshot) {
    if(snapshot.hasChildren) {
        for (FIRDataSnapshot *child in snapshot.children) {
            NSLog(@"User Id: %@", child.key);
            NSLog(@"User : %@", child.value);

NOTE: We need to add  "\uf8ff" character into search string for the queryEndingAtValue. 

"The character \uf8ff used in the query is a very high code point in the Unicode range (it is a Private Usage Area [PUA] code). Because it is after most regular characters in Unicode, the query matches all values that start with the queryText." https://stackoverflow.com/a/40633692/2186887

Have a nice day.

Bu Blogda Ara



E-posta *

Mesaj *