简体   繁体   English

Realm 在创建对象时崩溃(React Native)

[英]Realm crashes on create object (React Native)

This is my RealmService file:这是我的 RealmService 文件:

import Realm from 'realm';

const AuthSchema = {
    name: 'Auth',
    primaryKey: 'id',
    properties: {
        id: {
            type: 'int',
            indexed: true
        },
        time: 'date',
        username: 'string',
        action: 'string'
    }
}

const WiretransferSchema = {
    name: 'Wiretransfer',
    primaryKey: 'id',
    properties: {
        id: {
            type: 'int',
            indexed: true
        },
        time: 'date',
        source: 'string',
        target: 'string',
        amount: 'float',
        comments: {
            type: 'string',
            optional: true
        }
    }
}

let RealmService = {
    findAllAuth: function() {
        return repository.objects('Auth');
    },

    SaveAuth: function(username, action) {
        Realm.open({
            path: 'testtt.realm',
            schema: [AuthSchema, WiretransferSchema]
        }).then(realm => {

            // Get max ID
            var maxId = realm.objects('Auth').max('id');

            realm.write(() => {
                realm.create('Auth', {
                    id: maxId + 1,
                    time: Date.now(),
                    username: username,
                    action: action
                }, true);
            });

            realm.close();  
        }).catch(err => {
            console.log(err);
        });
    }
}

module.exports = RealmService;

The app crashes without any errors when this code is called:调用此代码时,应用程序崩溃且没有任何错误:

realm.write(() => {
                realm.create('Auth', {
                    id: maxId + 1,
                    time: Date.now(),
                    username: username,
                    action: action
                }, true);
            });

Just the write method doesn't crash the app.只是 write 方法不会使应用程序崩溃。 It's the create method.这是创建方法。

If I disable the debugging tools in React Native the app doesn't crash but there is nothing in the realm file added.如果我在 React Native 中禁用调试工具,应用程序不会崩溃,但在领域文件中没有添加任何内容。

I've tried RN 0.57.1 and 0.57.8.我试过 RN 0.57.1​​ 和 0.57.8。 Reinstalled realm and still no luck.重新安装了领域,但仍然没有运气。 What could the problem be?可能是什么问题?

Make sure datatypes and objects you are passing are same.确保您传递的数据类型和对象相同。 I got same crash when my datatype was int and I gave it string .当我的数据类型是int并且我给它string时,我也遇到了同样的崩溃。

In your case passing new Date() will work.在您的情况下,传递new Date()将起作用。

It won't give any errors but app will crash on both android and iOS.它不会给出任何错误,但应用程序会在 android 和 iOS 上崩溃。 Maybe realm team will solve it in future release as this bug already exists in Github forum.也许领域团队会在未来的版本中解决它,因为这个错误已经存在于 Github 论坛中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM