Ebay89
Ebay89

Reputation: 690

Cannot Modify Managed Objects outside of write transaction error in Realm + React Native

I am trying to write a class to my database in Realm and React Native and I am sure what is going wrong. My Realm Code is Below. When I call the createTopic method on the Topic Manager class I get an error message "Cannot Modify Managed Objects outside of a write transaction." in red on my screen. The method to write to the database was working before.... so I must be missing something simple. Thanks for taking a look!

import React, { Component } from 'react';

const Realm = require('realm');

class Topic {}
Topic.schema = {
    name: 'Topic',
    primaryKey: 'key',
    properties: {
        name: 'string',
        key: 'string'
    },
};

const topicManager = new Realm({schema: [Topic]});


export class TopicManager {

constructor(props) {

  }

    createTopic(insertName) {
         const uuid = this.uuidv4();
         savedTopic = topicManager.create('Topic', {
             key: uuid,
             name: insertName,
         });
    }

    uuidv4() {
      return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
        var r = Math.random() * 16 | 0, v = c == 'x' ? r : (r & 0x3 | 0x8);
        return v.toString(16);
      });
    }

}

Upvotes: 1

Views: 4398

Answers (1)

EpicPandaForce
EpicPandaForce

Reputation: 81539

Probably

createTopic(insertName) {
    topicManager.write(() => { // <--
      savedTopic = topicManager.create('Topic', {
             key: uuid,
             name: insertName,
         });
    }); // <--
}

Upvotes: 2

Related Questions