【问题标题】:Tried to synchronously call function {w} from a different thread - React Native Reanimated试图从不同的线程同步调用函数 {w} - React Native Reanimated
【发布时间】:2022-10-14 22:27:18
【问题描述】:
我的滑动功能中的 runOnJS 有问题。
我总是出错:
java.lang.RuntimeException: Tried to synchronously call function {w} from a different thread.
我得到了错误平移手势调用时的功能完成动画.
代码在这里:
https://pastebin.com/YaQs4bN6
【问题讨论】:
标签:
react-native
react-native-reanimated
react-native-reanimated-v2
【解决方案2】:
您正在从 onEnd 回调中调用“finishAnimation”。这可能是个问题,因为 finishAnimation 不是工作集。
所以你有两个选择:
-
finishAnimation 可以用“worklet”关键字标记
const finishAnimation = (swipe_down) => {
"worklet";
// This logger can't be here anymore since it's a JS function
// Logger.bool(swipe_down, { swipe_down });
if (swipe_down) {
offset.value = withTiming(height.value, { duration: 100 }, () =>
runOnJS(props.onSwipeComplete)()
);
} else {
offset.value = withTiming(0, { duration: 200 });
}
};
-
finishAnimation 可以在 JS 线程上调用 async:
runOnJS(finishAnimation)(
e.velocityY > swipeOutVelocity || offset.value > calculateThreshold()
);
希望它会奏效。