Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

XXX is an IDictionary but its generic type is String[] which is not supported by Realm #3640

Closed
ThinhVu opened this issue Jul 9, 2024 · 2 comments

Comments

@ThinhVu
Copy link

ThinhVu commented Jul 9, 2024

Problem

XXX is an IDictionary but its generic type is String[] which is not supported by Realm

Example data:

{
   data: {
     "192f56c6-651f-4b40-bc0c-f794c4879a45":  [ 
         "192f56c6-651f-4b40-bc0c-f794c4879a45", 
         "192f56c6-651f-4b40-bc0c-f794c4879a45" 
      ], 
     "192f56c6-651f-4b40-bc0c-f794c4879a45":  [ 
         "192f56c6-651f-4b40-bc0c-f794c4879a45", 
         "192f56c6-651f-4b40-bc0c-f794c4879a45" 
      ], 
   }
}

which have type of Dictionary<string, string[]> and it not support by Realm.

Does Realm currently have any way to work with the data schema like so?

Solution

No response

Alternatives

No response

How important is this improvement for you?

Would be a major improvement

Feature would mainly be used with

Local Database only

Copy link

sync-by-unito bot commented Jul 9, 2024

➤ PM Bot commented:

Jira ticket: RNET-1164

@nirinchev
Copy link
Member

You can use IDictionary<string, RealmValue> - RealmValue can be any supported type, including a list. However, it doesn't provide type safety in the sense, that both Realm and the type system will happily accept non-string[] types here. If you want to enforce this type, then that is not supported out of the box. You can create a hacky wrapper on top of IDictionary<string, RealmValue>, but that will work on the application rather than the database layer. Here's a rough sketch of the idea:

public partial class DataContainer : IRealmObject
{
    private Lazy<RealmDictionaryWrapper> _dataWrapper = new(() => new(StorageDict));
    
    // Real backing storage dictionary is private so its inaccessible outside of the class
    private IDictionary<string, RealmValue> StorageDict { get; }

    // Return the wrapper via the public API
    public RealmDictionaryWrapper Data => _dataWrapper.Value;
}

public class RealmDictionaryWrapper : IDictionary<string, string[]>
{
    private readonly IDictionary<string, RealmValue> _backingStorage;

    public RealmDictionaryWrapper(IDictionary<string, RealmValue> storage)
    {
        _backingStorage = storage;
    }

    public int Count => _backingStorage.Count;

    public void Add(string key, string[] value)
    {
        _backingStorage.Add(key, value.Select(a => (RealmValue)a).ToArray());
    }

    public bool ContainsKey(string key) => _backingStorage.ContainsKey(key);

    public bool Remove(string key) => _backingStorage.Remove(key);

    public bool TryGetValue(string key, out string[] value)
    {
        if (_backingStorage.TryGetValue(key, out var backingValue))
        {
            // This doesn't do any error handling - in case the backingValue is not a list or
            // its elements are not strings, this will throw.
            value = backingValue.AsList().Select(v => v.AsString()).ToArray();
            return true;
        }

        value = default;
        return false;
    }

    // ... remaining IDictionary members
}

@nirinchev nirinchev closed this as not planned Won't fix, can't repro, duplicate, stale Jul 9, 2024
@nirinchev nirinchev self-assigned this Jul 9, 2024
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Aug 8, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

2 participants