代码之家  ›  专栏  ›  技术社区  ›  Mujtaba Fadhil

realm on react native:未调试时无法获得结果

  •  1
  • Mujtaba Fadhil  · 技术社区  · 6 年前

    不调试 ,但当 调试

    另外,我注意到域的行为与wither调试的行为非常不同。

    这只是一个想法的总结:

    我在打印 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 回复  |  直到 6 年前
        1
  •  1
  •   Mujtaba Fadhil    6 年前

    我发现这个问题与 this issue

    不管出于什么原因,安卓上的React原生版都在发布一个旧版本 jsc的一个版本,它不支持语言特性 当前的react版本需要工作

    解决方法是 updating the JSC version that ships with android ,但我使用了jsc版本 216113个 而不是最新的 明斯克版本17 而不是 21岁

    以下是说明:

    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!
    
        2
  •  0
  •   Soorena    6 年前

    当你在里面的时候 debugging 模式下,react native在浏览器中而不是设备中运行js代码。 既然你说它在浏览器上运行良好,但在设备上出现故障,我建议你检查以下内容:

    • 检查设备是否连接到同一网络
    • 运行此: adb reverse tcp:8081 tcp:8081
    • 检查dev server是否正在运行,您可以使用以下命令运行它: $ react-native start
    • 一些js特性需要在react native中使用polyfill,例如 Symbol 是的。因此,将以下内容添加到 index.js 以下内容:

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

    祝你好运。