Nanoc
Nanoc

Reputation: 2381

Run code on background and get return code

i have this method

    - (BOOL)connectedToInternet
{
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:
                                    [NSURL URLWithString:@"http://www.google.com/"]];

    [request setHTTPMethod:@"HEAD"];

    NSHTTPURLResponse *response;

    [NSURLConnection sendSynchronousRequest:request
                          returningResponse:&response error: NULL];

    return ([response statusCode] == 200) ? YES : NO;
}

that method is taking a few seconds to do it, im using it in a simple if conditional to know if i have internet connection.

is there any way to do it in a background thread without having to change all code.

I'm calling it this way

if([self connectedToInternet])

So if i do it in a background thread i cant get the return value and then my method cant return the value.

If i have to change all it doesn't worth it.

I hope u can understand my question and thanks for any help.

Upvotes: 0

Views: 157

Answers (4)

stosha
stosha

Reputation: 2148

In Apple's "Reachability" Code Sample note the reachabilityWithAddress: method please.

Upvotes: 2

Bartu
Bartu

Reputation: 2210

You can do something similar to this using blocks;

definition (.h)

+ (void)isConnectedToInternet:(void (^)(BOOL connected))block;

implementation (.m)

+ (void)isConnectedToInternet:(void (^)(BOOL))block
{
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:
                                    [NSURL URLWithString:@"http://www.google.com/"]];

    [request setHTTPMethod:@"HEAD"];

    [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {
        NSHTTPURLResponse* httpResponse = (NSHTTPURLResponse*)response;

        if (block) {
            block( ([httpResponse statusCode] == 200) ? YES : NO);
        }
    }];
}

then call it like

[MyClass isConnectedToInternet:^(BOOL connected) {
        if (connected) {
            // do stuff;
        }
    }];

Upvotes: 0

Devarshi
Devarshi

Reputation: 16758

I would suggest the method which you are implementing to know 'if Internet is connected or not' is not the most optimized one... few days back I also tried to implement the same thing.. and I came across couple of solutions, over Internet.. and I wrote about it on my blog.. Checking Internet connection in cocoa.

My preferred way to know if network is connected or not is by using Reachability class. You can get clue on using it from this code: NetworkCheckUtility.

Hope this helps :-)

Upvotes: 0

zbMax
zbMax

Reputation: 2818

I don't know what exactly what you want to do, but what you want to use is probably :

dispatch_queue_t queue = dispatch_get_main_queue();
dispatch_async(queue, ^{
    //your asynchronous code here
});

But by using an if condition, you need the result in order to continue, don't you? So why running the code in background?

Upvotes: 0

Related Questions