views:

34

answers:

2

So assume I have a class that has an init method that does something like... grabs some data off the net in xml format and parses it to initialize some of its properties. My concern is how should I handle the case where the network is down or the xml data my object receives is bad?

Normally in C i would use return values to indicate an error and what kind and then that would get propagated back till I could report it to the user. I don't really think that will work in this situation.

A: 

For network down you have a few options

  • Alert the user you cannot retrieve the needed data
  • Show stale (last loaded, maybe not stale?) data

For Bad Data:

  • Alert the user
  • Try again
  • Show old data
  • Try to fix the data (missing a closing tag? etc)
  • Show a subset of the data (maybe you can extract something that is usable?)

As far as error codes, you can do:

  • Return codes ie bad_data -1, no_network -2, etc.
  • You can throw exceptions, catch them and map them to user friendly display messages
Mr-sk
+1  A: 

Use asynchronous network requests.

  1. Create the UI and show it with either dummy replacement for the actual values (like pictures) or no data (for example empty table).

  2. Then create and send the request for data and register handler that gets called with data.

  3. When you receive data your handler gets called with them.

  4. You parse the data and update the UI. In case of data being invalid you can now update UI to inform the user.

  5. You can use timeouts to cancel requests in case of network problem and functions not returning with data within specific time.

There was an example in last year Stanford's CS193p class (iPhone programming but the same applies to desktop apps) with showing empty user interface and updating it when data coming back. You can probably find references to it on net or otherwise there'll be new example this year.

stefanB