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

My Current Desktop and Happy Vining.

It's been a big switch when I quit my old procession (Software Engineer) and ventured into the unknown.  So this is my current desktop.   First it's a REAL desktop and not a computer screen. Second, I've had to learn a whole new set of tools.  You would think a pencil is just a pencil and a paint brush is just a paint brush, but noooooo, they are tools into you mind and that's what needs re-tooling. The tactual nature of these tools and operation as much more rewarding (and frustrating too) than typing code in an IDE.   That had it's incentive but that well went dry and life changes,  so I changed. The pleasure of creating colors (no RGB involved) ad hoc wise is interesting.  Getting the technique to merge them on paper is very tough.  You do not know what you're going to get, so you just have to go with it. I'm glad that I can do a lot of other things instead of Software, but I hope that others in my past procession work on other pa...

3rd Try is a Charm

I've been trying to draw / paint these barns for a couple of years but never felt or got them right.  This time I think they turned out right. So What went wrong before and what's right now with this drawing?  This time, the light was right.  It's coming from the upper right and the shadows just looked right.  The other thing is the corn field on the left had to "be in season", otherwise it's just a plowed field.  I had taken other photos from different angles but they never felt right.  This angle has the road, power lines, corn field, etc. all leading to the right.  The shadows on the lower right helps fill in that corner (don't forget about the corners!).  The last part is trying to draw (ink paint maybe) the trees in the background.  Not so easy when they are kind of a blob is green shades. So yeah, it's composition that is king.  Many times I just don't see it until the drawing / painting is finished and when it's right it feels goo...

Thought Patterns

I know how I work.  How a think about, visualize a problem and go about mentally design a solution and implement it in code.  My thought pattern works pretty well for me on most occasions, but not always. There are times when I have a force myself to change my thought pattern because of the problem I'm solving requires a different mindset.  Anyway, this posting is about how I work most of the time.  Here is goes. I quickly break down a feature into operations that are required to exists and tackle that code first.  My thinking is that, if I can solve the primary issues first, then secondary features will fall into "known knowns" and I can address those at anytime (and sometimes at my leisure) and this allows me to meet my internal timeline. If I addressed features and operations that are not primary first, then I feel I risk my (and the projects) timeline without addressing issues that allow the feature to operate.  You may have seen this occur where...