AFNetworking知识点之AFURLSessionManager

百家 作者:iOS开发 2017-10-08 11:09:06

点击上方“iOS开发”,选择“置顶公众号”

关键时刻,第一时间送达!


AFNetworking是在iOS开发者中最受欢迎的网络框架,于是也就成了众多iOS开发者学习的对象,里面确实有很多值得我们学习的知识点,出去找工作几乎也都会问到这些东西,网上关于AFN的解析数不胜数,我们今天就不做什么解析了,只把其中涉及到的知识点做一下笔记。


static dispatch_queue_t url_session_manager_creation_queue() {

    static dispatch_queue_t af_url_session_manager_creation_queue;

    static dispatch_once_t onceToken;

    dispatch_once(&onceToken, ^{

        af_url_session_manager_creation_queue = dispatch_queue_create("com.alamofire.networking.session.manager.creation", DISPATCH_QUEUE_SERIAL);

    });


    return af_url_session_manager_creation_queue;

}


这个静态函数主要涉及的知识点都是GCD方面的,  dspatch_once函数是一个block只执行一次的函数,然后用dispatch_queue_create(const char *_Nullable label, dispatch_queue_attr_t _Nullable attr)函数用于创建一个队列,第一个参数是队列名,第二个参数是队列属性(同步(DISPATCH_QUEUE_SERIAL)、异步(DISPATCH_QUEUE_CONCURRENT))。


static void url_session_manager_create_task_safely(dispatch_block_t block) {

    if (NSFoundationVersionNumber < NSFoundationVersionNumber_With_Fixed_5871104061079552_bug) {

        // Fix of bug

        // Open Radar:http://openradar.appspot.com/radar?id=5871104061079552 (status: Fixed in iOS8)

        // Issue about:https://github.com/AFNetworking/AFNetworking/issues/2093

        dispatch_sync(url_session_manager_creation_queue(), block);

    } else {

        block();

    }

}


这个函数是为了修复iOS8之前的bug,如果版本号是iOS8之前的则调用dispatch_sync(url_session_manager_creation_queue(), block);这句把block加入上一个函数生成的同步队列中去执行,如果不是就直接执行block。


static dispatch_group_t url_session_manager_completion_group() {

    static dispatch_group_t af_url_session_manager_completion_group;

    static dispatch_once_t onceToken;

    dispatch_once(&onceToken, ^{

        af_url_session_manager_completion_group = dispatch_group_create();

    });


    return af_url_session_manager_completion_group;

}


这个方法是创建一个调度组,也是GCD里的函数,可以把任务放在队列中特定的调度组里。


for (NSProgress *progress in @[ _uploadProgress, _downloadProgress ])

    {

        progress.totalUnitCount = NSURLSessionTransferSizeUnknown;

        progress.cancellable = YES;

        progress.cancellationHandler = ^{

            [weakTask cancel];

        };

        progress.pausable = YES;

        progress.pausingHandler = ^{

            [weakTask suspend];

        };

        if ([progress respondsToSelector:@selector(setResumingHandler:)]) {

            progress.resumingHandler = ^{

                [weakTask resume];

            };

        }

        [progress addObserver:self

                   forKeyPath:NSStringFromSelector(@selector(fractionCompleted))

                      options:NSKeyValueObservingOptionNew

                      context:NULL];

    }


respondsToSelector判断对象能否响应方法,- (void)addObserver:(NSObject *)observer forKeyPath:(NSString *)keyPath options:(NSKeyValueObservingOptions)options context:(nullable void *)context我们常常说的kvo,给oc对象添加观察者。


- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {

   if ([object isEqual:self.downloadProgress]) {

        if (self.downloadProgressBlock) {

            self.downloadProgressBlock(object);

        }

    }

    else if ([object isEqual:self.uploadProgress]) {

        if (self.uploadProgressBlock) {

            self.uploadProgressBlock(object);

        }

    }

}


kvo的回调方法,当你观察的keyPath发生变化时调用这个方法。


- (void)URLSession:(NSURLSession *)session

      downloadTask:(NSURLSessionDownloadTask *)downloadTask

didFinishDownloadingToURL:(NSURL *)location

{

    self.downloadFileURL = nil;


    if (self.downloadTaskDidFinishDownloading) {

        self.downloadFileURL = self.downloadTaskDidFinishDownloading(session, downloadTask, location);

        if (self.downloadFileURL) {

            NSError *fileManagerError = nil;


            if (![[NSFileManager defaultManager] moveItemAtURL:location toURL:self.downloadFileURL error:&fileManagerError]) {

                [[NSNotificationCenter defaultCenter] postNotificationName:AFURLSessionDownloadTaskDidFailToMoveFileNotification object:downloadTask userInfo:fileManagerError.userInfo];

            }

        }

    }

}


下载任务完成时调用的方法,下载任务是把下载到的文件临时放在location的文字,需要我们手动调用- (BOOL)moveItemAtURL:(NSURL *)srcURL toURL:(NSURL *)dstURL error:(NSError **)error方法放到指定位置。


static inline void af_swizzleSelector(Class theClass, SEL originalSelector, SEL swizzledSelector) {

    Method originalMethod = class_getInstanceMethod(theClass, originalSelector);

    Method swizzledMethod = class_getInstanceMethod(theClass, swizzledSelector);

    method_exchangeImplementations(originalMethod, swizzledMethod);

}


static inline BOOL af_addMethod(Class theClass, SEL selector, Method method) {

    return class_addMethod(theClass, selector,  method_getImplementation(method),  method_getTypeEncoding(method));

}


这里涉及到的是iOS的runtime方面的知识,现在有一部分人的观点就是runtime没用,这不就被打脸了,还是很有用的。

OBJC_EXPORT Method class_getInstanceMethod(Class cls, SEL name)函数用于获取类的实例方法,第一个参数就是类名,第二个参数要获取的方法名。

OBJC_EXPORT void method_exchangeImplementations(Method m1, Method m2)交换两个参数的实现指针。

OBJC_EXPORT IMP method_getImplementation(Method m)获取方法的实现。

OBJC_EXPORT const char *method_getTypeEncoding(Method m)获取方法的编码类型,包括参数和返回值,类似v32@0:8@16@24这种。

OBJC_EXPORT BOOL class_addMethod(Class cls, SEL name, IMP imp, const char *types)在类中添加新的方法。


+ (void)load {

    //https://github.com/AFNetworking/AFNetworking/pull/2702

    if (NSClassFromString(@"NSURLSessionTask")) {

        /**

         iOS 7和iOS 8在NSURLSessionTask实现上有些许不同,这使得下面的代码实现略显trick

         关于这个问题,大家做了很多Unit Test,足以证明这个方法是可行的

         目前我们所知的:

            - NSURLSessionTasks是一组class的统称,如果你仅仅使用提供的API来获取NSURLSessionTask的class,并不一定返回的是你想要的那个(获取NSURLSessionTask的class目的是为了获取其resume方法)

            - 简单地使用[NSURLSessionTask class]并不起作用。你需要新建一个NSURLSession,并根据创建的session再构建出一个NSURLSessionTask对象才行。

            - iOS 7上,localDataTask(下面代码构造出的NSURLSessionDataTask类型的变量,为了获取对应Class)的类型是 __NSCFLocalDataTask,__NSCFLocalDataTask继承自__NSCFLocalSessionTask,__NSCFLocalSessionTask继承自__NSCFURLSessionTask。

            - iOS 8上,localDataTask的类型为__NSCFLocalDataTask,__NSCFLocalDataTask继承自__NSCFLocalSessionTask,__NSCFLocalSessionTask继承自NSURLSessionTask

          - iOS 7上,__NSCFLocalSessionTask和__NSCFURLSessionTask是仅有的两个实现了resume和suspend方法的类,另外__NSCFLocalSessionTask中的resume和suspend并没有调用其父类(即__NSCFURLSessionTask)方法,这也意味着两个类的方法都需要进行method swizzling。

            - iOS 8上,NSURLSessionTask是唯一实现了resume和suspend方法的类。这也意味着其是唯一需要进行method swizzling的类

            - 因为NSURLSessionTask并不是在每个iOS版本中都存在,所以把这些放在此处(即load函数中),比如给一个dummy class添加swizzled方法都会变得很方便,管理起来也方便。


         一些假设前提:

            - 目前iOS中resume和suspend的方法实现中并没有调用对应的父类方法。如果日后iOS改变了这种做法,我们还需要重新处理

            - 没有哪个后台task会重写resume和suspend函数

         */

        NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration ephemeralSessionConfiguration];

        NSURLSession * session = [NSURLSession sessionWithConfiguration:configuration];

#pragma GCC diagnostic push

#pragma GCC diagnostic ignored "-Wnonnull"

        NSURLSessionDataTask *localDataTask = [session dataTaskWithURL:nil];

#pragma clang diagnostic pop

        IMP originalAFResumeIMP = method_getImplementation(class_getInstanceMethod([self class], @selector(af_resume)));

        Class currentClass = [localDataTask class];

        

        while (class_getInstanceMethod(currentClass, @selector(resume))) {

            Class superClass = [currentClass superclass];

            IMP classResumeIMP = method_getImplementation(class_getInstanceMethod(currentClass, @selector(resume)));

            IMP superclassResumeIMP = method_getImplementation(class_getInstanceMethod(superClass, @selector(resume)));

            if (classResumeIMP != superclassResumeIMP &&

                originalAFResumeIMP != classResumeIMP) {

                [self swizzleResumeAndSuspendMethodForClass:currentClass];

            }

            currentClass = [currentClass superclass];

        }

        

        [localDataTask cancel];

        [session finishTasksAndInvalidate];

    }

}


上面的注释已经解释清楚了,主要是针对iOS7和iOS8上resume和suspend方法实现的不同,手动添加了af_resume和af_suspend方法,然后利用上面的runtime函数,交换实现指针。


- (NSArray *)tasksForKeyPath:(NSString *)keyPath {

    __block NSArray *tasks = nil;

    dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);

    [self.session getTasksWithCompletionHandler:^(NSArray *dataTasks, NSArray *uploadTasks, NSArray *downloadTasks) {

        if ([keyPath isEqualToString:NSStringFromSelector(@selector(dataTasks))]) {

            tasks = dataTasks;

        } else if ([keyPath isEqualToString:NSStringFromSelector(@selector(uploadTasks))]) {

            tasks = uploadTasks;

        } else if ([keyPath isEqualToString:NSStringFromSelector(@selector(downloadTasks))]) {

            tasks = downloadTasks;

        } else if ([keyPath isEqualToString:NSStringFromSelector(@selector(tasks))]) {

            tasks = [@[dataTasks, uploadTasks, downloadTasks] valueForKeyPath:@"@unionOfArrays.self"];

        }


        dispatch_semaphore_signal(semaphore);

    }];


    dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);


    return tasks;

}


这个方法里涉及两个知识点,一个是关于信号量,一个是关于kvc。

关于信号量的知识我在前面GCD中也是讲过的,这里简单写一下:


dispatch_semaphore_create 创建一个semaphore 

dispatch_semaphore_signal 发送一个信号 

dispatch_semaphore_wait 等待信号


信号量的主要作用就是控制并发量,信号量为0则阻塞线程,大于0则不会阻塞。我们通过改变信号量的值,来控制是否阻塞线程,从而控制并发控制。

valueForKeyPath:方法是kvc中的方法,这里说一下这个方法的一些用法:


@count:返回一个值为集合中对象总数的NSNumber对象

@sum: 首先把集合中的每个对象都转换为double类型,然后计算其总,最后返回一个值为这个总和的NSNumber对象

@avg: 首先把集合中的每个对象都转换为double类型,然后计算其平均值,最后返回一个值为这个总和的NSNumber对象

@max: 使用compare:方法来确定最大值。所以为了让其正常工作,集合中所有的对象都必须支持和另一个对象的比较

@min: 但是返回的是集合中的最小值。

@distinctUnionOfObjects:获取数组中每个对象的属性的值,放到一个数组中并返回,会对数组去重。

@unionOfObjects: 同@distinctUnionOfObjects,但是不去重。

@distinctUnionOfArrays: 获取数组中每个数组中的每个对象的属性的值,放到一个数组中并返回,会对数组去重复。

@unionOfArrays:同@distinctUnionOfArrays,但是不去重。

@distinctUnionOfSets: 获取集合中每个集合中的每个对象的属性的值,放到一个集合中并返回。


- (void)invalidateSessionCancelingTasks:(BOOL)cancelPendingTasks {

    if (cancelPendingTasks) {

        [self.session invalidateAndCancel];

    } else {

        [self.session finishTasksAndInvalidate];

    }

}


这里涉及到任务的两种关闭方法:

invalidateAndCancel直接关闭会话。

finishTasksAndInvalidate等当前任务完成之后关闭会话。


#pragma mark - NSURLSessionDelegate


//会话失效的回调方法

- (void)URLSession:(NSURLSession *)session didBecomeInvalidWithError:(NSError *)error


//https会话请求阶段需要验证服务器证书的时候调回,涉及到的https方面的知识后面再讲

- (void)URLSession:(NSURLSession *)session

didReceiveChallenge:(NSURLAuthenticationChallenge *)challenge

 completionHandler:(void (^)(NSURLSessionAuthChallengeDisposition disposition, NSURLCredential *credential))completionHandler

{

    //创建默认的处理方式,PerformDefaultHandling方式将忽略credential这个参数

    NSURLSessionAuthChallengeDisposition disposition = NSURLSessionAuthChallengePerformDefaultHandling;

    __block NSURLCredential *credential = nil;

    // 调动自身的处理方法,也就是说我们通过sessionDidReceiveAuthenticationChallenge这个block接收session,challenge 参数,返回一个NSURLSessionAuthChallengeDisposition结果,这个业务使我们自己在这个block中完成。

    if (self.sessionDidReceiveAuthenticationChallenge) {

        //调用自定义处理获取处理方式

        disposition = self.sessionDidReceiveAuthenticationChallenge(session, challenge, &credential);

    }

    //没有自定义处理方式

    else {

        //判断服务器证书的验证方法(NSURLAuthenticationMethodServerTrust:验证服务器证书)

        if ([challenge.protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodServerTrust]) {

            //使用安全策略验证

            if ([self.securityPolicy evaluateServerTrust:challenge.protectionSpace.serverTrust forDomain:challenge.protectionSpace.host]) {

                // 如果验证通过,根据serverTrust创建依据

                credential = [NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust];

                if (credential) {

                    disposition = NSURLSessionAuthChallengeUseCredential;

                } else {

                    disposition = NSURLSessionAuthChallengePerformDefaultHandling;

                }

            }

            // 验证没通过,返回CancelAuthenticationChallenge

            else {

                disposition = NSURLSessionAuthChallengeCancelAuthenticationChallenge;

            }

        } else {

            disposition = NSURLSessionAuthChallengePerformDefaultHandling;

        }

    }

    //调用验证结果的block

    if (completionHandler) {

        completionHandler(disposition, credential);

    }

}


#pragma mark - NSURLSessionTaskDelegate


//请求重定向的时候调用的回调方法

- (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task willPerformHTTPRedirection:(NSHTTPURLResponse *)response newRequest:(NSURLRequest *)request completionHandler:(void (^)(NSURLRequest *))completionHandler


//和NSURLSessionDelegate的验证证书方法差不多,不讲了

- (void)URLSession:(NSURLSession *)session

              task:(NSURLSessionTask *)task

didReceiveChallenge:(NSURLAuthenticationChallenge *)challenge

 completionHandler:(void (^)(NSURLSessionAuthChallengeDisposition disposition, NSURLCredential *credential))completionHandler


//请求需要一个全新的数据时调用。比如一个请求body发送失败时,可以通过这个方法给一个新的body

- (void)URLSession:(NSURLSession *)session

              task:(NSURLSessionTask *)task

 needNewBodyStream:(void (^)(NSInputStream *bodyStream))completionHandler


//上传数据时多次被调用,didSendBodyData本次发送的大小,totalBytesSent总共发送的大小,totalBytesExpectedToSend预计发送的总大小

- (void)URLSession:(NSURLSession *)session

              task:(NSURLSessionTask *)task

   didSendBodyData:(int64_t)bytesSent

    totalBytesSent:(int64_t)totalBytesSent

totalBytesExpectedToSend:(int64_t)totalBytesExpectedToSend


//任务完成时调用

- (void)URLSession:(NSURLSession *)session

              task:(NSURLSessionTask *)task

didCompleteWithError:(NSError *)error


#pragma mark - NSURLSessionDataDelegate


//收到服务器响应时调用

- (void)URLSession:(NSURLSession *)session

          dataTask:(NSURLSessionDataTask *)dataTask

didReceiveResponse:(NSURLResponse *)response

 completionHandler:(void (^)(NSURLSessionResponseDisposition disposition))completionHandler


//当dataTask变成downloadTask时调用

- (void)URLSession:(NSURLSession *)session

          dataTask:(NSURLSessionDataTask *)dataTask

didBecomeDownloadTask:(NSURLSessionDownloadTask *)downloadTask


//接收到数据时调用,会调用多次

- (void)URLSession:(NSURLSession *)session

          dataTask:(NSURLSessionDataTask *)dataTask

    didReceiveData:(NSData *)data


//即将缓存响应时调用

- (void)URLSession:(NSURLSession *)session

          dataTask:(NSURLSessionDataTask *)dataTask

 willCacheResponse:(NSCachedURLResponse *)proposedResponse

 completionHandler:(void (^)(NSCachedURLResponse *cachedResponse))completionHandler


//后台任务完成时调用

- (void)URLSessionDidFinishEventsForBackgroundURLSession:(NSURLSession *)session


#pragma mark - NSURLSessionDownloadDelegate


//下载完成后调用

- (void)URLSession:(NSURLSession *)session

      downloadTask:(NSURLSessionDownloadTask *)downloadTask

didFinishDownloadingToURL:(NSURL *)location


//接收到数据之后调用,bytesWritten本次接收数据大小,totalBytesWritten接收到的总数据大小,预计数据总大小

- (void)URLSession:(NSURLSession *)session

      downloadTask:(NSURLSessionDownloadTask *)downloadTask

      didWriteData:(int64_t)bytesWritten

 totalBytesWritten:(int64_t)totalBytesWritten

totalBytesExpectedToWrite:(int64_t)totalBytesExpectedToWrite


//同上,只不过是用的是fileOffset

- (void)URLSession:(NSURLSession *)session

      downloadTask:(NSURLSessionDownloadTask *)downloadTask

 didResumeAtOffset:(int64_t)fileOffset

expectedTotalBytes:(int64_t)expectedTotalBytes

 



  • 作者:随风流逝

  • 链接:http://www.jianshu.com/p/56f0b3b936e7

  • 來源:简书

  • iOS开发整理发布,转载请联系作者授权

【点击成为安卓大神】

关注公众号:拾黑(shiheibook)了解更多

[广告]赞助链接:

四季很好,只要有你,文娱排行榜:https://www.yaopaiming.com/
让资讯触达的更精准有趣:https://www.0xu.cn/

公众号 关注网络尖刀微信公众号
随时掌握互联网精彩
赞助链接