首页 > 解决方案 > HostFunction 中的异常:来自 JS 的格式错误的调用:字段大小不同 - 问题发生在真实设备上,而不是在模拟器上

问题描述

在 Expo/React Native 项目中的一堆代码之后,我想在我的 iPhone 上测试它。第一分钟进展顺利,直到我收到意外的错误消息:

错误:HostFunction 中的异常:来自 JS 的错误调用:字段大小不同。

我在开发过程中多次遇到错误,因为我不小心在某处存储了 NaN。到目前为止一切顺利,很高兴错误消息直接出现,我知道从哪里开始摆脱它。

以下是错误:

[Unhandled promise rejection: Error: Exception in HostFunction: Malformed calls from JS: field sizes are different.]
Stack trace:
  node_modules/react-native/Libraries/BatchedBridge/MessageQueue.js:271:39 in enqueueNativeCall
  node_modules/react-native/Libraries/BatchedBridge/NativeModules.js:137:8 in fn
  node_modules/react-native/Libraries/Core/Timers/JSTimers.js:259:23 in setTimeout
  node_modules/promise/setimmediate/rejection-tracking.js:47:10 in <unknown>
  node_modules/promise/setimmediate/core.js:169:16 in reject
  node_modules/promise/setimmediate/core.js:211:11 in doResolve
  node_modules/promise/setimmediate/core.js:66:12 in Promise
  node_modules/react-native/Libraries/BatchedBridge/NativeModules.js:98:25 in fn
  node_modules/@unimodules/react-native-adapter/build/NativeModulesProxy.js:15:52 in _callee$
  node_modules/regenerator-runtime/runtime.js:45:44 in tryCatch
  node_modules/regenerator-runtime/runtime.js:271:30 in invoke
  node_modules/regenerator-runtime/runtime.js:45:44 in tryCatch
  node_modules/regenerator-runtime/runtime.js:135:28 in invoke
  node_modules/regenerator-runtime/runtime.js:170:17 in <unknown>
  node_modules/promise/setimmediate/core.js:45:7 in tryCallTwo
  node_modules/promise/setimmediate/core.js:200:23 in doResolve
  node_modules/promise/setimmediate/core.js:66:12 in Promise
  node_modules/regenerator-runtime/runtime.js:169:27 in callInvokeWithMethodAndArg
  node_modules/regenerator-runtime/runtime.js:192:38 in enqueue
  node_modules/regenerator-runtime/runtime.js:216:8 in <unknown>
  node_modules/@unimodules/react-native-adapter/build/NativeModulesProxy.js:10:62 in _callee
  node_modules/expo-file-system/build/FileSystem.js:50:55 in writeAsStringAsync$
  node_modules/regenerator-runtime/runtime.js:45:44 in tryCatch
  node_modules/regenerator-runtime/runtime.js:271:30 in invoke
  node_modules/regenerator-runtime/runtime.js:45:44 in tryCatch
  node_modules/regenerator-runtime/runtime.js:135:28 in invoke
  node_modules/regenerator-runtime/runtime.js:170:17 in <unknown>
  node_modules/promise/setimmediate/core.js:45:7 in tryCallTwo
  node_modules/promise/setimmediate/core.js:200:23 in doResolve
  node_modules/promise/setimmediate/core.js:66:12 in Promise
  node_modules/regenerator-runtime/runtime.js:169:27 in callInvokeWithMethodAndArg
  node_modules/regenerator-runtime/runtime.js:192:38 in enqueue
  node_modules/regenerator-runtime/runtime.js:216:8 in <unknown>
  node_modules/expo-file-system/build/FileSystem.js:46:7 in writeAsStringAsync
  node_modules/redux-persist-expo-filesystem/index.js:13:34 in writeFile
  node_modules/redux-persist-expo-filesystem/index.js:26:30 in <unknown>
  node_modules/promise/setimmediate/core.js:37:14 in tryCallOne
  node_modules/promise/setimmediate/core.js:123:25 in <unknown>
  node_modules/react-native/Libraries/Core/Timers/JSTimers.js:152:14 in _callTimer
  node_modules/react-native/Libraries/Core/Timers/JSTimers.js:200:17 in _callImmediatesPass
  node_modules/react-native/Libraries/Core/Timers/JSTimers.js:473:30 in callImmediates
  node_modules/react-native/Libraries/BatchedBridge/MessageQueue.js:337:6 in __callImmediates
  node_modules/react-native/Libraries/BatchedBridge/MessageQueue.js:135:6 in <unknown>
  node_modules/react-native/Libraries/BatchedBridge/MessageQueue.js:314:10 in __guard
  node_modules/react-native/Libraries/BatchedBridge/MessageQueue.js:134:17 in flushedQueue

多次启动应用程序后,错误发生的频率更高,并且在使用几秒钟后。redux-persist 存储是主要问题。我无法解释为什么它在模拟器上运行良好(并且仍然运行良好)而在真实设备上却不行。

我尝试了多个存储包,如“redux-persist-expo-fs-storage”、“redux-persist/lib/storage”和“redux-persist-expo-filesystem”。他们都没有做这项工作。

redux/index.js 文件:

import { combineReducers, createStore, applyMiddleware } from 'redux';
import promiseMiddleware from 'redux-promise-middleware';
import { persistStore, persistReducer } from 'redux-persist';
import guidelines from './guidelines/reducers';
import global from './global/reducers';
import ExpoFileSystemStorage from 'redux-persist-expo-filesystem';
import { composeWithDevTools } from 'redux-devtools-extension';
const persistConfig = {
    key: 'root',
    keyPrefix:'',
    storage: ExpoFileSystemStorage,
    timeout: null
}
const reducers = combineReducers({
    global,
    guidelines,
})
const persistedReducer = persistReducer(persistConfig, reducers);
export const store = createStore(persistedReducer, composeWithDevTools(applyMiddleware(promiseMiddleware())));
export const persistor = persistStore(store);

函数 promiseMiddleware 用于处理 redux 中的异步操作创建者,如下所示 axios 用于检索 actions.js 中的数据:

import axios from 'axios';
...
export const FETCH_AVAILABLE_GUIDELINES = 'FETCH_AVAILABLE_GUIDELINES';
export const FETCH_AVAILABLE_GUIDELINES_PENDING = 'FETCH_AVAILABLE_GUIDELINES_PENDING';
export const FETCH_AVAILABLE_GUIDELINES_FULFILLED = 'FETCH_AVAILABLE_GUIDELINES_FULFILLED';
export const FETCH_AVAILABLE_GUIDELINES_REJECTED = 'FETCH_AVAILABLE_GUIDELINES_REJECTED';
export const fetchAvailableGuidelines = () => {
    return {
        type: FETCH_AVAILABLE_GUIDELINES,
        payload: axios.get(CONFIG.API.ROOT + CONFIG.API.PATHS.OVERVIEW),
    }
};
...

减速器.js

import { FETCH_AVAILABLE_GUIDELINES_FULFILLED } from './actions'
const initialState = () => {
    return {
        availableGuidelines: [],
        ...
    }
}
export default (state = initialState, action) => {
    switch(action.type) {
        case FETCH_AVAILABLE_GUIDELINES_FULFILLED:
             return {
                 ...state,
                 availableGuidelines: [...action.payload.data],
             }
        ...
    }
}

有没有人有同样的问题或提示如何调试我的 redux-persist 存储。存储的数据是一个大型 JSON 对象。我确信所有键都有有效值。

感谢您的任何建议和帮助。

编辑:我的指南每个大约 6MB。如果我只取一个,它似乎不会崩溃。一旦下载了多个指南,它就会崩溃。我相信这与存储系统有关。

expo: 35.0
redux: ^4.0.1
redux-persist: ^5.10.0

标签: reactjsreact-nativereduxreact-reduxexpo

解决方案


如果您在每次应用程序启动时都获取此数据,并且您不关心此数据是否离线持久化,我建议使用blacklist配置redux-persist将此类大型状态从持久化中列入黑名单。

如果此状态是静态的并且没有变化 - 您也可以将其写入 JSON 文件并将该文件用作数据源。


推荐阅读