我正在尝试在Realm和React Native中为我的数据库编写一个类,我确信出了什么问题。我的境界代码在下面。当我在Topic Manager类上调用createTopic方法时,我收到一条错误消息“无法修改写入事务之外的托管对象”。在我的屏幕上红色。写入数据库的方法之前正在进行....所以我必须遗漏一些简单的东西。谢谢你看看!
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);
});
}
}
答案 0 :(得分:1)
可能
createTopic(insertName) {
topicManager.write(() => { // <--
savedTopic = topicManager.create('Topic', {
key: uuid,
name: insertName,
});
}); // <--
}