Setbackground load with do while loop

I am working on an application where I am checking the url using the following code.

- (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];
}

This is what I do when I get an answer.

- (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]);
    }
}

This is my upload data code

- (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, .

+3
2

UIImageView , .

SDWebImage

completionBlock, progressBlock , . , , , .

:

, , , 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 ...

           }];
0

NSURLConnection . NSURLConnection . , . . :

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

:

if (temp)
    {
        [self backgroundProcess];
    }

.

+2

All Articles