Inheriting from the Nsoperation class overrides the-start method. The status of the manual management operation is only considered to be the end of the operation when the isfinished KVO message is sent. In order for the operation queue to capture the changes to the operation, the properties of the state need to be implemented in a manner that fits kvo.
- Isexecuting on behalf of the task being executed
- Isfinished on behalf of the task has been completed
- IsCancelled on behalf of the task has been canceled execution
-(void) setfinished: (BOOL) finished { [self willchangevalueforkey:@ "isfinished"]; _finished = finished; [self didchangevalueforkey:@ "isfinished"];} -start1. Thread synchronization lock (self as a mutex) determines cancel, if yes, considers the task complete and promptly reclaims the resource (-reset) 2. Determine whether to enter the background, if you enter the background, the current task in the background to execute. Sself.backgroundtaskid = Uibackgroundtaskinvalid; means to tell the system that the task is completed without having to consider a problem that does not run into the background. Indeed, at the end of the start function, the backgroundtaskid is determined to be uibackgroundtaskinvalid if the download task is completed (whether or not the download is successful). 3. Initialize Nsurlconnection [self.connection start];4.connection Create success, return Progress (0,size). 5. Send start notification: Sdwebimagedownloadstartnotification6.runloop on 7. Determine the Isfinished property, if no, cancel the network, return the error message. Recycle resources. -cancel-(void) Cancel { @synchronized (self) { if ( Self.thread) { [self performSelector: @selector ( Cancelinternalandstop) OnThread:self.thread Withobject:nil waituntildone:no]; } Else { [Self cancelinternal]; } }} Every nstread will have a cfrunloop, so if you want to cancel, stop this runloop. So if Self.thread exists, stop runloop-(void) cancelinternal
- Invokes a custom cancelblock.
- Call Cancel Self.connection of nsurlconnection.
- Recycle resources.
Cancel Operation IsCancelled does not have real-time operation, detects at start, overrides the Cancel method, callback Cancelblock, network request cancellation, state setting, and a notification Sdwebimagedownloadstopnotification. Stop the thread and leave the property blank. Completion Status:-done sets the finished,executing state, and the property is set to null. Network Request Didreceivedata Method: The ImageData array adds the received data calculation direction, the picture height is fast reading toward, the orientation attribute is used to record the direction. Then resolve the distortion problem network request succeeded: Convert the requested data arr (image) to UIImage, cache the URL to [Sdwebimagemanager Sharedmanager], Sdscaledimageforkey method: Zoom the picture (depending on the screen), then unzip the-decodedimagewithimage
6 Sdwebimagedownloaderoperation Class