robotoworks / mechanoid

Eclipse plugin providing a set of DSL's for the rapid development of Android apps
58 stars 26 forks source link

mechOps lifecycle #201

Closed hannesa2 closed 10 years ago

hannesa2 commented 11 years ago

When I start an operation from an activity, the activity doesn't life after a while and I want to raise an error dialog from operation, like

    Builder builderProgress = new AlertDialog.Builder(context.getApplicationContext());

It doesn't work. Do you have an example showing this ?

fluxtah commented 11 years ago

@hannesa2 When using operations and handling the operation complete callback you should check the state of the activity before performing any UI changes, especially showing dialogs which can lead to issues such as fragment manager transaction errors.

In the docs it explains you can call methods such as isAdded() for fragments or isDestroyed() for activities http://robotoworks.com/mechanoid/doc/ops/index.html#executor-callbacks

In our application for activities we implement callbacks like this:

        @Override
        public boolean onOperationComplete(String key, OperationResult result) {
            if(isDestroyed()) {
                return false;
            }

            // TODO Handle the operation

            return true;
        }

What happens here is if the activity is in a state that can handle the operation (not being destroyed) the we can continue but if not we return false early, this is convenient since the onOperationComplete(...) will be called again (after rotation, etc) until you return true.

If the activity is not available anymore its best not to show a dialog, but if you do need to show something in the case of an operation failing at the same time as an orientation change or on the backstack, etc, then you can show an error panel instead of a dialog which is what we do for failed network requests in the Just-Eat UK application.

Hope that helps.

Regards,

Ian

hannesa2 commented 11 years ago

Thank you ! The last sentence is exactly what I want to handle. For example: in first activity I start background sync to server and let the user work further on other activities/fragments.... After a while any network problems happen. Initial activity is removed, and I want to show something to the user. Your solution 'error panel' : What do you mean exactly here, which working context to use ? How to handle the op-callback on an other activity ? Sorry for my rudimentary questions

Thank you ! Hannes

fluxtah commented 11 years ago

Well, because the activity is possibly on the back stack, then the operation complete callback will still be invoked so it should be possible to change the visibility of a view that represents an error panel, something like facebook:

image

In most cases though if an error occurs and the user is no longer looking at the activity you could safely ignore it but if you really need to do show something as a result of an operation error it would be best to persist some kind of state either in the database or in user preferences and check that state next time the user starts the app.

I recommend a great video which you may have seen that was the motivation behind operations http://www.youtube.com/watch?v=xHXn3Kg2IQE

In most cases for fetching data you do not need a robust architecture you can simply show connection error panels but for saving data over a network you might need to follow the pattern described by dobjanschi tracking errors in the database.