react-native领域:未调试时无法获得结果

时间:2018-06-23 15:16:39

标签: android react-native realm realm-js

未调试时,我无法获得任何查询的结果,但是在调试

时,效果很好

我还注意到,根据是否进行了调试,Realm的行为有很大不同

这只是这个想法的摘要:

我正在打印object.constructor.name以找到对象的类型

调试时(远程使用Chrome或Safari):

let realm = new Realm(config);
realm.constructor.name --> will print (Realm)

let dogs = realm2.objects('Dog');
dogs.constructor.name --> will print (Results)

(inserting few dogs)

for (let oneDog of dogs) {
    oneDog.constructor.name --> will print (RealmObject)
} --> works perfectly

但是在不进行调试时,一切都不同:

let realm = new Realm(config);
realm.constructor.name --> will print (Object)

let dogs = realm2.objects('Dog');
dogs.constructor.name --> will print nothing

(inserting few dogs)

for (let oneDog of dogs) {
    oneDog.constructor.name --> not compiled
} --> will give the error below

enter image description here

TypeError: undefined is not a function (evaluating ‘_iterator[typeof
Symbol === “function” ? Symbol.iterator : “@@iterator”]()')

我不确定这是错误还是我的代码有问题


领域和工具版本

  • Realm JS SDK版本:2.10.0
  • 本机反应:0.55.4
  • 客户端操作系统和版本:在Android设备8.0上运行
  • React Native的哪个调试器:Chrome

完整代码:

import React, { Component } from 'react';
import { View, Text } from 'react-native';

const Realm = require('realm');

export default class Page3Screen extends Component {
  state = { messege : 'no messege' }

  componentWillMount() {
    const config = {
      schema: [{ name: 'Dog', properties: { name: 'string' } }],
    };
    let realm = new Realm(config);
    console.log(realm.constructor.name);
    this.setState({ messege: realm.constructor.name });

    realm.write(() => {
      realm.create('Dog', { name: 'Zozo' });
    });

    let dogs = realm.objects('Dog');
    // console.log(dogs.constructor.name);
    // this.setState({ messege: dogs.constructor.name });

    // for (let oneDog of dogs) {
    //    console.log(oneDog.constructor.name);
    //    this.setState({ messege: oneDog.constructor.name });
    // }
  }

  render() {
    return (
      <View style={{ alignSelf: 'stretch', flex: 1, alignItems: 'center', justifyContent: 'center' }}>
        <Text>{this.state.messege}</Text>
      </View>
    );
  }
}

2 个答案:

答案 0 :(得分:1)

我发现问题与this issue

有关
  

无论出于何种原因,android上的react-native都会将旧的东西当作屎   版本的JSC,不支持语言功能   当前的React版本需要工作

updating the JSC version that ships with android解决了该问题,但我使用的是JSC版本 216113 ,而不是最新版本,以保留 minSdkVersion 17 而不是 21 < / strong>

以下是说明:

Follow steps below in order for your React Native app to use new version of JSC VM on android:

1. Add `jsc-android` to the "dependencies" section in your `package.json`:
```
dependencies {
+  "jsc-android": "^216113.0.0",
```

then run `npm install` or `yarn` (depending which npm client you use) in order for the new dependency to be installed in `node_modules`

2. Modify `andorid/build.gradle` file to add new local maven repository packaged in the `jsc-android` package to the search path:
```
allprojects {
    repositories {
        mavenLocal()
        jcenter()
        maven {
            // All of React Native (JS, Obj-C sources, Android binaries) is installed from npm
            url "$rootDir/../node_modules/react-native/android"
        }
+       maven {
+           // Local Maven repo containing AARs with JSC library built for Android
+           url "$rootDir/../node_modules/jsc-android/android"
+       }
    }
}
```

3. Update your app's `build.gradle` file located in `android/app/build.gradle` to force app builds to use new version of the JSC library as opposed to the version specified in [react-native gradle module as a dependency](https://github.com/facebook/react-native/blob/e8df8d9fd579ff14224cacdb816f9ff07eef978d/ReactAndroid/build.gradle#L289):

```
}

+configurations.all {
+    resolutionStrategy {
+        force 'org.webkit:android-jsc:r216113'
+    }
+}

dependencies {
    compile fileTree(dir: "libs", include: ["*.jar"])
```

4. You're done, rebuild your app and enjoy updated version of JSC on android!

答案 1 :(得分:0)

debugging模式下,react-native在浏览器而不是设备中运行js代码。 由于您说它可以在浏览器上正常运行,但在设备上会损坏,因此建议您检查以下内容:

  • 检查设备是否连接到同一网络
  • 运行此命令:adb reverse tcp:8081 tcp:8081
  • 检查开发服务器是否正在运行,可以使用以下命令运行它:$ react-native start
  • 一些JS功能需要在React-native中使用polyfill,例如Symbol。因此,请将以下内容添加到index.js

    import 'core-js/es6/symbol';
    import 'core-js/fn/symbol/iterator';
    
  • 在非常罕见的情况下,浏览器上的JS引擎与android / ios设备之间存在差异

祝你好运。