DorR
DorR

Reputation: 645

Parallel fetch requests in react native

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?

Upvotes: 7

Views: 3890

Answers (2)

Rikard Wissing
Rikard Wissing

Reputation: 61

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

Upvotes: 6

sebastianf182
sebastianf182

Reputation: 9978

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

Upvotes: -1

Related Questions