Performance and Reliability (Day 12)

This is a blog post I hope will help many who haven’t yet discovered the hidden dangers of mobile programming.

You have limited resources, a dangerous, broken environment and people expect performance and perfection.

But guess what, you can’t trust anything. Its all going to break.

Data connection will drop

Your data connection will slow down then speed up.

Your data connection will switch from cellular to wireless data

Memory is limited

You are running over potentially hostile networks

Storage will run out

Your app will be backgrounded then resume

You will have revoked permissions

The new OS update will break something

It will run on a huge array of different devices doing different things

It isn’t stationery. Someone can steal it.

Your CPU speed is limited, though redeemed slightly as there are multiple of them.

But wait now you have thread syncing issues.

Now, quickly deliver me a secure, performant app that can run on many mobile devices, across 3 platforms over networks and app distribution channels you don’t own. Its amazing that it can be pulled off, which is why developers are getting on board and behind Xamarin Forms.

Reliability

Unreliable Network Connections

Its going to happen. Things you need to account for here:

Connection Resiliency – retry connections. I recommend you look at Polly to establish a policy.

Transaction handling if a sequence of events fails at a particular step.

Notify the user visually of these errors. This puts the emphasis back on the phone and network provider not on your app.

Permission Revoking

You have permission in an app, now watch it get invoked later.

Use the Permissions Plugin for Xamarin on each app load or resume to see if the permissions still hold for the app, and get them to reinstate or shut down the app if they are not given.

Low Storage

Make sure you have error handling to accommodate for no space and a solid revert plan if needed.

The OS will likely warn well before hand but you may want to add your own error message or warning if they are about to do an action that will use up a lot of storage.

Use External Storage as appropriate (only for data that is OK in the public domain)

Slow Network Connections

Cache when necessary and appropriate but you are then battling against memory usage.

Low Memory

Low memory is unfortunately something you can’t handle. Your app just crashes with an OutOfMemory Exception. Your app is normally allocated an upper bound in memory and it is wise to manage your allocation.

Images in Android are very poorly handled. Make sure you dispose when needed and have the exact size needed. Anything larger just uses more memory. It doesn’t matter what format your images are in, they are all converted to bitmap on Android.

Dispose of elements when not needed.

Ensure your navigation hierarchy isn’t over 4 pages deep. My personal preference but if you are going beyond that you need to have a good reason as its getting very complex to get back out again.

Remove Model states. When a view that is using a model goes out of scope, raise an event to that model to clean its state and remove any caching or loaded items.

Remove any caches when no longer needed.

Low CPU Power

CPU power is at a premium on mobile devices and you must use it wisely.

Keep your pages as simple as possible. Map out your Visual Tree. If it looks complex, its going to take a lot of processing power to compute.

Multi-thread if possible. If loading data on the page take 4 separate and not dependent tasks, then create a new task via Task.Run and use Task.WhenAll to wait until they all finish. This tip alone can immensely speed up page load times. Don’t over complicate this because multi-threaded issues are a nightmare to track and resolve.

Use XamlC. While not quite production ready at this point in time, it pre-compiles all your Xaml so the mobile device doesn’t have to. It noticeable loads pages quicker.

Cache, its can be quicker than recomputing.

5 Common Pitfalls In Enterprise Mobile Development

Based on my experience, of over 9,000hrs of Xamarin.Forms development, check out some of my hard learnt lessons, in enterprise Xamarin.Forms development.