我正在进行afnetworking,我的Web服务花费了太多时间来加载数据,我希望UI不会冻结,我使用这个tutorial在后台运行webservice,这样我就可以在其他地方工作了观点也是如此,但直到现在还没有成功。
-(void) getArticles :(NSString*)stateAbbre completionHandler:(void (^)(id array))success
{
[MyCommonFunctions showGlobalProgressHUDWithTitle:@"Loading"];
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
NSString *link = [NSString stringWithFormat:@"http://cloud.ilmasoft.com/depilex/depilexs/get_articles_ios.php"];
NSLog(@"%@",link);
manager.responseSerializer.acceptableContentTypes = [manager.responseSerializer.acceptableContentTypes setByAddingObject:@"text/html"];
[manager GET:link parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"JSON: %@", responseObject);
NSMutableArray *dataArray = [[NSMutableArray alloc] init];
NSDictionary *returnedDealDict = responseObject ;
NSArray *returnArray = [returnedDealDict objectForKey:@"Result"];
for(NSDictionary *dealDict in returnArray)
{
ArticlesDC *articles = [[ArticlesDC alloc] init];
articles.articlesID = [[dealDict objectForKey:@"id"]intValue ];
articles.articleTitle = [dealDict objectForKey:@"title" ];
articles.articleDetail = [dealDict objectForKey:@"details" ];
articles.articleDate = [dealDict objectForKey:@"date" ];
articles.articlePic = [dealDict objectForKey:@"pic" ];
articles.articleThumbPath = [dealDict objectForKey:@"thumb_path" ];
articles.articleStatus = [dealDict objectForKey:@"status" ];
[dataArray addObject:articles];
[MyCommonFunctions dismissGlobalHUD];
}
success(dataArray);
// [MBProgressHUD hideHUDForView:self.view animated:YES];
if (dataArray.count == 0)
{
ALERT_VIEW(@"Please check your internet connection.");
// [MBProgressHUD hideHUDForView:self.view animated:YES];
}
}
failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"Error: %@", error);
ALERT_VIEW(@"Error occured while loading data.");
// [MBProgressHUD hideHUDForView:self.view animated:YES];
}];
}
在我的视图中确实加载了方法
[self getArticles:nil completionHandler:^(id array) {
articlesArray = array;
[tblView reloadData];
for (ArticlesDC *article in articlesArray)
{
NSString *stringWithoutSpace = [[NSString stringWithFormat:@"http://cloud.ilmasoft.com/depilex/admin/%@", article.articleThumbPath] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSData *imageData = [NSData dataWithContentsOfURL:[NSURL URLWithString: stringWithoutSpace]];
UIImage *imgOne = [UIImage imageWithData:imageData];
NSString *stringforImg = [[NSString stringWithFormat:@"http://cloud.ilmasoft.com/depilex/admin/%@", article.articlePic] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSData *imageDta = [NSData dataWithContentsOfURL:[NSURL URLWithString: stringforImg]];
UIImage *imgTwo = [UIImage imageWithData:imageDta];
[dbHandler insertArticlesIntoSqlite:article.articleTitle andDetail:article.articleDetail anddate:article.articleDate andImage:[MyCommonFunctions saveImageInDocuments:imgTwo] andThumb:[MyCommonFunctions saveImageInDocuments:imgOne]];
[defaults setInteger:1 forKey:@"getArticlesOffline"];
[defaults synchronize];
}
}];
答案 0 :(得分:1)
问题不在于AF,而是在该过程结束时您调用dataWithContentsOfURL
两次,这直接在主线程上运行以下载一些图像。您需要将该下载移动到后台线程。