用while循环加载setbackground
我正在使用以下代码检查网址的应用程序。
-(void)exists
{
NSString *strImg = @"some url";
NSURL *aImgUrl = [NSURL URLWithString:strImg];
NSMutableURLRequest *imgRequest = [NSMutableURLRequest requestWithURL:strImg];
[imgRequest setHTTPMethod:@"HEAD"];
imgConnection = [NSURLConnection connectionWithRequest:imgRequest delegate:self];
}
当我得到回应时,这就是我所做的。
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
if ([(NSHTTPURLResponse *)response statusCode] == 200)
{
// url exists
appDel.isExists = TRUE;
temp = FALSE;
[self loadData];
NSLog(@"%ld",(long)[(NSHTTPURLResponse *)response statusCode]);
}
else if([(NSHTTPURLResponse*)response statusCode] == 404)
{
//url does not exists
appDel.isExists = FALSE;
temp = TRUE;
[self loadData];
NSLog(@"%ld",(long)[(NSHTTPURLResponse *)response statusCode]);
}
}
这是我的加载数据代码
-(void)loadData
{
result = FALSE;
isReqSent = FALSE;
do
{
if (appDel.isExists == TRUE)
{
NSURL *aTxtUrl = [NSURL URLWithString:apiText];
NSURL *aImgUrl = [NSURL URLWithString:apiImage];
NSURLRequest *imgRequest = [NSURLRequest requestWithURL:aImgUrl];
NSURLRequest *txtRequest = [NSURLRequest requestWithURL:aTxtUrl];
[NSURLConnection sendAsynchronousRequest:txtRequest queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError)
{
if (data)
{
NSString *strTxt = [[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];
[lblTime setText:strTxt];
[policyImgWebView loadRequest:imgRequest];
result = TRUE;
}
}];
}
if (result)
{
appDel.isExists = TRUE;
}
else
{
if(!isReqSent)
{
NSString *soapMessage = @"my soap message";
NSString *soapAction = @"my soap url";
[objWebServiceParser xmlParsing:soapMessage :soapAction :Number];
isReqSent = TRUE;
}
}
if (temp)
{
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^{
[self backgroundProcess];
});
}
} while (result == FALSE);
这是我的后台进程
-(void)backgroundProcess
{
NSString *strImg = @"some url";
NSURL *aImgUrl = [NSURL URLWithString:apiImage];
NSMutableURLRequest *imgRequest = [NSMutableURLRequest requestWithURL:aImgUrl];
[imgRequest setHTTPMethod:@"HEAD"];
imgConnection = [NSURLConnection connectionWithRequest:imgRequest delegate:self];
}
我不知道我在做错什么,可以有人指导我吗?
我想要后台进程保持运行,直到它获取数据,当数据收到appdel.isExists变为真并获取主线程来更新ui。
我试过GCD& performSelectorInTheBackground但我没有得到它的权利,我得到NSThread失败错误35。
它与NSURLSessionDownloadTask一起工作,但仍在寻找更好的选择。
要从服务器加载UIImageView的图像,可以使用下面的框架。
SDWebImage
您可以获得completionBlock , progressBlock更多可用于更新UI的属性。 它会以块形式通知您,然后您可以在UI上更新任何您想要的内容。
样品:
这与代码一样简单,只要imageURL有效就可以加载图像(这是您的情况)
UIImageView *imageView;
[imageView setImageWithURL:[NSURL URLWithString:YOUR_URL]];
它也有如下丰富的方法
UIImageView *imageView;
[imageView setImageWithURL:[NSURL URLWithString:YOUR_URL]
placeholderImage:[UIImage imageNamed:@"placeholder.png"]
completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType)
{
// in "image" you'll get the downloaded image from the URL
... completion code here ...
}];
不要在后台调用NSURLConnection 。 由于NSURLConnection将自动工作。 由于您没有发送异步请求,它将在后台运行并且不会挂起主线程。 在您的负载数据中删除调度。 你有:
if (temp)
{
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^{
[self backgroundProcess];
});
}
做了:
if (temp)
{
[self backgroundProcess];
}
你不需要使用派遣。
链接地址: http://www.djcxy.com/p/68089.html