0

anyone know how I can handle error code when there is an error on the following:

database_flag = [NSString stringWithContentsOfURL:database_flag_query encoding:NSASCIIStringEncoding error:&error];

TO explain more please find below my code Basically I want to check mysql for a flag if the flag is 1 then i get the ip address of the stream from the databse else i use the local one store. the only issue is when there is not access to the mysql server the program gets stuck!!

database_flag_query = [NSURL URLWithString:@"http://192.168.0.20/iqkradio_stream_ip_flag.php"];
database_flag = [NSString stringWithContentsOfURL:database_flag_query encoding:NSASCIIStringEncoding error:&error];

database_flag = [database_flag stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];

if ([database_flag isEqualToString: @"1"])
{
    NSLog(@"URL flag is set");
    database_url_query = [NSURL URLWithString:@"http://192.168.0.20/iqkradio_stream_ip_url.php"];
    database_url = [NSString stringWithContentsOfURL:database_url_query encoding:NSASCIIStringEncoding error:&error];
    database_url = [database_url stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
    url1 = [NSURL URLWithString:[database_url stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
    NSLog(database_url);
}

else
{
    NSLog(@"URL flag is not set, Reverting to stored value");
    url1 = [NSURL URLWithString: [NSString stringWithFormat: @"http://radio.qkradio.com.au:8382/listen.mp3"]];
}



streamer = [[AudioStreamer alloc] initWithURL:url1];
Ossama
  • 2,401
  • 7
  • 46
  • 83
  • 1
    I showed you how to handle this exact line in my answer to [your earlier question](http://stackoverflow.com/questions/15983768/parse-integer-from-mysql-query-in-xcode). – rmaddy Apr 13 '13 at 05:19
  • 1
    The proper way to get any data from a network is to use `NSURLConnection` and perform an asynchronous load of the data. This also allows you to set a timeout. By doing an asynchronous load you don't block the main thread and the timeout helps deal with overly slow or dead connections. – rmaddy Apr 13 '13 at 05:27
  • @ossama I would listen to what rmaddy says in his answer, I have used lots of answers he has offered. Just saying man. – Adrian P Apr 13 '13 at 05:49
  • Thanks maddy i will give this a try, witha timeout – Ossama Apr 13 '13 at 05:52
  • being a noob at xcode, would you please be able to modify the code to use NSURLConnection – Ossama Apr 13 '13 at 05:56

1 Answers1

0

EDIT - NSURLConnection & Timeouts - Based on the additional information and the comment stream below, and to put information in the answer (rather than the long comment stream):

see accepted answer to this question here for the timeout example. For the NSURLConnection example, checkout the apple documentation here

General Error Handling -

The following link may be helpful --> http://developer.apple.com/library/ios/#documentation/cocoa/conceptual/ProgrammingWithObjectiveC/ErrorHandling/ErrorHandling.html.

"Before you call this method, you’ll need to create a suitable pointer so that you can pass its address:

NSError *anyError;
BOOL success = [receivedData writeToURL:someLocalFileURL
                                options:0
                                  error:&anyError];
if (!success) {
    NSLog(@"Write failed with error: %@", anyError);
    // present error to user
}

If an error occurs, the writeToURL:... method will return NO, and update your anyError pointer to point to an error object describing the problem.

When dealing with errors passed by reference, it’s important to test the return value of the method to see whether an error occurred, as shown above. Don’t just test to see whether the error pointer was set to point to an error."

So, for your issue, try adding:

if(!database_flag)
{
    //call your error handling function
    [myFunction withError: error];
}

before trimming the database_flag. If your connection isn't working, then you need to handle it before continueing to your if([database_flag isEqualToString:... code.

If that doesn't solve the problem, can you give some information/log statements on where/what the error is that is halting your application?

Hope that helps.

Community
  • 1
  • 1
jwest
  • 100
  • 1
  • 1
  • 9
  • Thannks, is there an example I could use to help me with this, it seems I do not have all the pieces – Ossama Apr 13 '13 at 05:09
  • can't i simply point to a function to handle the error, for example database_flag = [NSString stringWithContentsOfURL:database_flag_query encoding:NSASCIIStringEncoding **error:function()**]; – Ossama Apr 13 '13 at 05:11
  • How exactly do you want to handle the error? As in the example I posted, you could check if database_flag isn't what you expect (perhaps null) and then handle the error as above. What type is database_flag? Note that this type of error is different from an Exception. It does not stop the program flow, but is used to report (i.e., give information about) errors if they occurred. Hence the following statement from the link I included: "Tip: If you’re not interested in the error object, just pass NULL for the error: parameter." – jwest Apr 13 '13 at 05:18
  • @Ossama No you can't point to a function, an NSError object must be used. However, you can use an if block to check if an error occurred (check the NSError returned) and call your function inside the block. (i.e., where the example has `NSLog(@"Write failed with error: %@", anyError);`, just call your function instead) – jwest Apr 13 '13 at 05:20
  • TO explain more please find above my code, Basically I want to check mysql for a flag if the flag is 1 then i get the ip address of the stream from the databse else i use the local one store. the only issue is when there is not access to the mysql server the program gets stuck!! – Ossama Apr 13 '13 at 05:23
  • @Ossama Edited answer. Perhaps you could add specific errors you are getting to your question? (maybe which log statements are going through, if any)? – jwest Apr 13 '13 at 05:33
  • when i put a dummy url (offline) the program gets stuck on the 2nd line of the code above, actually it only gets stuck for 60s, then it cotinues execution. i do not wish to have 60secs wait, otherwise the user wil think the program is stuck – Ossama Apr 13 '13 at 05:46
  • @Ossama The time delay is not because it is stuck. It is trying to communicate with the dummy URL. It takes a while to realize that it cannot make the connection. Unfortunately that is generally the case with hitting URLs like this. Fortunately connections are usually successful. I have used certain apps where this has happened (where it can't connect to the server) and it is only a minor annoyance IMO, as it doesn't happen too often. In the error checking block, though, you could alert the user of the issue so that they are aware and understand why they had to wait, if you wanted to. – jwest Apr 13 '13 at 05:55
  • I think i should use NSURLConnection as per maddy, this way i could set my timeout, but don't really know how hehehe any help – Ossama Apr 13 '13 at 05:57
  • see accepted answer to this question [here](http://stackoverflow.com/questions/1424608/nsurlconnection-timeout) for the timeout example. For the NSURLConnection example, checkout the apple documentation [here](http://developer.apple.com/library/ios/#documentation/Cocoa/Conceptual/URLLoadingSystem/URLLoadingSystem.html) – jwest Apr 13 '13 at 06:03
  • how do i capture the data to a NSString? – Ossama Apr 13 '13 at 06:13
  • I've not used NSURLConnections to interact with databases, Sorry. I'd suggest trying to comment to rmaddy. Seems like there is some experience there you could tap, if you can't find it in the Apple documentation. – jwest Apr 13 '13 at 06:18