【发布时间】:2014-06-30 10:40:31
【问题描述】:
我的应用程序有一个登录页面,人们需要输入密码才能登录。在我将其提交给 iTuneConnect 之前,我的应用程序的所有内容都可以在我的几部 iPhone 上运行,然后它已获得 Apple 的批准。当我从应用商店下载回来时发生了一些事情。 HUD的状态一直在加载中......无法登录进去。
这是我的简单代码:
@synthesize threadCount = _threadCount;
- (void) initData
{
_codeIsVaild = NO;
}
- (IBAction)Login:(id)sender
{
_threadCount = 0;
//Login procedure
MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
hud.mode = MBProgressHUDModeIndeterminate;
hud.labelText = @"Loading...";
[hud showAnimated:YES whileExecutingBlock:^{
[self getData];
while (_threadCount != 5) {
//Waiting for all tasks complete...It will be pass when the threads counter is equal 5
}
} completionBlock:^{
if (_codeIsVaild)
{
//Password is Vaild
[self performSegueWithIdentifier:@"loginsegue" sender:self];
}
else
{
//Show error Alert
}
[hud removeFromSuperview];
}];
}
}
-(void) getData
{
dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
NSString *URLString = [APILink getData];
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
manager.responseSerializer = [AFHTTPResponseSerializer serializer];
manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/html"];
[manager POST:URLString parameters:paras
success:^(AFHTTPRequestOperation *operation, id responseObject)
{
//Tasks
[self taskone];
[self tasktwo];
[self taskthree];
[self taskfour];
[self taskfive];
_codeIsVaild = YES;
}
else
{
_codeIsVaild = NO;
}
dispatch_semaphore_signal(semaphore);
}
failure:^(AFHTTPRequestOperation *operation, NSError *error)
{
_codeIsVaild = NO;
dispatch_semaphore_signal(semaphore);
}];
dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
}
-(void) taskone
{
NSString *URLString = [APILink taskoneAPIurl];
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/html"];
[manager POST:URLString parameters:paras
success:^(AFHTTPRequestOperation *operation, id responseObject) {
_threadCount += 1;
}
failure:^(AFHTTPRequestOperation *operation, NSError *error) {
_threadCount += 1;
}];
}
//Same as taskone,but get data from another url
-(void) taskTwo
-(void) taskThree
-(void) taskFour
-(void) taskFive
我认为问题在于异步任务管理。如果是这样的话,我的所有手机应该都无法登录。最奇怪的是,在我将它上传到应用商店之前它可以在我的手机上正常工作,但是当我从应用商店下载它时它就不能正常工作.我能做些什么?谢谢大家。
【问题讨论】:
-
终于找到了解决办法,参考网址:How to batch request with AFNetworking 2?这表明代码不能等待几个请求的结果,使用一个死循环来等待。您应该使用 dispatch_group_t 和 dispatch_group_notify 谢谢 CirrusFlyer 和大家!祝你有美好的一天!
标签: ios objective-c asynchronous app-store grand-central-dispatch