Realm Flex sync Rules Problem C#

Hello,
I have a problem to search in my database. The rules defined are ReadAllRightOwn by default, when I search field name to verify if the character name is unique, the reading is only in my own data. I would like it to search the entire database.
Can you help me please?

rules :

{
  "roles": [
    {
      "name": "readAllWriteOwn",
      "apply_when": {},
      "document_filters": {
        "write": {
          "owner_id": "%%user.id"
        },
        "read": true
      },
      "read": true,
      "write": true,
      "insert": true,
      "delete": true,
      "search": true
    }
  ]
}

my C# code :

public bool IsCharacterNameUnique(string characterName)
{
    //Character results = realm.All<Character>().Where(t => t.Id == characterName).FirstOrDefault();
    Character results = realm.Find<Character>(characterName);

    Debug.Log(results);
    bool isUnique = results == null;
    results = null;

    return isUnique;
}

Can you clarify what is meant by

Meaning you’re only querying within the Character model where owner_id is YOUR id but you want to query for all Character models regardless of owner_id?

Your C# code is executing the query on the data locally in the database. Can you show us what your subscriptions look like - it’s possible you haven’t requested that the server send you all Character documents.

To clarify , we are making a multiplayer game and we want to keep the character’s names unique.
an example :
It exists 2 accounts with 1 character in each account, they have differents names.
With the first account I want to create a second character, but I don’t want him to have the same name as others characters.
With my code and rules, when I try to give him the same name as the first character in this account , I get the message “this name already exists”.
But if I try to give him the same name as the character in other account, it doesn’t work.

1st account :

  • 1st character name “Character_01”
  • 2nd character name “Character_01” result : “name already exists” :+1:
  • 2nd character name “test_01” result : character created but not saved in database (because the name is the ID of the character) :-1:
  • 2nd character name “test_02” character created and saved :+1:

2nd account :

  • 1st character name “test_01”

I’m not sure to understand your question.
my instantiation :

public static Realms.Sync.App AppInstance { get; } = Realms.Sync.App.Create("App_ID");
public Realms.Sync.User user;
public FlexibleSyncConfiguration config;
public Realm realm;

void Start()
{
	user = AppInstance.CurrentUser;
	config = new FlexibleSyncConfiguration(user);
	realm = Realm.GetInstance(config);
	...
}

in the sign in code :

 user = await AppInstance.LogInAsync(Credentials.EmailPassword(usermail, password));

 //Initial Subscriptions
 config = new FlexibleSyncConfiguration(user)
 {
     PopulateInitialSubscriptions = (realm) =>
     {
         var myItems = realm.All<PlayerData>().Where(n => n.UserId == user.Id);
         realm.Subscriptions.Add(myItems);

         var myChar = realm.All<Character>().Where(n => n.UserId == user.Id);
         realm.Subscriptions.Add(myChar);

         var myInv = realm.All<InventoryItem>().Where(n => n.UserId == user.Id);
         realm.Subscriptions.Add(myInv);
     }
 };

I changed the script in my sign in code :

user = await AppInstance.LogInAsync(Credentials.EmailPassword(usermail, password));

 //Initial Subscriptions
 config = new FlexibleSyncConfiguration(user)
 {
     PopulateInitialSubscriptions = (realm) =>
     {
         var myItems = realm.All<PlayerData>().Where(n => n.UserId == user.Id);
         realm.Subscriptions.Add(myItems);

         var myChar = realm.All<Character>().Where(n => n.UserId == user.Id);
         realm.Subscriptions.Add(myChar);

         var myInv = realm.All<InventoryItem>().Where(n => n.UserId == user.Id);
         realm.Subscriptions.Add(myInv);
     }
 };

To

user = await AppInstance.LogInAsync(Credentials.EmailPassword(usermail, password));

 //Initial Subscriptions
 config = new FlexibleSyncConfiguration(user)
 {
     PopulateInitialSubscriptions = (realm) =>
     {
         var myItems = realm.All<PlayerData>().Where(n => n.UserId == user.Id);
         realm.Subscriptions.Add(myItems);

         var myChar = realm.All<Character>();
         realm.Subscriptions.Add(myChar);

         var myInv = realm.All<InventoryItem>().Where(n => n.UserId == user.Id);
         realm.Subscriptions.Add(myInv);
     }
 };

Now it works I can search in all characters

Thank you for your help, @nirinchev your answer help me a lot !

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.