android - react-native 上的 Realm : can't get results when not debugging

标签 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 和工具的版本

  • Realm JS SDK 版本:2.10.0
  • React Native: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>
    );
  }
}

最佳答案

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

关于android - react-native 上的 Realm : can't get results when not debugging,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51002487/

相关文章:

android - 如何从动态字段名更新 Realm 值

android - 在哪里放置 Twitter Key for Android 的 API key

android - 当应用程序从暂停状态返回时,如何发现应用程序卡住原因?

xcode - 这些红色靶心图标在 Xcode 中是什么意思?

ios - 无法用存储的属性覆盖 'description'

swift - 当网络连接不可用时,Realm 使用本地数据库而不是同步?

android - packageInfo.versionCode 总是返回 1

java - AdMob NativeExpressAds 拒绝显示在实际的 Android 设备上

javascript - React Native - 复选框无法取消选中 "checked"框

javascript - 在ScrollView问题中 react Native FlatList