setbackground加载与while循环

时间:2014-02-25 13:29:34

标签: ios objective-c nsxmlparser nsthread nsurlconnectiondelegate

我正在使用以下代码检查网址的应用程序。

-(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变为true&获取主线程来更新ui。

我试过GCD& performSelectorInTheBackground但我没有把它做对我得到NSThread失败错误35。


它与NSURLSessionDownloadTask一起工作,但仍在寻找更好的选择。

2 个答案:

答案 0 :(得分:2)

请勿在后台调用NSURLConnection。由于NSURLConnection会自动生效。由于您没有发送异步请求,因此它将在后台运行,并且不会挂起您的主线程。在您的加载数据中删除调度。你有:

if (temp)
    {
        dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^{
            [self backgroundProcess];
        });
    }

制作:

if (temp)
    {
        [self backgroundProcess];
    }

您无需使用发送。

答案 1 :(得分:0)

要从服务器加载UIImageView中的图像,可以使用以下框架。

<强> SDWebImage

你得到completionBlockprogressBlock&amp;您可以使用更多属性来更新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 ...

           }];