大家好,我玩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]];


内部操作DidStart:

 [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”选项: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 pls经验的人都可以解释这个框架,

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

我确实想提高我在mutilthread编程方面的技能,并且我需要示例和建议,我已经阅读了Apple的文档,并做了一些简单的演示,还没有尝试过大型项目或没有编写健壮的框架或模型,我如何开始?非常感谢

最佳答案

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

关于ios - AFNetworking如何工作?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/18439356/

10-13 07:57