Parallel fetch requests in react native

无人久伴 提交于 2020-01-14 10:09:35

问题


I am developing a new app in react native and I need to make 20 fetches to my api in parallel. When I developed in phone gap, I could create 20 web workers for the Ajax calls to happen parallel. When I am executing 20 fetches in parallel in react native it looks like every fetch is taking longer than the one before. Like it has a queue of fetches and it won't run them together.

Is there any way to solve this? Now it takes like 1 minute to finish the fetches when in my phonegap app it takes like 10 secs.. Help would be much appreciated


回答1:


The number of connections per host is limited to four in iOS. You need to increase HTTPMaximumConnectionsPerHost in NSURLSession.

The ugly way to test this is to directly add the following line to node_modules/react-native/Libraries/Network/RCTHTTPRequestHandler.m: NSURLSessionConfiguration

[configuration setHTTPMaximumConnectionsPerHost:25];

Read more: https://developer.apple.com/documentation/foundation/nsurlsessionconfiguration/1407597-httpmaximumconnectionsperhost?language=objc




回答2:


You could use Promise.all to run all your promises/fetch at the same time and then wait for the response.

https://developer.mozilla.org/es/docs/Web/JavaScript/Referencia/Objetos_globales/Promise/all



来源:https://stackoverflow.com/questions/47478687/parallel-fetch-requests-in-react-native

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!