question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

provide example of using classes for realm objects with custom methods

See original GitHub issue

Goals

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:closed
  • Created 6 years ago
  • Reactions:2
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

6reactions
Talor-Acommented, Aug 31, 2017

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

import realm from './realm'
export class Task {
  static schema = {
    name: 'Task',
    primaryKey: 'id',
    properties: {
      id: { type: 'string', indexed: true },
      title: { type: 'string' }
    }
  }

  static getTasks () {
    return realm.objects('Task')
  }
  static newTask (title) {
    realm.write(() =>
      realm.create('Task', { title: 'test', id: Math.random().toString() })
    )
  }
  setTitle (title) {
    realm.write(() => {
      this.title = title
    })
  }
}

file: realm.js

import { Task } from './task'
const realm = new Realm({
  schema: [
    Task
  ],
})

export default realm

file: TasksView.js


export default class Tasks extends Component {
  constructor (props) {
    super(props)
    this.state = { tasks: Task.getTasks() }
    this.create = this.create.bind(this)
  }
  componentDidMount () {
    const tasks = Task.getTasks()
    tasks.addListener(() => this.setState({ tasks: Task.getTasks() }))
  }
  create () {
    Task.newTask('test')
  }
  render () {
    return (
      <ScrollView>
        {Array.from(this.state.tasks).map(task => (
          <Button
            onPress={() => task.setTitle('changed title!')}
            title={task.title}
            key={task.id}
          />
        ))}
        <Button onPress={this.create} title={'new'} />
      </ScrollView>
    )
  }
}

0reactions
Talor-Acommented, Aug 31, 2017

@fealebenpae figured it out. I had export default new Realm({schema:[Task]}) in one file, and Task class 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… 🤔

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found