gpt4 book ai didi

iphone - NSOperationQueue 和 ASIHTTPRequest

转载 作者:行者123 更新时间:2023-12-03 21:22:39 28 4
gpt4 key购买 nike

我正在为围绕 ASIHTTPRequest 编写的包装类编写测试用例。由于我无法确定的原因,我的测试用例在 ASIHTTPRequest 完成之前就失败了。

以下是程序流程的工作原理。

  1. 从我的测试用例开始。
  2. 初始化我的 http 引擎对象,指示它创建一个新列表
  3. 创建新的 ASIHTTPRequest 对象并进行设置。
  4. 将请求添加到操作队列。
  5. 等到队列为空
  6. 检查我的委托(delegate)方法是否被调用,如果没有,则测试失败。

现在,大多数情况下一切正常并且测试通过,但有时会失败,因为在操作队列将控制权返回给我的等待方法之后调用了我的委托(delegate)方法。

测试用例

// Set my flags to 'NO'
- (void)setUp {
requestDidFinish = NO;
requestDidFail = NO;
}

- (void)testCreateList {
NSString *testList = @"{\"title\": \"This is a list\"}";

JKEngine *engine = [[JKEngine alloc] initWithDelegate:self];
NSString *requestIdentifier = [engine createList:jsonString];

[self waitUntilEngineDone:engine];
NSString *responseString = responseString_;
[engine release];

GHAssertNotNil(requestIdentifier, nil);
GHAssertTrue(requestDidFinish, nil);
GHAssertTrue([responseString hasPrefix:@"{\"CreateOrEditListResult\""], nil);

}

// Puts the test into a holding pattern until the http request is done
- (void)waitUntilEngineDone:(JKEngine *)engine {
[engine waitUntilFinishedRunning];
}

// The delegate method called on successful completion
- (void)requestFinished:(NSString *)requestIdentifier withResponse:(NSString *)response {
NSLog(@"request did finish");
requestDidFinish = YES;
responseIdentifier_ = [requestIdentifier retain];
responseString_ = [response retain];
}

引擎代码

- (NSString *)createList:(NSString *)list {
ASIHTTPRequest *request = [[ASIHTTPRequest alloc] initWithURL:[NSURL URLWithString:url]];
[request addRequestHeader:@"Content-Type" value:kContentType];
[request setRequestMethod:kPOST];
request.delegate = self;

[request appendPostData:[list dataUsingEncoding:NSUTF8StringEncoding]];

NSString *requestIdentifier = [NSString stringWithNewUUID];

[operationQueue_ addOperation:request];
[operationDictionary_ setObject:request forKey:requestIdentifier];

return requestIdentifier;
}

// This is the ASIHTTPRequest delegate method that's called on success
// but it sometimes isn't called until AFTER the operationQueue finishes running
- (void)requestFinished:(ASIHTTPRequest *)request {
DLog([request responseString]);

BOOL canNotifiyDelegate = [self.delegate respondsToSelector:@selector(requestFinished:withResponse:)];
if (canNotifiyDelegate) {
NSArray *keyArray = [operationDictionary_ allKeysForObject:request];
NSString *requestIdentifier = [keyArray objectAtIndex:0];
[operationDictionary_ removeObjectForKey:requestIdentifier];

if ([keyArray count] != 1) {
ALog(@"It looks like a request was added to the operation dictionary multiple times. There's a bug somewhere.", nil);
}

[self.delegate requestFinished:requestIdentifier withResponse:[request responseString]];
}
}

- (void)waitUntilFinishedRunning {
[operationQueue_ waitUntilAllOperationsAreFinished];
}

最佳答案

这就是 ASIHTTPRequest 的工作方式。委托(delegate)方法在主线程上调用,并且对委托(delegate)的调用不会阻塞请求线程,因此完全有可能在队列完成后调用您的委托(delegate)。

关于iphone - NSOperationQueue 和 ASIHTTPRequest,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3327079/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com