简体   繁体   English

如何防止React Native中的setInterval阻止正在运行的动画?

[英]How to prevent setInterval in react native from blocking running animation?

I have a react native application. 我有一个反应本地应用程序。 There are 2 components: tickets and ticket details. 有2个部分:票证和票证详细信息。 In the "tickets" component there is a list of tickets. 在“票证”组件中,有一个票证列表。 When a user clicks on a single ticket the "ticket details" component is shown. 当用户单击单个票证时,将显示“票证详细信息”组件。 In the "tickets" component there is the setInterval which retrieves tickets from the server every 10 seconds. 在“票证”组件中,有一个setInterval,它每10秒从服务器检索一次票证。 In the "ticket details" component there is the animated progress bar(using react native Animated module). 在“门票详细信息”组件中,有动画进度条(使用react native Animated模块)。

Issue: every 10 seconds animation in "ticket details" component freezes. 问题:“门票详细信息”组件中的动画每10秒冻结一次。 How to solve? 怎么解决?

I guess that is because of setInterval blocks main UI thread. 我猜这是因为setInterval阻止了主UI线程。

I tried: - https://facebook.github.io/react-native/docs/interactionmanager.html but I didn't even manage to call runAfterInteractions on my RN 0.58. 我想: - https://facebook.github.io/react-native/docs/interactionmanager.html ,但我甚至不设法打电话对我的RN 0.58 runAfterInteractions。

The next thing that comes to mind is https://github.com/joltup/react-native-threads . 接下来想到的是https://github.com/joltup/react-native-threads But it is a native module with some scary issues like "memory leak" and additional build instructions so I don't really want to use it. 但这是一个本机模块,存在一些令人恐惧的问题,例如“内存泄漏”和其他构建指令,因此我真的不想使用它。

I think I miss a way easier method to solve this issue. 我想我想念一种更简单的方法来解决这个问题。

This is how I get tickets in the "tickets" component. 这就是我在“票证”组件中获取票证的方式。 getTickets is a redux action. getTickets是一个redux操作。

componentDidMount() {
this.timer = setInterval(() => {
    const {getTickets} = this.props;
    getTickets();
 }, 10000);
}

This is the animated progress bar from the "ticket details" component. 这是“票证详细信息”组件中的动画进度条。 I doubt that it was useful concerning this issue. 我怀疑这在此问题上是否有用。 Just for reference. 仅供参考。

<ProgressBarAnimated 
    value={100} 
    barAnimationDuration={progressBarAnimationDuration} 
    width={progressBarWidth}
    borderWidth={0}
    backgroundColor='#1d1e1f'
    height={8}
    borderRadius={0} />

I expect setInterval not to freeze progress bar animation. 我希望setInterval不会冻结进度栏动画。

Once again how I see the process: 我再次看到该过程:

  1. progress bar animation is running 进度条动画正在运行

  2. every 10th-second setInterval performs HTTP request and blocks main UI thread(with progress bar animation) for ~2 seconds 每隔10秒setInterval执行HTTP请求并阻塞主UI线程(带有进度条动画)2秒钟

  3. when the request is done animation is back to normal 请求完成后,动画恢复正常

The animation was blocked because it was running in JS thread instead of UI thread. 动画被阻止,因为它在JS线程而不是UI线程中运行。

You should run animation in the UI thread via https://facebook.github.io/react-native/docs/animated with option useNativeDriver: true . 您应该通过https://facebook.github.io/react-native/docs/animated在UI线程中运行动画,并带有useNativeDriver:true选项。 With this option animation is smooth. 使用此选项,动画将变得流畅。

Also notice that if you use PanResponder + Animated then useNativeDriver option does not work . 另请注意,如果您使用PanResponder + Animated,则useNativeDriver选项不起作用 You can go with https://github.com/wix/react-native-interactable . 您可以使用https://github.com/wix/react-native-interactable I've tried it and all animations on touches are running in the UI thread so animation is not blocked. 我已经尝试过,并且所有触摸动画都在UI线程中运行,因此动画不会被阻止。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM