Просмотр исходного кода

Change the context arg to init method because it should be readonly after created

DreamPiggy 8 лет назад
Родитель
Сommit
d6a3e2c

+ 1 - 2
SDWebImage/SDWebImageDownloader.m

@@ -225,9 +225,8 @@
         else {
             request.allHTTPHeaderFields = [sself allHTTPHeaderFields];
         }
-        SDWebImageDownloaderOperation *operation = [[sself.operationClass alloc] initWithRequest:request inSession:sself.session options:options];
+        SDWebImageDownloaderOperation *operation = [[sself.operationClass alloc] initWithRequest:request inSession:sself.session options:options context:context];
         operation.shouldDecompressImages = sself.shouldDecompressImages;
-        operation.context = context;
         
         if (sself.urlCredential) {
             operation.credential = sself.urlCredential;

+ 24 - 5
SDWebImage/SDWebImageDownloaderOperation.h

@@ -27,6 +27,11 @@ FOUNDATION_EXPORT NSString * _Nonnull const SDWebImageDownloadFinishNotification
                               inSession:(nullable NSURLSession *)session
                                 options:(SDWebImageDownloaderOptions)options;
 
+- (nonnull instancetype)initWithRequest:(nullable NSURLRequest *)request
+                              inSession:(nullable NSURLSession *)session
+                                options:(SDWebImageDownloaderOptions)options
+                                context:(nullable SDWebImageContext *)context;
+
 - (nullable id)addHandlersForProgress:(nullable SDWebImageDownloaderProgressBlock)progressBlock
                             completed:(nullable SDWebImageDownloaderCompletedBlock)completedBlock;
 
@@ -36,9 +41,6 @@ FOUNDATION_EXPORT NSString * _Nonnull const SDWebImageDownloadFinishNotification
 - (nullable NSURLCredential *)credential;
 - (void)setCredential:(nullable NSURLCredential *)value;
 
-- (nullable SDWebImageContext *)context;
-- (void)setContext:(nullable SDWebImageContext *)context;
-
 - (BOOL)cancel:(nullable id)token;
 
 @end
@@ -77,7 +79,7 @@ FOUNDATION_EXPORT NSString * _Nonnull const SDWebImageDownloadFinishNotification
 /**
  * The context for the receiver.
  */
-@property (copy, nonatomic, nullable) SDWebImageContext *context;
+@property (copy, nonatomic, readonly, nullable) SDWebImageContext *context;
 
 /**
  * The expected size of data.
@@ -102,7 +104,24 @@ FOUNDATION_EXPORT NSString * _Nonnull const SDWebImageDownloadFinishNotification
  */
 - (nonnull instancetype)initWithRequest:(nullable NSURLRequest *)request
                               inSession:(nullable NSURLSession *)session
-                                options:(SDWebImageDownloaderOptions)options NS_DESIGNATED_INITIALIZER;
+                                options:(SDWebImageDownloaderOptions)options;
+
+/**
+ *  Initializes a `SDWebImageDownloaderOperation` object
+ *
+ *  @see SDWebImageDownloaderOperation
+ *
+ *  @param request        the URL request
+ *  @param session        the URL session in which this operation will run
+ *  @param options        downloader options
+ *  @param context        A context contains different options to perform specify changes or processes, see `SDWebImageContextOption`. This hold the extra objects which `options` enum can not hold.
+ *
+ *  @return the initialized instance
+ */
+- (nonnull instancetype)initWithRequest:(nullable NSURLRequest *)request
+                              inSession:(nullable NSURLSession *)session
+                                options:(SDWebImageDownloaderOptions)options
+                                context:(nullable SDWebImageContext *)context NS_DESIGNATED_INITIALIZER;
 
 /**
  *  Adds handlers for progress and completion. Returns a tokent that can be passed to -cancel: to cancel this set of

+ 10 - 1
SDWebImage/SDWebImageDownloaderOperation.m

@@ -25,6 +25,9 @@ typedef NSMutableDictionary<NSString *, id> SDCallbacksDictionary;
 
 @property (strong, nonatomic, nonnull) NSMutableArray<SDCallbacksDictionary *> *callbackBlocks;
 
+@property (assign, nonatomic, readwrite) SDWebImageDownloaderOptions options;
+@property (copy, nonatomic, readwrite, nullable) SDWebImageContext *context;
+
 @property (assign, nonatomic, getter = isExecuting) BOOL executing;
 @property (assign, nonatomic, getter = isFinished) BOOL finished;
 @property (strong, nonatomic, nullable) NSMutableData *imageData;
@@ -59,13 +62,19 @@ typedef NSMutableDictionary<NSString *, id> SDCallbacksDictionary;
     return [self initWithRequest:nil inSession:nil options:0];
 }
 
+- (instancetype)initWithRequest:(NSURLRequest *)request inSession:(NSURLSession *)session options:(SDWebImageDownloaderOptions)options {
+    return [self initWithRequest:request inSession:session options:options];
+}
+
 - (nonnull instancetype)initWithRequest:(nullable NSURLRequest *)request
                               inSession:(nullable NSURLSession *)session
-                                options:(SDWebImageDownloaderOptions)options {
+                                options:(SDWebImageDownloaderOptions)options
+                                context:(nullable SDWebImageContext *)context {
     if ((self = [super init])) {
         _request = [request copy];
         _shouldDecompressImages = YES;
         _options = options;
+        _context = [context copy];
         _callbackBlocks = [NSMutableArray new];
         _executing = NO;
         _finished = NO;

+ 9 - 3
Tests/Tests/SDWebImageDownloaderTests.m

@@ -33,14 +33,20 @@
 
 @property (nonatomic, assign) BOOL shouldDecompressImages;
 @property (nonatomic, strong, nullable) NSURLCredential *credential;
-@property (nonatomic, assign) SDWebImageContext *context;
 
 @end
 
 @implementation CustomDownloaderOperation
 
-- (nonnull instancetype)initWithRequest:(nullable NSURLRequest *)req inSession:(nullable NSURLSession *)ses options:(SDWebImageDownloaderOptions)opt {
-    if ((self = [super init])) { }
+- (instancetype)initWithRequest:(NSURLRequest *)request inSession:(NSURLSession *)session options:(SDWebImageDownloaderOptions)options {
+    return [self initWithRequest:request inSession:session options:options];
+}
+
+- (instancetype)initWithRequest:(NSURLRequest *)request inSession:(NSURLSession *)session options:(SDWebImageDownloaderOptions)options context:(SDWebImageContext *)context {
+    self = [super init];
+    if (self) {
+        
+    }
     return self;
 }