Let’s take a completely hypothetical situation where you’re developing an app that uses Core Data for the local storage and have a bunch of beta testers eagerly awaiting the next version before your product launch. Your previous attitude towards the data model was something along the lines of “It’s still pre-1.0, I’m not bothering with migrations yet, just delete and reinstall, c’mon.” However, you forgot that requiring the beta testers to deal with that isn’t exactly a friendly experience for them, and you made at least three changes to the data model since your last Beta release. Now if they run the app it’ll crash immediately because the database isn’t in sync with the data model.

Oops.

Usually when you make changes to the data model, you do so by creating a new version and telling the NSPersistentStoreCoordinator to perform lightweight migrations, then make your changes. Adding a new version of the data model after changes were made accomplishes nothing. Fortunately, you’re not screwed. We’re going to jump back in time, grab the old data model then pretend it was there all along.

Your MyProject.xcdatamodeld file is actually a directory. If you browse it in the Finder or Terminal, you’ll see more folders inside it, one for each version of your model. Inside those folders is a file simply called contents. This is an XML representation of the editor you see in Xcode.

Step 1 — Find the data model from the last beta version you released

Look through the history the xcdatamodeld file in your source control system 1. Hopefully you’ve been tagging all of your releases and can just checkout that specific one.

1
> git checkout 1.0-beta4

If not, you can mess around with git log to figure out where to go. This snippet can help you see the commits for a single file:

1
> git log --pretty=format:'%h : %s' --graph -n 45 FILENAME

Then, checkout the particular commit with the right version.

Step 2 — Copy the contents file

Find the contents file within your .xcdatamodeld file. Copy all that XML somewhere safe.

Go back to your HEAD or wherever you were.

Step 3 — Create the new version of the data model

If you didn’t know, the process is:

  • Open the .xcdatamodeld file in Xcode
  • In the Editor menu, click “Add Model Version”. Follow the instructions.
  • Open the File Inspector for your .xcdatamodeld. There is a Model Version segment in the inspector, make sure it’s on the version you just created.

Now you have two data models that are identical. Let’s change the history on the original one.

Step 4 — Change history

Close Xcode. That’s not mandatory but I’ve had it crash when mucking about with these files, and it’s just not worth the hassle.

Open the contents file for the original .xcdatamodeld in a text editor.

Paste in the version you created in Step 2.

Open Xcode. If you haven’t set up the NSPersistentStoreCoordinator to run migrations, do so now. This tutorial is pretty good.

Now when the app runs, the migrations update the users’ data and keep things from crashing.

Note: This is for lightweight migrations. Custom migrations are more complicated. objc.io has a great article on these. I don’t know if you can play fast and loose with the data model file like you can here though.

  1. You ARE using Source Control, right? Sometimes new developers will ask me why they need Source Control. I usually parrot the usual answers - branching is good, undo mistakes, tool integration, etc - but situations like this are where it really shines. Without source control here, you’d be hosed. You’d have to manually fix the XML in the contents file, which would be monumentally hard or altogether impossible depending on what changed and how good your memory is.

In the interest of security I’ve started to turn on Two Factor Authentication (aka 2FA) for some of the services I use. I tried it out with GitHub about a year ago, but turned it off shortly thereafter because I encountered a bunch of problems and didn’t have the time to figure them all out. That and Google’s Authenticator app having data loss issues after an update was a big red flag too.

Today it’s a little easier to manage. 1Password has 2FA support built in now, and there’s also Duo Mobile’s app. Turning it on was pretty easy: Go to the security page, click a few buttons, and follow instructions. Once it was enabled I decided to push some changes for a project, and then this happened:

1
2
3
> git push origin master
remote: Invalid username or password.
fatal: Authentication failed for 'https://github.com/swilliams/my-repo.git/'

Umm, ok. I mean, I guess the most secure repository is one that nobody can access.

The solution isn’t immediately obvious. I looked at GitHub’s setup docs again, but they didn’t mention anything about 2FA. When in doubt, try it again right? This time I got a username/password prompt. I had assumed I would get some sort of additional prompt to enter a single use code for the 2FA, so I pasted in my GitHub password.

1
2
3
4
5
> git push origin master
Username for 'https://github.com': swilliams
Password for 'https://swilliams@github.com':
remote: Invalid username or password.
fatal: Authentication failed for 'https://github.com/swilliams/my-repo.git/'

No dice.

Googling around a bit finally brought me to this page, “Creating an access token for command-line use”. When you enable 2FA you need to use a token as your password for the Terminal. I created this with the default scopes provided1, then copy/pasted the resulting token into the password prompt in my Terminal window.

1
2
3
4
5
6
7
8
9
10
± git push origin master
Username for 'https://github.com': swilliams
Password for 'https://swilliams@github.com':
Counting objects: 80, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (78/78), done.
Writing objects: 100% (80/80), 9.02 KiB | 0 bytes/s, done.
Total 80 (delta 58), reused 0 (delta 0)
To https://github.com/swilliams/my-repo.git
   01efb2c..445a0b6  master -> master

Yay!

I think that should handle all the headaches for 2FA with GitHub. I like the warm security feeling it brings, and it seems like the user experience has been cleared up too.

  1. For standard git operations, I don’t think you would need any of the other scopes available for apps, and you could probably remove

    gist
    from it too.

Managing memory has become easier. Things like ARC take away much of what was once a painful and bug-ridden task. Things are not always panacea of course; it is still far too possible to do the wrong thing and access memory you are not supposed to and receive the dreaded EXC_BAD_ACCESS error.

Today I was working on a project in Swift. And despite my appreciation of the language, the tooling still remains… suspect. I have covered that topic a couple of times before.

Sometimes it’s obvious where the problem is based on the call-stack. This time it was not so obvious. The problem occurred at the end of a series of steps in a wizard when all the prior screens were finally being released from memory. From what I could tell the error was when one of the view controllers that represented one of the steps in the wizard was being deinitialized, but otherwise I couldn’t immediately see where the problem was.

In Objective C you can turn on what’s called “NS Zombies” mode, which keeps objects allocated (as a special “Zombie” object) and then raises warnings you try to access one of those Zombie objects. In my experience this has not worked quite was well with Swift. With Zombies mode enabled the app ran fine without crashing, but also didn’t raise any warnings about accessing bad memory. It was a Heisenbug!

Heisenberg
Eh, close enough

Next I started to play around with the code thinking that I was improperly handling the lifecycle of some of the properties of several classes. I changed around some lazy properties and made certain other things optional, but this was just wheel spinning.

I backed up and reviewed the callstack again. The last frame before explosion was now at swift_unknownWeakRelease in a helper class referenced by the offending ViewController. This helper had this property:

1
2
// SearchBarHelper.swift
private unowned let searchBar: UISearchBar

I then re-checked the documentation for unowned.

If you try to access an unowned reference after the instance that it references is deallocated, you will trigger a runtime error. Use unowned references only when you are sure that the reference will always refer to an instance.

Note also that Swift guarantees your app will crash if you try to access an unowned reference after the instance it references is deallocated. You will never encounter unexpected behavior in this situation. Your app will always crash reliably, although you should, of course, prevent it from doing so.

That searchBar was originally defined as an IBOutlet on the ViewController:

1
2
// ViewController.swift
@IBOutlet weak var searchBar: UISearchBar!

The lights turned on. Of course it was crashing. An outlet that’s a weak property can and will be deallocated when the controller referencing it is no longer visible. I forgot about that when I created the other class to manage certain characteristics about that search bar. So, the searchBar was released at some point, and when the helper was next called (in deallocation) part of its representation in memory was an unowned property that was nil, a state it cannot be in. Thus, EXC_BAD_ACCESS.

The solution was simple, I still didn’t want SearchBarHelper to have ownership of the searchBar, so I changed it to a weak optional.

1
2
// SearchBarHelper.swift
private weak var searchBar: UISearchBar?

Success!

Takeaway lesson: read and understand the documentation. Memory and how it is handled is still something that absolutely must be understood if you want to manage it well.

The other day a couple of salespeople for the Arizona Republic came knocking on my door. Here was their pitch:

“We’re going to start delivering you a paper every Wednesday and Sunday. Would you like it in your driveway or on the roof?” and then a pause for laughter. I was more confused; her delivery was pretty bad, and there was a distinct waft of desperation.

I started to explain that I didn’t want a newspaper even if it was free.

“But it’s only $.95 per week! You’ll make that up with all the coupons you’ll be getting!” She was pushing the coupons pretty hard. I guess this is why people get newspapers, in order to buy cereal for $.20 off?

I said I didn’t really care about coupons and politely told them to have a good afternoon. After extricating myself from the conversation I realized something: not once during their pitch did they mention the actual content of the paper. That’s got to be depressing if you’re a journalist — according to your sales staff your work is nothing more than a coupon delivery system with some words in between.

  1. You don’t have to be on Twitter every 2 minutes. If there’s a meme you don’t like, a perfectly valid option is to put your phone away.
  2. Silliness is good. Everything doesn’t have to be all doom and gloom serious all the time.
  3. Change takes time, and is often the result of a succession of small steps with a few medium ones here and there. Be happy when the small steps happen.
  4. Find some joy.

Is that a little reductive? Sure. But if you find yourself feeling angrier and angrier over what happens on Twitter, it might be time to reconsider how you’re using it.

My First Diablo is the newest episode of my podcast. It’s also the first episode where we have a guest: Steve Lubitz of Isometric fame. We talk video games and how that works when you have a bunch of kids in the house. I’d wanted to discuss this topic because my kids are now old enough to play console games without getting too frustrated over the experience, and I’d been out of the loop for quite some time.

Steve had some solid recommendations and now my wife and I are discussing when would be a good time to re-introduce consoles into the house. That might happen this summer when the kids are done with school, and there will be more time available to figure out limits and such. I’m thinking a Wii U would be the right way to go since there’s a better selection of kid-friendly games there. What I’m not looking forward to is all the controllers/accessories that Nintendo seems to think are required for their games. Most bundles for the Wii U come with the controller with a screen on it. I can’t tell if you can buy more of those or not1. But you can get Wii-motes, nunchucks, pro controllers, and various third party gizmos. I’m not exactly looking forward to sifting through exactly what kind of those we’ll need, nor storing them. I’m not even going to touch handheld systems.2

Video games were a big part of my childhood (some might say obsessive… I had a subscription to Nintendo Power without owning a Nintendo system). My hope is that rather than just consuming all the video game minutiae like I did, my kids will just have fun playing them with their family and friends.

  1. Amazon and other retailers seem to be sold out of official ones.

  2. Scientifically speaking, there are 327 different versions of the Nintendo DS to choose from.

Recently, Glenn Greenwald, Laura Poitras, and Edward Snowden took part in an ‘IAMA’ converstation on Reddit. It’s a long one, and somewhere down the page, this question was asked:

What’s the best way to make NSA spying an issue in the 2016 Presidential Election? It seems like while it was a big deal in 2013, ISIS and other events have put it on the back burner for now in the media and general public. What are your ideas for how to bring it back to the forefront?

Funny how this seems to happen. Snowden gave a tremendous answer. There’s too many great quotes to pull from his response, so I’ll just copy/paste the whole thing.

This is a good question, and there are some good traditional answers here. Organizing is important. Activism is important.

At the same time, we should remember that governments don’t often reform themselves. One of the arguments in a book I read recently (Bruce Schneier, “Data and Goliath”), is that perfect enforcement of the law sounds like a good thing, but that may not always be the case. The end of crime sounds pretty compelling, right, so how can that be?

Well, when we look back on history, the progress of Western civilization and human rights is actually founded on the violation of law. America was of course born out of a violent revolution that was an outrageous treason against the crown and established order of the day. History shows that the righting of historical wrongs is often born from acts of unrepentant criminality. Slavery. The protection of persecuted Jews.

But even on less extremist topics, we can find similar examples. How about the prohibition of alcohol? Gay marriage? Marijuana?

Where would we be today if the government, enjoying powers of perfect surveillance and enforcement, had — entirely within the law — rounded up, imprisoned, and shamed all of these lawbreakers?

Ultimately, if people lose their willingness to recognize that there are times in our history when legality becomes distinct from morality, we aren’t just ceding control of our rights to government, but our agency in determing thour futures.

How does this relate to politics? Well, I suspect that governments today are more concerned with the loss of their ability to control and regulate the behavior of their citizens than they are with their citizens’ discontent.

How do we make that work for us? We can devise means, through the application and sophistication of science, to remind governments that if they will not be responsible stewards of our rights, we the people will implement systems that provide for a means of not just enforcing our rights, but removing from governments the ability to interfere with those rights.

You can see the beginnings of this dynamic today in the statements of government officials complaining about the adoption of encryption by major technology providers. The idea here isn’t to fling ourselves into anarchy and do away with government, but to remind the government that there must always be a balance of power between the governing and the governed, and that as the progress of science increasingly empowers communities and individuals, there will be more and more areas of our lives where — if government insists on behaving poorly and with a callous disregard for the citizen — we can find ways to reduce or remove their powers on a new — and permanent — basis.

Our rights are not granted by governments. They are inherent to our nature. But it’s entirely the opposite for governments: their privileges are precisely equal to only those which we suffer them to enjoy.

We haven’t had to think about that much in the last few decades because quality of life has been increasing across almost all measures in a significant way, and that has led to a comfortable complacency. But here and there throughout history, we’ll occasionally come across these periods where governments think more about what they “can” do rather than what they “should” do, and what is lawful will become increasingly distinct from what is moral.

In such times, we’d do well to remember that at the end of the day, the law doesn’t defend us; we defend the law. And when it becomes contrary to our morals, we have both the right and the responsibility to rebalance it toward just ends.

You should listen to Relay.fm’s new series on Inquisitive, “Behind the App”. Myke opens the show with how the iPhone (and smartphones in general) helped bring podcasts to a larger audience. He’s absolutely right.

Before smartphones, you had to sync everything to an iPod with your computer before you could listen to it. For me, this usually happened at home, and if I forgot to plug in my iPod and sync it, I’d go without new episodes for the day; a fate worse than death. Syncing an iPod at the office was usually out of the question. IT policy at the time disallowed connecting personal hardware to company PCs 1 and blacklisted many of the sites hosting the audio files anyway.

I could live with the first issue, but I would go to great lengths to work around the latter one. Even though the experience wasn’t as nice, downloading and listening to a podcast on my work PC was still something. Here’s how:

  • SSH into my Linux based web server
  • wget the RSS feed
  • Find the link to the newest mp3 file
  • wget the mp3
  • Copy it into a folder publicly accessible on my server (this wasn’t blocked by IT)
  • Download it locally with my web browser (this was Windows and installing something like wget was also strictly verboten from IT)
  • Listen

Easy peesy lemon squeezey, right?2 Now, if I want to listen to a podcast, I open up Overcast and see if there’s anything new. The only thing I have to think about now if I want to download over my cellular connection or not.

The future is good.

  1. For very good reason. God only knows what kind of malware you brought along with your external drive…

  2. “OK… First I’ll access the secret military spy satellite that’s in a geosynchronous orbit over the Midwest. Then, I’ll ID the limo by the vanity plate ‘MR. BIGGG’ and get his approximate position. Then, I’ll reposition the transmitter dish on the remote truck to 17.32 degrees east, hit WESTAR 4 over the Atlantic, bounce the signal down into the Azores, up to COMSAT 6, beam it back to SATCOM 2 transmitter number 137, and down on the dish on the back of Mr. Big’s limo… It’s almost too easy.” — Garth Algar

When listening to a podcast, the host will often forget a word or well known fact. Maybe they’ll say something silly like “Jedi mind meld”. Everyone in the audience knows exactly what they meant, and everyone will likely make that fact known (I’ve sure been there, and will likely continue to be).

Now that I’ve released a few episodes of my own podcast I’ve seen the other side of the microphone. Talking and thinking at the same time is surprisingly difficult after say, 8 words in. I’ll find myself stumbling over witty quips and forgetting familiar things that had been otherwise seared into my memory.

So, I have a teensy bit more empathy for Pete Carroll the day after an impressive Super Bowl Loss. Yeah, he probably should’ve run Marshawn Lynch on that one play, but I don’t know if I would’ve had the brain power to make a different decision with under 30 seconds left, in the 4th quarter, of the Super Bowl.

Podcasts are Important Business. Here’s an update.

Better Know a Jackal

The [rabid] fans of the 5by5 podcast network are informally known as “Jackals”. One of said Jackals, Mike Beasterfeld had the brilliant idea to create a podcast to interview us. I made it onto episode 28. It’s a nice little bio that you can use to gather information to steal my identity.

Less Than Or Equal

I was a guest on episode 25 of Aleen Simms’s Less Than Or Equal podcast. I had a great time talking with her and we chatted about maturity, growing up, and how having daughters changed me; specifically some specific incidents in my personal history that formed me into what I am today. Speaking of daughters…

Front to Back

We just released our 6th(!) episode. I’ve really enjoyed making this show and have learned more and more about audio editing. This time around I used some compression to make our voices sound better.

Please check them out!

Copyright © 2018 - Scott Williams - Powered by Octopress