2

不调试时我无法获得任何查询的结果,但在调试时可以完美运行

此外,我注意到 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

在此处输入图像描述

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>
    );
  }
}
4

2 回答 2

1

I found that the problem is related to this issue

For whatever reason react-native on android is shipping an old as shit version of JSC, one that doesn't have support for language features that current react version needs to work

it was solved by updating the JSC version that ships with android, but I've used JSC version 216113 instead of the latest to keep minSdkVersion 17 instead of 21

Here are the instructions:

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!
于 2018-06-25T11:53:02.417 回答
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';
    
  • 在非常罕见的情况下,浏览器和 android/ios 设备上的 JS 引擎之间存在差异

祝你好运。

于 2018-06-23T16:50:51.883 回答