AFNetworking究竟是如何运作的?

时间:2013-08-26 07:59:55

标签: ios multithreading afnetworking

大家好,我和ASIHttpRequest,AFNetworking玩了一段时间,今天,我尝试更深入地阅读AFNetworking的源代码,它真的是一个功能强大且设计良好的网络框架,现在我有几个要问的问题: 1,AFJSONRequestOperation

NSMutableURLRequest *request = [httpClient requestWithMethod:@"POST" path:kMobileBind parameters:paramsBase];

AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {  //successblock   } fail:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON){  //failblock   }

在此之后,创建操作并将成功块和失败块设置为它,然后我们开始操作,它在单个线程中启动NSURLconnection

[self performSelector:@selector(operationDidStart) onThread:[[self class] networkRequestThread] withObject:nil waitUntilDone:NO modes:[self.runLoopModes allObjects]];

inside operationDidStart:

 [self.lock lock];
 NSRunLoop *runLoop = [NSRunLoop currentRunLoop];
    for (NSString *runLoopMode in self.runLoopModes) {
        [self.connection scheduleInRunLoop:runLoop forMode:runLoopMode];
        [self.outputStream scheduleInRunLoop:runLoop forMode:runLoopMode];
    }
[self.connection start];
[self.lock unlock];

此操作运行并调用NSURLconnection回调,

 - (void)connectionDidFinishLoading:(NSURLConnection *)__unused connection {
self.responseData = [self.outputStream propertyForKey:NSStreamDataWrittenToMemoryStreamKey];
[self.outputStream close];
[self finish];
self.connection = nil;
}

- (void)finish {
    self.state = AFHTTPOperationFinishedState;
}

finish方法设置操作状态,并使用KVO技术完成操作: 我没有看到observeValueForKeyPath和[obj addObserver:self forKeyPath:@“isFinished”options:0 context:NULL]; KVO如何运作?

的setState:

    [self willChangeValueForKey:newStateKey];
    [self willChangeValueForKey:oldStateKey];
    _state = state;
    [self didChangeValueForKey:oldStateKey];
    [self didChangeValueForKey:newStateKey];



- (BOOL)isFinished {
    return self.state == AFHTTPOperationFinishedState;
}

然后我被困在这里,我找到了setcallblock基金,但是不能理解self.completionBlock是如何被调用的:

- (void)setCompletionBlockWithSuccess:(void (^)(AFHTTPRequestOperation *operation, id responseObject))success

   failure:(void (^)(AFHTTPRequestOperation *operation, NSError *error))failure

{
    self.completionBlock = ^ {
    if ([self isCancelled]) {
        return;
    }

    if (self.error) {
        if (failure) {
            dispatch_async(self.failureCallbackQueue ? self.failureCallbackQueue : dispatch_get_main_queue(), ^{
                failure(self, self.error);
            });
        }
    } else {
        if (success) {
            dispatch_async(self.successCallbackQueue ? self.successCallbackQueue : dispatch_get_main_queue(), ^{
                success(self, self.responseData);
            });
        }
    }
};

}

可能是我确实仔细阅读了代码,但这确实让我感到困惑,任何有AFNetworking经验的人都会解释这个框架,

还有一个问题,网络请求是在共享线程上运行的,它会减慢请求吗?

我想提高我的mutilthread编程技巧,我需要样本和建议,我已经阅读了苹果的文档,并做了一些简单的演示,没有尝试过一个大项目或编写一个健壮的框架或模型,如何我开始吗?非常感谢

1 个答案:

答案 0 :(得分:1)

completionBlockAFJSONRequestOperation超类NSOperation的属性。当操作完成时执行。