From 393085a9b0e92fa653bbedb59ff0d568e9db90cd Mon Sep 17 00:00:00 2001 From: DreamPiggy Date: Thu, 5 Apr 2018 18:44:38 +0800 Subject: [PATCH] Update the default downloader config to use the dispatch_once to avoid thread-safe issue --- SDWebImage/SDWebImageDownloaderConfig.h | 1 + SDWebImage/SDWebImageDownloaderConfig.m | 5 +++-- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/SDWebImage/SDWebImageDownloaderConfig.h b/SDWebImage/SDWebImageDownloaderConfig.h index c4e3792c..9f702bd6 100644 --- a/SDWebImage/SDWebImageDownloaderConfig.h +++ b/SDWebImage/SDWebImageDownloaderConfig.h @@ -25,6 +25,7 @@ typedef NS_ENUM(NSInteger, SDWebImageDownloaderExecutionOrder) { /** Gets/Sets the default downloader config used for shared instance or initialization when it does not provide any downloader config. Such as `SDWebImageDownloader.sharedDownloader`. + @note You can modify the property on default downloader config, which can be used for later created downloader instance. The already created downloader instance does not get affected. @note You should not pass nil to this value. */ @property (nonatomic, class, nonnull) SDWebImageDownloaderConfig *defaultDownloaderConfig; diff --git a/SDWebImage/SDWebImageDownloaderConfig.m b/SDWebImage/SDWebImageDownloaderConfig.m index 1a9f0f88..2d9d6136 100644 --- a/SDWebImage/SDWebImageDownloaderConfig.m +++ b/SDWebImage/SDWebImageDownloaderConfig.m @@ -13,9 +13,10 @@ static SDWebImageDownloaderConfig * _defaultDownloaderConfig; @implementation SDWebImageDownloaderConfig + (SDWebImageDownloaderConfig *)defaultDownloaderConfig { - if (!_defaultDownloaderConfig) { + static dispatch_once_t onceToken; + dispatch_once(&onceToken, ^{ _defaultDownloaderConfig = [SDWebImageDownloaderConfig new]; - } + }); return _defaultDownloaderConfig; }