简体   繁体   English

react-native 是否支持多线程和后台线程或并行执行? 我们怎么做?

[英]Does react-native support Multithreading and Background threading or Parallel Execution? How can we do that?

I have gone through the official documentation of react-native and some other medium sources and blogs, I came to know that there is UI Thread and JavaScript Thread in react-native.查阅了react-native的官方文档和其他一些媒体资源和博客,才知道react-native中有UI线程JavaScript线程 Javascript thread is the thread where the logic will run javascript code is executed, API calls are made, touch events are processed and many other. Javascript 线程是运行 javascript 代码的逻辑线程,进行 API 调用,处理触摸事件等等。 And UI Thread updates the UI.并且 UI Thread 更新 UI。 If JavaScript thread is working on the process and if we again perform button action or any other touch event it takes some time to respond, after completing one process only the UI Updates until than I feel like its lagging or it's stuck somewhere.如果 JavaScript 线程正在处理该过程,并且如果我们再次执行按钮操作或任何其他触摸事件,则需要一些时间来响应,在完成一个过程后仅 UI 更新,直到我觉得它滞后或卡在某处。

For example例如

There is a bottom tab navigator it has four tabs, There is a listview in the first tab and map view which shows the route between multiple points in the second tab有一个底部选项卡导航器,它有四个选项卡,第一个选项卡中有一个列表视图,第二个选项卡中显示多个点之间的路线的地图视图

I had performed some button action(clicked like button in the list view) in the first tab and immediately navigating to the second tab, It feels some lag in updating UI or it takes some time(1-3 sec) to navigate from the first tab to the second tab我在第一个选项卡中执行了一些按钮操作(单击列表视图中的按钮)并立即导航到第二个选项卡,感觉更新用户界面有些滞后,或者从第一个选项卡开始导航需要一些时间(1-3 秒)标签到第二个标签

In the second tab the map takes time(5-10 sec) to fetch route between multiple points when this process is going on if I navigate to any other tab the navigation between tabs is not smooth and lots of UI lag is present.在第二个选项卡中,如果我导航到任何其他选项卡,则在此过程进行时,地图需要时间(5-10 秒)来获取多个点之间的路线,选项卡之间的导航不流畅并且存在大量 UI 延迟。

How can we make the react-native app respond faster to touchable actions and reduce UI lag我们如何让 react-native 应用更快地响应可触摸操作并减少 UI 延迟

Is there any concept of Multithreading and Background threading that we can run particular logics in the main thread and update UI to make a react-native application to respond faster without any lag.有没有多线程后台线程的概念,我们可以在主线程中运行特定的逻辑并更新 UI 以使本机应用程序响应更快而没有任何延迟。

无法在 react-native 中使用线程,但您可以使用react-native-bg-thread等第三方软件包进行性能优化

React Native uses javascript wich is a single threaded language so in theory there shouldn't be a way to use multiple threads. React Native 使用 javascript,它是一种单线程语言,因此理论上不应该有使用多线程的方法。 If something is blocking the js thread you should move that code to native side implementing it in objective-c/swfit and java/kotlin so then you will be able to run that on a native thread and preventing the lock of the js thread.如果有什么东西阻塞了 js 线程,你应该将该代码移动到本地端在 Objective-c/swfit 和 java/kotlin 中实现它,这样你就可以在本地线程上运行它并防止 js 线程的锁定。 Anyway after Reanimated 2 was released the worklet apis made possibile to create this library which allows to create other js threads to run code and not blocking the main one无论如何,在 Reanimated 2 发布后,worklet apis 可以创建这个库,它允许创建其他 js 线程来运行代码而不阻塞主线程

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

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