react-native 上的领域:不调试时无法获得结果

Realm on react-native : can't get results when not debugging

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

时,我可以完美地工作

我还注意到 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”]()')

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


领域和工具的版本


完整代码:

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

当您处于 debugging 模式时,在您的浏览器而不是设备中响应本机 运行s js 代码。 既然你说它在浏览器上工作正常但在设备上坏了,我建议你检查以下内容:

  • 检查设备是否连接到同一网络
  • 运行 这个:adb reverse tcp:8081 tcp:8081
  • 检查开发服务器是否 运行ning,你可以 运行 它与:$ react-native start
  • 一些 JS 功能需要在 react-native 中进行 polyfill,例如 Symbol。所以将以下内容添加到 index.js:

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

祝你好运。

我发现问题与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

它已被 updating the JSC version that ships with android 解决,但我使用 JSC 版本 216113 而不是最新版本以保持 minSdkVersion 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!