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

Doesn't Map value type work at all? #8644

Closed
gerchicov-vg opened this issue Jul 5, 2024 · 4 comments
Closed

Doesn't Map value type work at all? #8644

gerchicov-vg opened this issue Jul 5, 2024 · 4 comments

Comments

@gerchicov-vg
Copy link

How frequently does the bug occur?

Always

Description

Code example

@objcMembers
class Obj1: Object {
    dynamic var obj2: Obj2?
}

@objcMembers
class Obj2: Object {
    dynamic var obj3: Obj3?
}

@objcMembers
class Obj3: Object {
    dynamic var val: String?
    dynamic var valMap: Map<String, String>?
}

try! realm.write {
            let obj1 = Obj1()
            obj1.obj2 = .init()
            obj1.obj2?.obj3 = .init()
            obj1.obj2?.obj3?.val = "test"
            obj1.obj2?.obj3?.valMap = .init()
            obj1.obj2?.obj3?.valMap?["testKey"] = "testValue"
            realm.add(obj1)
        }

try! RealmManager.shared.realm.write {
            let requestedObj1 = realm.objects(Obj1.self).first!
            ...
}

I check every value inside requestedObj1 and see each data I've stored previously except of Map object and its values - it just becomes nil after assignment.

Possibly there are similar bugs exist but I can't found it - documentation doesn't show a concrete example of how to use Map and in the same time Map is similar to Swift's map for collections and sequences which make searching almost impossible.

In general are there other ways to store a dictionary in Realm?

Stacktrace & log output

No response

Can you reproduce the bug?

Always

Reproduction Steps

No response

Version

10.46.0

What Atlas Services are you using?

Local Database only

Are you using encryption?

No

Platform OS and version(s)

iOS 15.5 (simulator)

Build environment

Xcode version: 15.0
Dependency manager and version: cocoapods 1.15.2

Copy link

sync-by-unito bot commented Jul 5, 2024

➤ PM Bot commented:

Jira ticket: RCOCOA-2404

@tgoyne
Copy link
Member

tgoyne commented Jul 5, 2024

Map? is not a supported property type, as collections must be non-optional. When using @objc dynamic, unsupported properties are implicitly ignored. With @Persisted, this would be a compilation error instead.

@gerchicov-vg
Copy link
Author

gerchicov-vg commented Jul 8, 2024

@tgoyne when I use @Persisted var it crashes on start saying that I should declare this map optional. When it is optional a compiler doesn't like it:
Generic struct 'Persisted' requires that 'Map<String, ...>' conform to '_PersistableInsideOptional'
And I don't know how to deal with this error.

Currently I refactored my objects to use List+identifier fields instead of single Map field - it is not so convenient but at least it works.

@nirinchev
Copy link
Member

You can see an example of how map properties are defined in our tests:

@Persisted var boolMap: Map<String, Bool>
@Persisted var intMap: Map<String, Int>
@Persisted var int8Map: Map<String, Int8>
@Persisted var int16Map: Map<String, Int16>
@Persisted var int32Map: Map<String, Int32>
@Persisted var int64Map: Map<String, Int64>
@Persisted var floatMap: Map<String, Float>
@Persisted var doubleMap: Map<String, Double>
@Persisted var stringMap: Map<String, String>
@Persisted var dataMap: Map<String, Data>
@Persisted var dateMap: Map<String, Date>
@Persisted var decimalMap: Map<String, Decimal128>
@Persisted var objectIdMap: Map<String, ObjectId>
@Persisted var uuidMap: Map<String, UUID>
@Persisted var boolOptMap: Map<String, Bool?>
@Persisted var intOptMap: Map<String, Int?>
@Persisted var int8OptMap: Map<String, Int8?>
@Persisted var int16OptMap: Map<String, Int16?>
@Persisted var int32OptMap: Map<String, Int32?>
@Persisted var int64OptMap: Map<String, Int64?>
@Persisted var floatOptMap: Map<String, Float?>
@Persisted var doubleOptMap: Map<String, Double?>
@Persisted var stringOptMap: Map<String, String?>
@Persisted var dataOptMap: Map<String, Data?>
@Persisted var dateOptMap: Map<String, Date?>
@Persisted var decimalOptMap: Map<String, Decimal128?>
@Persisted var objectIdOptMap: Map<String, ObjectId?>
@Persisted var uuidOptMap: Map<String, UUID?>
@Persisted var objectOptMap: Map<String, CodableTopLevelObject?>
@Persisted var embeddedObjectOptMap: Map<String, CodableEmbeddedObject?>
.

@nirinchev nirinchev closed this as not planned Won't fix, can't repro, duplicate, stale Jul 22, 2024
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Aug 21, 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

3 participants