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

custom setter function is not called #1803

Closed
XilinJia opened this issue Jul 10, 2024 · 2 comments
Closed

custom setter function is not called #1803

XilinJia opened this issue Jul 10, 2024 · 2 comments

Comments

@XilinJia
Copy link

XilinJia commented Jul 10, 2024

How frequently does the bug occur?

Always

Description

class Human: RealmObject {
    var name: String = ""
        set(value) {
           println("Example Setter called with value: $value")
           field = value
        }
}

When I set the name with: person.name = "John", the setter function is not called.

I googled hard to understand about the issue, it appears this 7 years old post explained it, but obviously is regarding the Java API, seems still valid with the current Kotlin API? https://stackoverflow.com/questions/43066612/realm-call-setter

Is it possible to get the setters to work in Kotlin as that's a neat feature of the language?

Stacktrace & log output

No response

Can you reproduce the bug?

Always

Reproduction Steps

No response

Version

2.0.0

What Atlas App Services are you using?

Local Database only

Are you using encryption?

No

Platform OS and version(s)

Android 9, Android 14

Build environment

Android Studio version: ...
Android Build Tools version: ...
Gradle version: ...

Copy link

sync-by-unito bot commented Jul 10, 2024

➤ PM Bot commented:

Jira ticket: RKOTLIN-1109

@nirinchev
Copy link
Member

If you need to execute custom logic in the setter/getter of a property, you should @Ignore it so that it's not treated as persisted. So essentially, it should look something like this:

class Human: RealmObject {
    @Ignore
    public var name: String
        set(value) {
           println("Example Setter called with value: $value")
           name_persisted = value
        }
        get() {
            return name_persisted
        }

    private var name_persisted: String = ""

@nirinchev nirinchev closed this as not planned Won't fix, can't repro, duplicate, stale Jul 22, 2024
@sync-by-unito sync-by-unito bot closed this as completed 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

2 participants