diff --git a/packages/react-native/Libraries/Image/RCTImageLoader.mm b/packages/react-native/Libraries/Image/RCTImageLoader.mm index d6db050e35c..68a12d35122 100644 --- a/packages/react-native/Libraries/Image/RCTImageLoader.mm +++ b/packages/react-native/Libraries/Image/RCTImageLoader.mm @@ -68,6 +68,8 @@ static NSError *addResponseHeadersToError(NSError *originalError, NSHTTPURLRespo @end @implementation RCTImageLoader { + std::atomic _isLoaderSetup; + std::mutex _loaderSetupLock; NSArray> * (^_loadersProvider)(RCTModuleRegistry *); NSArray> * (^_decodersProvider)(RCTModuleRegistry *); NSArray> *_loaders; @@ -106,6 +108,7 @@ RCT_EXPORT_MODULE() { if (self = [super init]) { _redirectDelegate = redirectDelegate; + _isLoaderSetup = NO; } return self; } @@ -123,12 +126,16 @@ RCT_EXPORT_MODULE() - (void)setUp { - // Set defaults - _maxConcurrentLoadingTasks = _maxConcurrentLoadingTasks ?: 4; - _maxConcurrentDecodingTasks = _maxConcurrentDecodingTasks ?: 2; - _maxConcurrentDecodingBytes = _maxConcurrentDecodingBytes ?: 30 * 1024 * 1024; // 30MB + std::lock_guard guard(_loaderSetupLock); + if (!_isLoaderSetup) { + // Set defaults + _maxConcurrentLoadingTasks = _maxConcurrentLoadingTasks ?: 4; + _maxConcurrentDecodingTasks = _maxConcurrentDecodingTasks ?: 2; + _maxConcurrentDecodingBytes = _maxConcurrentDecodingBytes ?: 30 * 1024 * 1024; // 30MB - _URLRequestQueue = dispatch_queue_create("com.facebook.react.ImageLoaderURLRequestQueue", DISPATCH_QUEUE_SERIAL); + _URLRequestQueue = dispatch_queue_create("com.facebook.react.ImageLoaderURLRequestQueue", DISPATCH_QUEUE_SERIAL); + _isLoaderSetup = YES; + } } - (float)handlerPriority @@ -156,7 +163,7 @@ RCT_EXPORT_MODULE() - (id)imageURLLoaderForURL:(NSURL *)URL { - if (!_maxConcurrentLoadingTasks) { + if (!_isLoaderSetup) { [self setUp]; } @@ -229,7 +236,7 @@ RCT_EXPORT_MODULE() - (id)imageDataDecoderForData:(NSData *)data { - if (!_maxConcurrentLoadingTasks) { + if (!_isLoaderSetup) { [self setUp]; } @@ -564,7 +571,7 @@ static UIImage *RCTResizeImageIfNeeded(UIImage *image, CGSize size, CGFloat scal } // All access to URL cache must be serialized - if (!_URLRequestQueue) { + if (!_isLoaderSetup) { [self setUp]; } @@ -979,7 +986,7 @@ static UIImage *RCTResizeImageIfNeeded(UIImage *image, CGSize size, CGFloat scal }); }; - if (!_URLRequestQueue) { + if (!_isLoaderSetup) { [self setUp]; } dispatch_async(_URLRequestQueue, ^{