provide example of using classes for realm objects with custom methods
See original GitHub issueGoals
I have a class defined like so:
export class Task {
static schema = {
name: 'Task',
primaryKey: 'id',
properties: {
id: { type: 'string', indexed: true },
title: { type: 'string' }
}
}
constructor (title) {
this.title = title || ''
this.id = Utils.guid()
}
static getTasks = () => {
return realm.objects('Task')
}
setTitle = title => {
realm.write(() => {
this.title = title
})
}
}
...
export default new Realm({
schema: [Task],
})
I can do Task.getTasks(), but if I call task.setTitle('Changed Title!') on one of the Task objects from realm, I get an error that setTitle is not defined. this way of defining classes is mentioned in #141, so I would assume it would be possible still.
I’ve tried a few different ways to get this working, including defining the Task schema separately from the Task class, and extending Realm.Object, but can’t find a method that works. A documented solution would be ideal for this.
Issue Analytics
- State:
- Created 6 years ago
- Reactions:2
- Comments:6 (2 by maintainers)
Top Results From Across the Web
Define a Realm Object Schema - Java SDK - MongoDB
The following code block shows a Realm object that describes a Frog. This Frog class can be stored in Realm Database because it...
Read more >Custom methods in RealmObjects... solution? - Stack Overflow
I am using Realm as a data module in my android application, though I am running into some problems regarding how to assign...
Read more >Creating a Custom Realm (Sun GlassFish Enterprise Server ...
You can create a custom realm by providing a custom Java Authentication and Authorization Service (JAAS) login module class and a custom realm...
Read more >Storing and Retrieving Objects - Beginning Realm on iOS
Try creating new objects and persisting them automatically on disk. Learn how to fetch data back once saved on disk.
Read more >Server Developer Guide - Keycloak
The following example shows how to use the Java client library to get the details of the master realm: import org.keycloak.admin.client.Keycloak ...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found

okay final update for some reason it’s working in different files, perhaps that wasn’t the problem. but here’s the code I needed. perhaps an example like this could be added to the docs? file: Task.js
file: realm.js
file: TasksView.js
@fealebenpae figured it out. I had
export default new Realm({schema:[Task]})in one file, andTaskclass declaration in another. in the Task file I was importing the realm instance from the first file, and I think something about the two files requiring each other was causing the error. unfortunately I now don’t know how I can split these statements up, but that’s a separate problem I guess… 🤔