读源码-----AFNetWorking3.1(一)

2018-06-20  本文已影响30人  小芳姑娘2012

AFNetworking是对NSURLSession的封装,看AFNetworking之前必须先了解NSURLSession的网络请求原理步骤。
1.创建NSURLSessionDataTask
2.配置NSURLSessionDataTask
3.设置NSURLSessionDataTask的Delegate
4.调用NSURLSessionDataTask的resume方法开始请求
5.在Delegate的方法里面处理网络请求的各个过程
6.清理NSURLSessionDataTask的配置
虽然代码很多,但是一点一点慢慢看。example的代码很规范,mvc架构,从业务层开始了解底层。


image.png

AppDelegate

didFinishLaunchingWithOptions函数中:
1 设置缓存机制

NSURLCache *URLCache = [[NSURLCache alloc] initWithMemoryCapacity:4 * 1024 * 1024 diskCapacity:20 * 1024 * 1024 diskPath:nil];
[NSURLCache setSharedURLCache:URLCache];

2 设置网络指示的小菊花
[[AFNetworkActivityIndicatorManager sharedManager] setEnabled:YES];
点击进入AFNetworkActivityIndicatorManager类文件,有注释,翻译如下:
AFNetworkActivityIndicatorManager管理状态栏中的网络活动指示器的状态。当启用时,它将侦听表明task已经启动或完成的通知,并相应地启动或停止动画指示器。活动请求的数量增加和递减,就像堆栈或信号量一样,活动指示符在这个数字大于零的情况下也会被激活。
当应用程序完成启动时,应该启用AFNetworkActivityIndicatorManager的共享实例。在AppDelegate application:didFinishLaunchingWithOptions:你可以使用以下代码:

[[AFNetworkActivityIndicatorManager sharedManager]setEnabled:YES);

通过为sharedManager设置enabledYES,网络活动指示器将在请求开始和结束时自动显示和隐藏。您不需要自己调用incrementActivityCountdecrementActivityCount
详细内容以后阅读。
3 设置rootViewController为GlobaltimelineViewController的NavigationController

AFNetworkActivityIndicatorManager

在init方法中self.currentState表示当前状态初始化是没有活动状态,是一个枚举值,有4个状态值, 2个定时器self.activationDelay self.completionDelay。注册了3个通知来接收网络状态变化。

- (void)networkRequestDidStart:(NSNotification *)notification {
    if ([AFNetworkRequestFromNotification(notification) URL]) {
        [self incrementActivityCount];
    }
}
- (void)updateCurrentStateForNetworkActivityChange {
    if (self.enabled) {
        switch (self.currentState) {
            case AFNetworkActivityManagerStateNotActive:
                if (self.isNetworkActivityOccurring) {
                    [self setCurrentState:AFNetworkActivityManagerStateDelayingStart];
                }
                break;
            case AFNetworkActivityManagerStateDelayingStart:
                //No op. Let the delay timer finish out.
                break;
            case AFNetworkActivityManagerStateActive:
                if (!self.isNetworkActivityOccurring) {
                    [self setCurrentState:AFNetworkActivityManagerStateDelayingEnd];
                }
                break;
            case AFNetworkActivityManagerStateDelayingEnd:
                if (self.isNetworkActivityOccurring) {
                    [self setCurrentState:AFNetworkActivityManagerStateActive];
                }
                break;
        }
    }
}

更新self.curruentState的状态,一开始默认是不活跃状态,进入
AFNetworkActivityManagerStateNotActive情况,设置网络状态DelayingStart。

if (self.isNetworkActivityOccurring) {
                    [self setCurrentState:AFNetworkActivityManagerStateDelayingStart];
                }

点击进入setCurrentState:根据self.currentState状态不同做出不同反应,没有网络请求时取消两个定时器,隐藏小菊花。

- (void)setCurrentState:(AFNetworkActivityManagerState)currentState {
    @synchronized(self) {
        if (_currentState != currentState) {
            [self willChangeValueForKey:@"currentState"];
            _currentState = currentState;
            switch (currentState) {
                case AFNetworkActivityManagerStateNotActive:
                    [self cancelActivationDelayTimer];
                    [self cancelCompletionDelayTimer];
                    [self setNetworkActivityIndicatorVisible:NO];
                    break;
                case AFNetworkActivityManagerStateDelayingStart:
                    [self startActivationDelayTimer];
                    break;
                case AFNetworkActivityManagerStateActive:
                    [self cancelCompletionDelayTimer];
                    [self setNetworkActivityIndicatorVisible:YES];
                    break;
                case AFNetworkActivityManagerStateDelayingEnd:
                    [self startCompletionDelayTimer];
                    break;
            }
            [self didChangeValueForKey:@"currentState"];
        }
        
    }
}

DelayingStart状态时添加定时器,默认1秒后执行再setCurrentState:方法 currentState改为AFNetworkActivityManagerStateActive,活跃状态时显示小菊花,几个状态之间的相互转化很巧妙。
这是设置小菊花的方法,并且支持自定义

- (void)setNetworkActivityIndicatorVisible:(BOOL)networkActivityIndicatorVisible {
    if (_networkActivityIndicatorVisible != networkActivityIndicatorVisible) {
        [self willChangeValueForKey:@"networkActivityIndicatorVisible"];
        @synchronized(self) {
             _networkActivityIndicatorVisible = networkActivityIndicatorVisible;
        }
        [self didChangeValueForKey:@"networkActivityIndicatorVisible"];
        if (self.networkActivityActionBlock) {
            self.networkActivityActionBlock(networkActivityIndicatorVisible);
        } else {
            [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:networkActivityIndicatorVisible];
        }
    }
}

复习到的知识点:通知,定时器,手动kvo

GlobalTimelineViewController

GlobalTimelineViewController是继承uitableViewController
viewDidLoad函数中设置了tableView的上拉下拉刷新控件refreshControl,refreshControl是UITableViewController的属性,是系统封装好的刷新控件,添加事件reload,在reload函数中添加网络请求。
UITableViewDataSource代理方法绑定数据源设置cell。数据源self.posts是在reload函数中的block中得到的

NSURLSessionTask *task = [Post globalTimelinePostsWithBlock:^(NSArray *posts, NSError *error) {
        if (!error) {
            self.posts = posts;
            [self.tableView reloadData];
        }
    }];
[self.refreshControl setRefreshingWithStateOfTask:task];

Model Post类

Post类调用globalTimelinePostsWithBlock创建一个网络请求任务,并在返回成功的回调block中对进入数据转模型。example将网络请求方法放在model类中,在view controller中Post调用类方法,block回调的直接就是模型数据了。

+ (NSURLSessionDataTask *)globalTimelinePostsWithBlock:(void (^)(NSArray *posts, NSError *error))block {
    return [[AFAppDotNetAPIClient sharedClient] GET:@"stream/0/posts/stream/global" parameters:nil progress:nil success:^(NSURLSessionDataTask * __unused task, id JSON) {
        NSArray *postsFromResponse = [JSON valueForKeyPath:@"data"];
        NSMutableArray *mutablePosts = [NSMutableArray arrayWithCapacity:[postsFromResponse count]];
        for (NSDictionary *attributes in postsFromResponse) {
            Post *post = [[Post alloc] initWithAttributes:attributes];
            [mutablePosts addObject:post];
        }

        if (block) {
            block([NSArray arrayWithArray:mutablePosts], nil);
        }
    } failure:^(NSURLSessionDataTask *__unused task, NSError *error) {
        if (block) {
            block([NSArray array], error);
        }
    }];
}

在返回成功的回调中
AFAppDotNetAPIClient是一个继承AFHTTPSessionManager的单例类,是对AFHTTPSessionManager的又一层简单封装

+ (instancetype)sharedClient {
    static AFAppDotNetAPIClient *_sharedClient = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        _sharedClient = [[AFAppDotNetAPIClient alloc] initWithBaseURL:[NSURL URLWithString:AFAppDotNetAPIBaseURLString]];
        _sharedClient.securityPolicy = [AFSecurityPolicy policyWithPinningMode:AFSSLPinningModeNone];
    });
    
    return _sharedClient;
}

AFAppDotNetAPIClient发起一个GET请求。点击进入其父类AFHTTPSessionManager的GET请求方法:

- (NSURLSessionDataTask *)GET:(NSString *)URLString
                   parameters:(id)parameters
                     progress:(void (^)(NSProgress * _Nonnull))downloadProgress
                      success:(void (^)(NSURLSessionDataTask * _Nonnull, id _Nullable))success
                      failure:(void (^)(NSURLSessionDataTask * _Nullable, NSError * _Nonnull))failure
{

    NSURLSessionDataTask *dataTask = [self dataTaskWithHTTPMethod:@"GET"
                                                        URLString:URLString
                                                       parameters:parameters
                                                   uploadProgress:nil
                                                 downloadProgress:downloadProgress
                                                          success:success
                                                          failure:failure];

    [dataTask resume];

    return dataTask;
}

UIRefreshControl+AFNetworking

这是一个UIRefreshControl的category,给UIRefreshControl添加了一个方法- (void)setRefreshingWithStateOfTask:(NSURLSessionTask *)task;。点击进入方法实现。

- (void)setRefreshingWithStateOfTask:(NSURLSessionTask *)task {
    [[self af_notificationObserver] setRefreshingWithStateOfTask:task];
}

因为category不能添加实例变量,作者使用的objc_getAssociatedObject和objc_setAssociatedObject方法来绑定一个实例变量af_notificationObserver。
AFRefreshControlNotificationObserver类来实现setRefreshingWithStateOfTask方法。添加通知前先移除之前的通知,然后再添加3个通知。

上一篇下一篇

猜你喜欢

热点阅读