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

Use type-safe keyPath modify object #8521

Open
Jaycyn opened this issue Mar 17, 2024 · 1 comment
Open

Use type-safe keyPath modify object #8521

Jaycyn opened this issue Mar 17, 2024 · 1 comment

Comments

@Jaycyn
Copy link

Jaycyn commented Mar 17, 2024

How frequently does the bug occur?

Always

Description

Since type-safe keyPath syntax was added to Realm, I would expect that syntax to work everywhere, not just in queries. It does not appear to work with observe or updating/creating an object

A query pre 10.11.0. would be

let dogsSorted = dogs.sorted(byKeyPath: "name")

and the from 10.11.0. forward we can use

let dogsSorted = dogs.sorted(by: \.name)

That same syntax should work when updating objects (and everywhere else) as well.

Stacktrace & log output

No response

Can you reproduce the bug?

Always

Reproduction Steps

Supposed we have a Person object with the required primary key and a name string

class Person: Object {
    @Persisted(primaryKey: true) var _id: ObjectId
    @Persisted var name = ""
}

create an object and write it to Realm

let t0 = Person()
t0.name = "Jay"
try! realm.write {
   realm.add(t0)
}

then later, read that object in to update the name via the \.name keypath

let jay = realm.objects(Person.self).where { $0.name == "Jay" }.first!
print(jay)

outputs

Person {
	_id = 65f70df1a24efa5b3435f85b;
	name = Jay;
}

Now modify the name using type-safe keyPath

let modifiedJay = Person(value: [\Person._id: jay._id, \Person.name: "Modified Jay"])
print(modifiedJay)

and the output

Person {
	_id = 65f70df3a24efa5b3435f85e;
	name = ;
}

Note that neither the _id nor the name was property populated. However, using Strings as keys does work

let jay = realm.objects(Person.self).where { $0.name == "Jay" }.first!
print(jay)

let modifiedJay = Person(value: ["_id": jay._id, "name": "Modified Jay"])
print(modifiedJay)

outputs

Person {
	_id = 65f70df1a24efa5b3435f85b;
	name = Jay;
}
Person {
	_id = 65f70df1a24efa5b3435f85b;
	name = Modified Jay;
}

Note the primary key is the same and the name was updated.

Version

10.48.1

What Atlas Services are you using?

Local Database only

Are you using encryption?

No

Platform OS and version(s)

macOS Sonoma 14.2.1

Build environment

Xcode version: 15.3
Dependency manager and version: Realm 10.48.1

Copy link

sync-by-unito bot commented Mar 17, 2024

➤ PM Bot commented:

Jira ticket: RCOCOA-2311

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant