Ios Query filtering not working

Can anyone let me know how to get all the users from my Realm.
Basically i want to search for other users

guard let realm = Realm.syncOpen() else {
return completion(List())
}

    let predicate = NSPredicate(format: "username == Jas");
    let results = realm.objects(User.self).filter(predicate)

    completion(results.toList())

this is the function used, but im not getting any result back.
Is their any issue with realm atlas or do i need to do app side change.

What does syncOpen look like?

I would also recommend you edit the query to be "username == 'Jas'".

What does your user model look like?

1 Like

Should the parition id be same for all the users in table ?
I tested it, if the partition id value is same i am getting the list of users which is what i want.
If the partition id value is different i am not receveing any data.

You’ll need to balance what you want from queries with security of your users. That means if you want to be able to search for something within a grouping of data, that data needs to have the same partition key.

However, you won’t want to expose all user data to all other users so one option is to somewhat denormalize the data and have private user data, where each users data has the users uid as it’s partition key and then data you want to expose (search) (which is public or duplicated data ) where they all share the same key.

In essence you’d have a User object with private user data

User: Object {
     partitionKey is this users uid  
     phone_number
     address
     other private data
}

then you would have a public user object where all of the objects share the same partition key

Public_User: Object {
    partitionKey:  public_users
    user_id is this users uid
    name
    favorite food
    other publicly accessable data
}

That way a user has their own data that only they can access, but then when you need to query across users for say, all users that love pizza, the query is run against the public_users partition key (Realm).

1 Like