这可能与我对块的理解有关,也可能与 NSOperationQueue 如何/何时触发它的操作有关,但无论如何我都会问:
我有一个基本的获取请求方法:
- (void)fetchActiveUser:(NSString*)username withPassword:(NSString *)password {
[self setAuthorizationHeaderWithUsername:username password:password];
// SETS FLAG that we are performing a request
fetchModeActive = TRUE;
[self getPath:kTLActiveUserURI parameters:nil success:^(AFHTTPRequestOperation
*operation, id responseObject) {
if ([responseObject isKindOfClass:[NSXMLParser class]]) {
TLPersonParser *personParser = [[TLPersonParser alloc]
initWithParser:responseObject];
[personParser setDelegate: self];
[personParser parsePeople];
[personParser release];
}
// handle stuff here.
NSLog(@"Success!");
fetchModeActive = FALSE;
[[NSNotificationCenter defaultCenter]
postNotificationName:kTLFetchedActiveUserNotification object:nil];
}
failure:^(__unused AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"Failure.");
fetchModeActive = FALSE;
[[NSNotificationCenter defaultCenter]
postNotificationName:kTLFetchedActiveUserNotification object:error];
}];
while(self.fetchModeActive) {
// WHY DOES THIS RUN INFINITELY?
// Both above Success: & Failure: blocks set fetchModeActive = FALSE
// when complete
NSLog(@"fetching....");
}
}
滚动到最底部,您将看到我的等待循环。为什么这个运行无限输出“获取......”?success:和failure:块都将我们的 fetchModeActive 标志设置回 false ?
我的理解是,这个 fetch 操作在后台异步运行,但看起来甚至没有执行 fetch!
有什么建议么?我需要在单独的线程中触发获取请求吗?
谢谢!