Darel Rex Finley in PhotoBooth

Rock-Solid iOS App Stability

2011.12.22   prev     next

A couple years ago, I decided to dabble in iPhone app development. I wrote a little daily fitness routine tracker and tried to sell it for 99¢. It didn’t sell worth a damn, so I made it free for more than a year, and just recently I let it drop off the App Store so I could avoid the annual $99 developer fee. Maybe it will come back someday if I think of a better idea for an iPhone app and I’m paying the annual fee again.

But now for the interesting part of the story. The app had to have a way to store the user data in a file, so I just made up a file format. This byte meant this; that byte meant that; some data structures were variable length. Nothing too terribly complicated — just what was needed to store the user’s data for this app.

One day while testing the near-finished app, a funny thing happened. One of the scrollable lists in the app suddenly became defective. It still functioned, but the contents were obviously wrong. And I realized by looking at it that the likely culprit was corrupted user data. But when did it get corrupted? And by what code in my app? Suddenly, the enormity of diagnosing the problem made me feel a little ill.

And I felt a little more ill when I realized something else: There’s a lot of code in my app that makes changes to the user data. If there’s a bug somewhere that caused this problem to occur, even if I find it and fix it, how will I know it’s the only one? What if there are more? And what if I introduce still more bugs while making changes or adding features to the app? And worst of all: What if even one of these bugs slips out in the released product, and users lose everything the’ve done in the app, after weeks or months of use? That’s totally unacceptable. I don’t want that to happen at all.

I realized that I had to come up with some way to make my user data absolutely, rock-solid stable. But how? The only way I could think of that seemed like it would work would be to completely re-invent my user data as an XML-based format, then use somebody’s already-written, XML manipulation package to apply all changes to the content of the user data.

But this plan, even assuming it gave me the desired user-data stability, seemed like too big of a change to an app that was already written and in the testing phase. And it also seemed like an uncomfortable transfer of too much of the functionality to somebody else’s code over which I have no control. I.e., I can’t make it run fast if it’s slow; I can’t make it power-efficient if it’s a battery hog; I can’t bug-fix it if it turns out to need that, and I can’t make XML a tight, memory-efficient format if XML just isn’t.

And even if I did all that, couldn’t the user data still become corrupted? Valid XML doesn’t automatically mean valid user data, according to what my app needs to store there.

So what to do? I puzzled over it for a few days and then came up with a solution.

validateUserData

The user data was a single block of memory (variable size) that gets written to the app’s file space as a single, binary file, and read entirely into memory when the app loads. So it’s always there in memory, and I can examine it any time I want.

So I wrote a new function, called validateUserData. When you call this function, you pass it the memory address of the user data, and it does a quick but very thorough anal exam of the entire block of user data, looking for any deviation from the format that the user data is supposed to be in, according to my own, already-designed user data spec.

Of course, this function can’t find anything that might be wrong with the data, since it’s conceivable that the user data could be corrupted in a way that still looks like valid user data according to the spec, but it’s not too likely that will happen.

If validateUserData decides that everything is OK, it returns an empty string (i.e. no error message). But if it finds anything wrong with the user data, it immediately returns with a descriptive message. This message might not mean anything to a user (e.g. “Date list has negative length”), but to the developer (me) it’s very valuable information.

Then, I changed the app so that any time it does anything with the user data, it first makes a copy of the user data block, performs its change on the copy, then validates the new user data with validateUserData. If the new data validates, the old data is discarded, and the app continues normally. But if the data doesn’t validate, then the new data is discarded, the old data is kept, and the app pops up an alert saying that the action couldn’t be performed because it was going to cause corruption of user data. And the app displays the descriptive message returned by validateUserData.

Once I installed this feature into the app, I quickly discovered the cause of the above-described bug, and found a couple more bugs as well. Fixing these bugs was a breeze, because I knew exactly what action first corrupted the user data, and I had a description of exactly what was wrong with the structure of the user data.

And best of all: When I released the app to users, I did so with supreme confidence in the stability of the app because (a) validateUserData had given me a strong feeling that no obscure bugs were still lurking undetected, and (b) validateUserData promises to both protect users from data loss if they do run into an obscure bug, and give them the ability to report the bug in a way that will be extremely useful to my attempts to find and fix it.

 

Update 2012.03.02 — “iOS” added to title

 

prev     next

 

 

Hear, hear

prev     next

Favorite links

Starbucks

Apple

RoughlyDrafted

Daring Fireball

Joel on Software

Grupo Fantasma

Macalope

Red Meat

Despair, Inc.

The J. Peterman Company

Zombie Survival Guide plus Dawn of the Dead

Charlie Superfly Check “The First Time” to hear what she actually sang in the competition. HowardTV ripped it out and spliced in utter crap they had her sing later.

Real Solution #9 (Mambo Mania Mix) over stock nuke tests.

Ernie & Bert In Casino

Great Explanation of Star Wars

Best Superbowl 43 Commercial

Best Superbowl 45 Commercial

Best Superbowl 46 Commercial

Kirk & Spock get Closer

American football explained.

Piada. Yum. Like, Chipotle meets Italian.

TV: Survivor; Justified; Khloé & Lamar; Cash Cab

God’s kitchen

My vote for best commercial ever.

Entire site as a zip file — last updated 2012.05.02

My books

Now available on the iBookstore!

   

Previous articles

What Windows-vs.-Mac Actually Proved

A Tale of Two Logos

Microsoft’s Three Paths

Amazon Won’t Be A Big Winner In the DOJ’s Price-Fixing Suit

Infinite Sets, Infinite Authority

Strategy Analytics and Long Term Accountability

The Third Stage of Computing

Why 1 Isn’t Prime, 2 Is Prime, and 2 Is the Only Even Prime

Readability BS

Lie Detection and Psychos

Liking

Steps

Microsoft’s Dim Prospects

Humanity — Just Barely

Hanke-Henry Calendar Won’t Be Adopted

Collatz Conjecture Analysis (But No Proof; Sorry)

Rock-Solid iOS App Stability

Microsoft’s Uncreative Character

Microsoft’s Alternate Reality Bubble

Microsoft’s Three Ruts

Society’s Fascination With Mass Murder

PlaysForSure and Wikipedia — Revisionism At Its Finest

Procrastination

Patent Reform?

How Many Licks

Microsoft’s Incredible Run

Voting Socialist

Darwin Saves

Collatz Conjecture ... Not Yet

The Size of Things In the Universe

The Self-Fulfilling Prophecy That Wasn’t

Fun

Nobody Was In Love With Windows

Apples To Apples — How Anti-Apple Pundits Shoot Themselves In the Foot

No Holds Barred

Betting Against Humanity

Apple’s Premium Features Are Free

Why So Many Computer Guys Hate Apple

3D TV With No Glasses and No Parallax/Focus Issues

Waves With Particle-Like Properties

Gridlock Is Just Fine

Sex Is A Fantasy

Major Player

Why the iPad Wannabes Will Definitely Flop

Predators and Parasites

Prison Is For Lotto Losers

The False Dichotomy

Wait and See — Windows-vs-Mac Will Repeat Itself

Dishonesty For the Greater Good

Barr Part 2

Enough Information

Zune Is For Apple Haters

Good Open, Bad Open

Beach Bodies — Who’s Really Shallow?

Upgrade? Maybe Not

Eliminating the Impossible

Selfish Desires

Farewell, Pirate Cachet

The Two Risk-Takers

Number of Companies — the Idiocy That Never Dies

Holding On To the Solution

Apple Religion

Long-Term Planning

What You Have To Give Up

The End of Elitism

Good and Evil

Life

How Religion Distorts Science

Laziness and Creativity

Sideloading and the Supersized-Mastodon-In-the-Room That Snell Doesn’t See

Long-Term Self-Delusion

App Store Success Won’t Translate To Books, Movies, and Shows

Silly iPad Spoilsports

I Disagree

Five Rational Counterarguments

Majority Report

Simply Unjust

Zooman Science

Reaganomics — Like A Diet — Works

Free R&D?

Apple’s On the Right Track

Mountains of Evidence

What We Do

Hope Conquers All

Humans Are Special — Just Not That Special

Life = Survival of the Fittest

Excuse Me, We’re Going To Build On Your Property

No Trademark iWorries

Knowing

Twisted Excuses

The Fall of Google

Real Painters

The Meaning of Kicking Ass

How To Really Stop Casual Movie Disc Ripping

The Solitary Path of the High-Talent Programmer

Fixing, Not Preaching

Why Blackmail Is Still Illegal

Designers Cannot Do Anything Imaginable

Wise Dr. Drew

Rats In A Too-Small Cage

Coming To Reason

Everything Isn’t Moving To the Web

Pragmatics, Not Rights

Grey Zone

Methodologically Dogmatic

The Purpose of Language

The Punishment Defines the Crime

Two Many Cooks

Pragmatism

One Last Splurge

Making Money

What Heaven and Hell Are Really About

America — The Last Suburb

Hoarding

What the Cloud Isn’t For

Diminishing Returns

What You’re Seeing

What My Life Needs To Be

Taking An Early Retirement

Office Buildings

A, B, C, D, Pointless Relativity

Stephen Meyer and Michael Medved — Where Is ID Going?

If You Didn’t Vote — Complain Away

iPhone Party-Poopers Redux

What Free Will Is Really About

Spectacularly Well

Pointless Wrappers

PTED — The P Is Silent

Out of Sync

Stupid Stickers

Security Through Normalcy

The Case For Corporate Bonuses

Movie Copyrights Are Forever

Permitted By Whom?

Quantum Cognition and Other Hogwash

The Problem With Message Theory

Bell’s Boring Inequality and the Insanity of the Gaps

Paying the Rent At the 6 Park Avenue Apartments

Primary + Reviewer — An Alternative IT Plan For Corporations

Yes Yes Yes

Feelings

Hey Hey Whine Whine

Microsoft About Microsoft Visual Microsoft Studio Microsoft

Hidden Purple Tiger

Forest Fair Mall and the Second Lamborghini

Intelligent Design — The Straight Dope

Maxwell’s Demon — Two Real-World Examples

Zealots

Entitlement BS

Agenderle

Mutations

Einstein’s Error — The Confusion of Laws With Their Effects

The Museum Is the Art

Polly Sooth the Air Rage

The Truth

The Darkness

Morality = STDs?

Fulfilling the Moral Duty To Disdain

MustWinForSure

Choice

Real Design

The Two Rules of Great Programming

Cynicism

The End of the Nerds

Poverty — Humanity’s Damage Control

Berners-Lee’s Rating System = Google

The Secret Anti-MP3 Trick In “Independent Women” and “You Sang To Me”

ID and the Large Hadron Collider Scare

Not A Bluff

The Fall of Microsoft

Life Sucks When You’re Not Winning

Aware

The Old-Fashioned Way

The Old People Who Pop Into Existence

Theodicy — A Big Stack of Papers

The Designed, Cause-and-Effect Brain

Mosaics

IC Counterarguments

The Capitalist’s Imaginary Line

Education Isn’t Everything

I Don’t Know

Funny iPhone Party-Poopers

Avoiding Conflict At All Costs

Behavior and Free Will, Unconfused

“Reduced To” Absurdum

Suzie and Bubba Redneck — the Carriers of Intelligence

Everything You Need To Know About Haldane’s Dilemma

Darwin + Hitler = Baloney

Meta-ware

Designed For Combat

Speed Racer R Us

Bold — Uh-huh

Conscious of Consciousness

Future Perfect

Where Real and Yahoo Went Wrong

The Purpose of Surface

Eradicating Religion Won’t Eradicate War

Documentation Overkill

A Tale of Two Movies

The Changing Face of Sam Adams

Dinesh D’Souza On ID

Why Quintic (and Higher) Polynomials Have No Algebraic Solution

Translation of Paul Graham’s Footnote To Plain English

What Happened To Moore’s Law?

Goldston On ID

The End of Martial Law

The Two Faces of Evolution

A Fine Recommendation

Free Will and Population Statistics

Dennett/D’Souza Debate — D’Souza

Dennett/D’Souza Debate — Dennett

The Non-Euclidean Geometry That Wasn’t There

Defective Attitude Towards Suburbia

The Twin Deficit Phantoms

Sleep Sync and Vertical Hold

More FUD In Your Eye

The Myth of Rubbernecking

Keeping Intelligent Design Honest

Failure of the Amiga — Not Just Mismanagement

Maxwell’s Silver Hammer = Be My Honey Do?

End Unsecured Debt

The Digits of Pi Cannot Be Sequentially Generated By A Computer Program

Faster Is Better

Goals Can’t Be Avoided

Propped-Up Products

Ignoring ID Won’t Work

The Crabs and the Bucket

Communism As A Side Effect of the Transition To Capitalism

Google and Wikipedia, Revisited

National Geographic’s Obesity BS

Cavemen

Theodicy Is For Losers

Seattle Redux

Quitting

Living Well

A Memory of Gateway

Is Apple’s Font Rendering Really Non-Pixel-Aware?

Humans Are Complexity, Not Choice

A Subtle Shift

Moralism — The Emperor’s New Success

Code Is Our Friend

The Edge of Religion

The Dark Side of Pixel-Aware Font Rendering

The Futility of DVD Encryption

ID Isn’t About Size or Speed

Blood-Curdling Screams

ID Venn Diagram

Rich and Good-Looking? Why Libertarianism Goes Nowhere

FUV — Fear, Uncertainty, and Vista

Malware Isn’t About Total Control

Howard = Second Coming?

Doomsday? Or Just Another Sunday

The Real Function of Wikipedia In A Google World

Objective-C Philosophy

Clarity From Cisco

2007 Macworld Keynote Prediction

FUZ — Fear, Uncertainty, and Zune

No Fear — The Most Important Thing About Intelligent Design

How About A Rational Theodicy

Napster and the Subscription Model

Intelligent Design — Introduction

The One Feature I Want To See In Apple’s Safari.