Skip to main content

Multi-Tasking (in Android & iOS)

This post is not about how-to perform multi-tasking for Android and iOS, it's about my thinking related to how I handle threading issues in Android and iOS.

I've written multi-threading code for decades (in C/C++ and Java) before I got into mobile development.  Threading was one of those issues that used to be a black art.  You needed to handle and understand all of the semaphores, mutating and memory issues to provide a robust threaded application.  It's a lot easier now in both Android and iOS.  Clear examples and simplified code make threading quick and easy, but there is still complexity underlying the concept of threading for the UI.

My biggest insight when switching to mobile threads is the pattern of placing the threading inside of the activity or controller.  This is a big plus for simplicity and ease of use for development.  The thread is right there where it's needed and the thread has access to the Activities (or Controller's) data members for updating the UI when it's safe.  Cool, very cool.  Nice, neat and local to it's usage.  Threading for the masses.

For most of my threading issues this is what I use.  But this local pattern is not suited for all needs and is where the patterns for Android and iOS diverge.

One of my biggest issues with Android is the destruction of the Activity on rotation.  Not a big deal when doing Android phone development where the user is primary expected to to use a single orientation, but tablets are more rotatable.  The screen ratio is closer to not have a single preference and  different uses prefer different orientation (books -vs- movies, etc.).  With such a large screen to present on, it's expected that the tablets will be rotated and the app should support it and this has an impact on threading in Android.

The pattern of having the thread local in the Activity is great except when the device is rotated, the app supports it and a local Activity thread is running.  The thread will still be running, but the local UI data members that need to be updated will be for the pre-rotated Activities.  In short, it will not work in all cases, it's one of those timing things, sometimes it will work, sometimes it won't.  The local pattern will fail as it's too tightly coupled to that instance of the Activity.  For this case the thread needs to be changed to couple loosely to the Activity when updates need to occur.  I'm not going to cover solutions for this (they are not very hard, just different, i.e., observer pattern) but to express my thoughts of the Android threading for Tablet applications that should be rotatable.

I view the ability of tablet apps to support rotation as important as it impacts the tablet's usage.  Don't force the user to rotate the tablet when switching from reading a book to going to the App marketplace.

On iOS the threading issues for local threads inside of a Controller as much the same as Android except when the device is rotated there is no destruction of class instances and the same local thread pattern will always work.  The local threading block still has access to the controllers data members for updates on completion on the Event thread.   In iOS GCD (Grand Central Dispatch) the level of control other the threads is also much greater.  The queue's that can be used used, the processing order (serial or parallel) allows control that does not exist (easily) in Android.  I'll be using mostly the local pattern for my threads in iOS.  This is switch from my non-mobile apps where the threaded tasks are contained in their own class and packages.

My bottom line is that I've switched the location and implementation of my threads to use the local pattern of placing the threads inside of the Activity and Controllers.  It's just too nice of a pattern to miss out on.  My exceptions are for 1) Android apps that are rotatable and 2) longer running threads that impact multiple Activities or Controllers (I use MVC notifications for those).

It's been enjoyable working with threads for mobile apps and understanding the local pattern.  It makes my life easier.

Comments

Popular posts from this blog

Still Life

 Life is never still, at least I don't think you would want it to be.  That's why so many of us (i.e., retired care free people) travel.  Keep seeing new things and places.   Well the Hurricane so far inland was a new thing and one I would have rather forgone but life is what happens.  Life keeps moving. Except a still life is forever and captures a slice of time frozen in a painting.  This is from a wooden bowl of pears onboard our ship during dinner one nice.  Again, just thought it looked nice. Enjoy.

A Trip Abroad

 Just back from our trip abroad as Asheville continues to recover from the hurricane.  This trip has been planned for a long time and we almost didn't make it because our dog sitting business will be closed for a while. But make it we did and now we are back. We love Amsterdam.  What a fun place to visit.  So many things to too and do there.  The weather was not the best but it did not have an impact on our visit.  Just bundle up. Got some quick painting in while on the boat (i.e., Viking) as we moved from port to port.  This painting is of a garden at a heritage site of windmills.  I thought it looked nice. Nice time on the trip to de-stress from what was happing back home.  We were fine back home but not everything is well with many others.

A Process

Once in a while I take photos of a work in progress.  This is for me as well as others as the work moves from stage to stage.  And it is done in stages with defined processes for each step.  On this walkway overpass up in Spruce Pine, I've done both an ink / marker and an Ink / Wash on the same piece. This is the finished watercolor of the work.  To start the process I did an ink drawing of it and then took a tracing from that.  It's the tracing where I did another ink drawing but this time on watercolor paper. Tracings of a work is done with standard tracing paper.  I get mine from CheapJoes.com and use the 8x10 size as that covers most of my needs.  The tracing is done with a 0.3 ink pen (Winsor & Newton Fineliner).  Once I have the tracing I can then use it for other paper or to do another work of the same subject. The tracing is just a start as I still need to get it transferred onto other paper.  For that you need a very bright light...