瀏覽代碼

Fixed an issue where registering a UIProgessView to a task that was already completed would crash with the following error:
CRASH: Fatal exception: An instance ***** of class ******* was deallocated while key value observers were still registered with it.

Mathieu Meylan 9 年之前
父節點
當前提交
7dc973b89c
共有 1 個文件被更改,包括 8 次插入0 次删除
  1. 8 0
      UIKit+AFNetworking/UIProgressView+AFNetworking.m

+ 8 - 0
UIKit+AFNetworking/UIProgressView+AFNetworking.m

@@ -55,6 +55,10 @@ static void * AFTaskCountOfBytesReceivedContext = &AFTaskCountOfBytesReceivedCon
 - (void)setProgressWithUploadProgressOfTask:(NSURLSessionUploadTask *)task
                                    animated:(BOOL)animated
 {
+    if (task.state == NSURLSessionTaskStateCompleted) {
+        return;
+    }
+    
     [task addObserver:self forKeyPath:@"state" options:(NSKeyValueObservingOptions)0 context:AFTaskCountOfBytesSentContext];
     [task addObserver:self forKeyPath:@"countOfBytesSent" options:(NSKeyValueObservingOptions)0 context:AFTaskCountOfBytesSentContext];
 
@@ -64,6 +68,10 @@ static void * AFTaskCountOfBytesReceivedContext = &AFTaskCountOfBytesReceivedCon
 - (void)setProgressWithDownloadProgressOfTask:(NSURLSessionDownloadTask *)task
                                      animated:(BOOL)animated
 {
+    if (task.state == NSURLSessionTaskStateCompleted) {
+        return;
+    }
+    
     [task addObserver:self forKeyPath:@"state" options:(NSKeyValueObservingOptions)0 context:AFTaskCountOfBytesReceivedContext];
     [task addObserver:self forKeyPath:@"countOfBytesReceived" options:(NSKeyValueObservingOptions)0 context:AFTaskCountOfBytesReceivedContext];