realm / realm-swift

Realm is a mobile database: a replacement for Core Data & SQLite
https://realm.io
Apache License 2.0
16.34k stars 2.15k forks source link

Map Does Not, In Fact, Support Strings As Keys #8290

Open bdkjones opened 1 year ago

bdkjones commented 1 year ago

How frequently does the bug occur?

Always

Description

I have an Object with this property:

@Persisted var foo: Map<String, Double>

The keys of this object were intended to be bundle identifiers (e.g. com.apple.Safari). I ported the entire damn app to Realm only to discover that Realm has an undocumented, arbitrary limitation on keys: they can't contain periods.

No such limitation exists on Swift dictionaries. The documentation (which I read thoroughly to make sure Realm would work for all my model features PRIOR to porting this app) at https://www.mongodb.com/docs/realm-sdks/swift/latest/Classes/Map.html incorrectly states that "only string keys are supported". In fact, only a subset of strings are supported as keys, which is a pretty important detail to omit.

The ideal remedy is to remove the arbitrary limitation on keys. Barring that, the documentation should be updated to properly outline Realm's limitations so that developers don't waste their time adopting Realm when it won't work.

Stacktrace & log output

Dictionary::insert: key must not contain '.'
(
    0   CoreFoundation                      0x000000019fed3154 __exceptionPreprocess + 176
    1   libobjc.A.dylib                     0x000000019f9f24d4 objc_exception_throw + 60
    2   MyApp                               0x00000001044e5cd8 _ZN18RLMAccessorContext12createObjectEP11objc_objectN5realm12CreatePolicyEbNS2_6ObjKeyE + 2560
    3   MyApp                               0x00000001045e0198 RLMAddObjectToRealm + 252


### Can you reproduce the bug?

Always

### Reproduction Steps

Attempt to use a string containing a period as a key in `Map`.

### Version

10.40.2

### What Atlas Services are you using?

Local Database only

### Are you using encryption?

No

### Platform OS and version(s)

macOS 13.4.1

### Build environment

Xcode version: 15.0 beta 2
Dependency manager and version: N/A
dianaafanador3 commented 1 year ago

Hi @bdkjones thanks for raising this issue in our documentation, asking the core team why do we have this constraint, and also I'll request the document team to update this as well.

aehlke commented 1 year ago

I recommend using percent encoded escaping meanwhile

dianaafanador3 commented 1 year ago

Documentation and dosstrings are updated, leaving this issue open so we can work to support this in the future, if the restrictions on the server change or we find a workaround.