Ios 子类化AFHTTPRequestOperation
我正在使用Ios 子类化AFHTTPRequestOperation,ios,objective-c,afnetworking,hierarchy,afnetworking-2,Ios,Objective C,Afnetworking,Hierarchy,Afnetworking 2,我正在使用AFNetworking 2.0库作为基础的网络层上工作。我计划为每个请求创建单独的类,并使用AFHTTPRequestOperation作为基类。 所以我有点像: ABCLoginOperation : AFHTTPRequestOperation 每个自定义包装器类都有一些附加属性,便于使用请求/响应参数。 因此,在我的案例中,工作流如下所示: 创建ABCLoginOperation类实例 通过设置其附加属性来配置ABCLoginOperation 运行ABCLoginOpera
AFNetworking 2.0
库作为基础的网络层上工作。我计划为每个请求创建单独的类,并使用AFHTTPRequestOperation
作为基类。
所以我有点像:
ABCLoginOperation : AFHTTPRequestOperation
每个自定义包装器类都有一些附加属性,便于使用请求/响应参数。
因此,在我的案例中,工作流如下所示:
ABCLoginOperation
类实例李>
ABCLoginOperation
李>
ABCLoginOperation
,将其添加到任何NSOperationQueue
ABCLoginOperation *loginOperation = [[ABCLoginOperation alloc] init];
ABCLoginRequestModel *requestModel = operation.requestModel;
requestModel.username = @"username";
requestModel.password = @"password";
[ABCOperationManager enqueueOperation:operation];
- (void)enqueueOperation:(ABCOperation *)operation {
[self configureOperation:operation];
[self.operationQueue addOperation:operation]; //or use AFHTTPOperationManager's operation queue
}
- (void)configureOperation:(ABCOperation *)operation {
NSMutableURLRequest *mutableURLRequest = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:[operation getRequestPath]] cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:operation.timeoutInterval];
//some further configuration of mutableUrlRequest
operation.request = mutableURLRequest //the problem
}
问题是,AFHTTPRequestOperation
不知道我用来描述请求参数的其他属性,我必须在步骤2和步骤3之间解决这个问题因此,我实现了一个定制的请求管理器
abcopeOptionManager
,它应该基于ABCLoginOperation的
属性设置AFHTTPRequest的
属性。我认为可以创建NSURLRequestObject
,根据ABCLoginOperation
对象的属性设置其参数,并将该NSURLRequestObject
设置为AFHTTPRequestOperation
/AFURLConnectionOperation
在其实现中使用的request
属性。因此,abCopeOptionManager的
排队操作:
方法的实现如下:
ABCLoginOperation *loginOperation = [[ABCLoginOperation alloc] init];
ABCLoginRequestModel *requestModel = operation.requestModel;
requestModel.username = @"username";
requestModel.password = @"password";
[ABCOperationManager enqueueOperation:operation];
- (void)enqueueOperation:(ABCOperation *)operation {
[self configureOperation:operation];
[self.operationQueue addOperation:operation]; //or use AFHTTPOperationManager's operation queue
}
- (void)configureOperation:(ABCOperation *)operation {
NSMutableURLRequest *mutableURLRequest = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:[operation getRequestPath]] cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:operation.timeoutInterval];
//some further configuration of mutableUrlRequest
operation.request = mutableURLRequest //the problem
}
但问题是AFHTTPRequestOperation
的request
属性是只读的,只能在构造函数中设置。因此,似乎没有办法预先配置我的ABCOperation
,因为创建它的唯一方法是使用-initWithRequest:
构造函数。这使得AFHTTPRequestOperation
子类化在我看来毫无用处。请您解释一下我做错了什么,以及在AFNetworking 2.0
周围创建自定义包装层的正确方法是什么提前谢谢 请求操作是用请求初始化的,之后是不可变的。您可以在子类中将
request
属性重写为readwrite
,但我不建议这样做。