简体   繁体   English

如何提高在React Native中传递大型JSON对象的性能?

[英]How can I improve the performance of passing around a large JSON object in React Native?

I'm building an app with React Native and on my transitions, where I'm loading more data, it's very slow and I'm wondering if there's a better way to structure my app to avoid these shutdowns, which get longer as the JSON file gets larger. 我正在使用React Native构建一个应用程序,并且在过渡时正在加载更多数据,这非常慢,我想知道是否有更好的方法来构建我的应用程序来避免这些关闭,因为关闭时间会更长文件变大。

Let's say I have 假设我有

var data = require('./data/data.json')

in my index view, I think pass this data to a view, which then splits it up to other views like items={data[section_name]} and it goes down recursively like that further down the hierarchy. 在我的索引视图中,我认为将这些数据传递给一个视图,然后将其拆分为其他视图,例如items={data[section_name]}并且像递归向下那样向下递归。 Creating smaller dicts on each level. 在每个级别上创建较小的字典。 And even with small objects it is still quite slow. 即使有小物体,它仍然相当缓慢。

The JSON files I'm working with are from 3-8 MBs. 我正在使用的JSON文件大小为3-8 MB。

I understand what you want do to with your big data in your project, because I did the same way before for my react-native app. 我了解您想对您的项目中的大数据做什么,因为以前我对本机应用程序做过同样的事情。

Actually, this way is fine, it's kind of loading the data once only, then we can retrieve hierarchy easily like items={data[section_name]} 实际上,这种方法很好,它只是一次加载数据,然后我们可以像items={data[section_name]}一样轻松地检索层次结构

However, the problem now is it may take a while for processing the big data! 但是,现在的问题是处理大数据可能需要一段时间!

Therefore, the idea is we should move the part of loading/initializing the data asynchronously, move it somewhere out of our view, so that the view's transition as well as its other things will not be affected! 因此,我们的想法是我们应该异步移动加载/初始化数据的部分,将其移到视图之外的某个位置,以使视图的过渡以及其他事物不会受到影响! Then, when the data are being loaded/processed, we may show something like fade-in, fade-out effects, on the view, so it will take a short-time (300-1000ms). 然后,当数据被加载/处理时,我们可能会在视图上显示诸如淡入,淡出效果之类的东西,因此这会花费很短的时间(300-1000ms)。 However, in my opinion, there may be a better way for this: when the application is opened for the first time, when the splash screen is being shown (for a few seconds, this behavior is common, we can load the big data in the background here!) 但是,在我看来,可能有更好的方法:第一次打开应用程序时,在显示初始屏幕时(这种现象很常见,在几秒钟内,我们可以将大数据加载到这里的背景!)

FYI, my application loads big data using ajax, it will even take longer than loading local files, but using my above-mentioned way, nobody complains about the data-loading speed ^^ (actually, users don't know and don't see where and when data are loaded, when the app goes to its home/top page, everything has been fully loaded) 仅供参考,我的应用程序使用ajax加载大数据,甚至比加载本地文件还要花费更长的时间,但是使用我上面提到的方式,没有人抱怨数据加载速度^^(实际上,用户不知道也不知道查看何时何地加载数据,当应用程序转到主页/首页时,一切都已完全加载)

Now it comes to the important part of my answer : I suggest you using Flux/Redux for this, maybe Flux is easier for you now, because Redux is kind of an improvement based on Flux (in either Flux or Redux, we will have a Store, and this Store will work asynchronously, you can store your big JSON data here): 现在我要回答的重要部分是 :我建议您为此使用Flux / Redux,也许现在对您来说Flux更容易,因为Redux是基于Flux的一种改进(无论是Flux还是Redux,我们都会有一个应用商店,该应用商店将异步运行,您可以在此处存储您的大型JSON数据):

https://www.atlassian.com/blog/software-teams/flux-architecture-step-by-step https://www.atlassian.com/blog/software-teams/flux-architecture-step-by-step

Flux can be applied on reactjs and react-native of course, you may find there are several new definitions if you don't know about Flux or Redux yet, however, it's really worth reading and trying! Flux可以应用在reactjs和react-native上,如果您不了解Flux或Redux,您可能会发现一些新的定义,但是,这确实值得阅读和尝试! After you understand it, it'll be very easy and then you can apply that architecture on whatever react project you like ^^ 了解它之后,这将非常容易,然后您可以将该架构应用于您喜欢的任何react项目^^

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

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