A note on NSManagedObjectContextId

Like a lot of iOS Devs I’m used to working with my domain objects across various different threads.

As you may know, when you decide to work with an object on a different thread, you need to retrieve that object from that thread’s NSManagedObjectContext. This is normally done like so:

[myNewContext objectWithID:recordFromADifferentContext.objectID]

The record’s objectId is a universal identifier and you can find out more information from the Apple Docs on it.

However, I was recently having trouble with saving an object in one thread and then retrieving it in another thread. It was throwing an exception when I tried to use the object on the second thread.

The problem is that the initial objectId on a record is a temporary one. Therefore, when we hop onto a different thread, we can’t do a lookup on it.

The solution? Simply make the following call on your original managedObjectContext like so

[recordInFirstContext.managedObjectContext obtainPermanentIDsForObjects:@[recordInFirstContext] error:&error];

This will force the record to create a permanent objectId which can then be retrieved on another thread.

Screw stereotypes

A recent tweet by Marc Andreessen got me thinking about our culture:

“Silicon Valley is nerd culture, and we are the bro’s natural enemy.”

It got me thinking: what is nerd culture?

An interest in programming? Computers in general? Does it exist anymore? As a guy who could most certainly be viewed as a nerd (I started programming on a Commodore 64 as a kid and I could probably give you the rundown of every single game released in the N64/Playstation era), the whole ‘nerd culture’ thing always kinda jarred with me.

There are cultures around music, sport, film etc. Nerd culture is different to these in that its viewed as an exclusive membership. If you liked computers then you spent Friday nights at home playing Dungeons and Dragons, not out catching a game of ball.

When people learnt you had interest in computers they quickly assumed you didn’t have any interest in pursuits outside of technology.

This is understandable. Computers have only had widespread adoption in the past two or three decades. Tech culture is super young in the grand scheme of things. “Outsiders” built stereotypes due to a lack of understanding.

But my issue isn’t with outsiders pigeon-holing us. That happens for every culture eg. Football fans being seen as yobs, art fans seen as snobs etc. My problem is when we start viewing it this way ourselves.

If we ourselves start labelling each other as either “geeks”, “brogrammers” or whatever then we do a disservice to ourselves, to the person we’re labelling and to our industry.

Why? Because nobody is just one thing. I know plenty of people who are crazy sport fans who make great coders and I know plenty of self-proclaimed super-nerdy types who can’t hack for shit.

Here’s why it’s important to kill off these stereotypes now: because we’re at an important changing point in our industry and our culture. A career in tech is possible for more people (and more types of people) than ever before. The vast, vast majority of people considering a career in tech won’t fit into the nerd/brogrammer archetype.

For some people that won’t matter but for others it will make them reconsider pursuing an interest in which they have a very real passion.

Caricaturing the roles in our industry will reduce diversity and will mean that we’ll miss out on some really talented people who could do special things in technology.

What you can do

1. Realise that stereotypes add nothing positive to anything and try to wipe them from your mind.

2. Don’t fit yourself around a stereotype. Are you a programmer with bad social skills? Well then work on it – becoming a good conversationalist isn’t that hard. Are you a sales guy who just “doesn’t get” tech? Work on it. Again, it isn’t that hard.

3. Encourage as many people as you can towards our industry. The days of programmers being almost exclusively neck-bearded white males are coming to an end, people. This is a good thing.

Screw the stereotypes. Encourage people to code. Watch our industry blossom.

A word about inheritance and enums in Rails 4

Enums are a great feature in Rails 4 – ActiveRecord’s native adoption of them is nearly worth the upgrade alone. However, I want to share a gotcha I came across today on a project I’m working on (the new version of tech startup simulator Hipster CEO if you must know).

I have a model Task in my schema that functions as an assignable piece of work to a member of staff. I’ve used the acts_as_relation gem to create a multi-table inheritance structure with things like TechTask, MarketingTask etc to assign to different departments. Each one of these subtasks will have a specific type (eg. frontend work, security analysis, testing etc. for a TechTask).

Therefore, each subtask will be of a specific type but these will vary from subtask to subtask. For example, you can’t create a TechTask with a type of “online_marketing”. Since this attribute is shared (admittedly with different key/value pairs) I thought it would be good practice to move it up to the parent Task model and then define the individual keys in the subclasses. And that’s precisely when things went wrong.

In order for the parent class Task to use the enums, they need to be defined in the parent class itself. Otherwise, when you create a subclass object, it will fail when you try to set the enum. Moving all the enums up a level pretty much defeats the purpose of having these subclasses, since they would be so tightly coupled.

In the end, I had to create an identical attribute on each subclass of Task and wrote generic helper methods for retrieval/inspection etc.

Of course, it’s debatable that inheriting enums is a correct approach at all (from my research online there was plenty of people strongly against it) but this does feel like a valid use case.

In summary, don’t inherit enums in Rails 4 because you’re gonna have a bad time.

UPDATE: Moving the enum down to the sub-class table means you can’t (AFAIK) create a composite index on the enum and whatever data you have in the parent table (in my case, it was an association id). Not the end of the world, but something to bear in mind all the same.

RMMapView not deallocating memory in iOS? This could be the problem

I’m working with Mapbox at the moment – it’s a great tool for bundling offline maps into your apps. I recently noticed that my RMMapView’s weren’t deallocating as expected. These map views used by Mapbox, I’m not sure if they’re open source or not. Anyway, long story short, this was leading to a memory leak and I spent about a day trying to figure out why. The fix is pretty simple, just remember to set the “showsUserLocation” flag to false when you destroy the parent view. Something like:

- (void)viewWillDisappear:(BOOL)animated {
    if ([self isMovingFromParentViewController]) {
        _mapView.showsUserLocation = NO;
    [super viewWillDisappear:animated];

Hope this helps save someone a lot of time someday!

Embracing Generalism

When people ask what I do for a living I find it difficult to give a clear answer. My replies range from “Tech developer/designer/entrepreneur-type” on a good day to “I fix printers” on a not so good one.

The truth is I’ve been employed professionally in tech for the past seven years or so and my time has been pretty evenly split between server side stuff (Ruby/Java), front end work (Javascript/HTML/CSS/Photoshop), mobile stuff (mostly iOS) and general startup hustling (marketing/sales/branding).

Needless to say, it’s pretty difficult to find a job description that asks for all that and that’s cool because I’m freelance nowadays.

However, on the days when Imposter Syndrome kicks in, this “wide but shallow” approach to career development really bites. I feel like I should know a lot more about, say, scalable server infrastructure for a guy who’s been developing products for seven years.

Don’t get me wrong, I know the basics (if not more than that) for most of the subjects in my field. My problem is that I want to master them.

But I enjoy every aspect of what I do right now. I love writing server side tech as much as I love working with some awesome CSS framework. Or creating mobile experiences. And selling to clients. Or pitching to journalists. And there’s the rub: spreading myself so thin on all these different tasks means becoming an expert on them is impossible within my given timeframe. It’s a tradeoff that I’m just going to have to embrace.

When I came to this conclusion, I tried to imagine what my future might be. If I was an expert in, say, machine learning (which would probably take a lot longer than seven years but whatever) then I could probably get a sweet job at the likes of Facebook and get well paid for it. But Facebook wouldn’t look twice at a generalist like me – in fact, the best tech companies probably wouldn’t.

But there’s an upside. Being able to build, design, market and sell your own product gives you a freedom that a high-profile job or perceived guru-status never could. To be able to identify a market opportunity and take it all the way from “git init’ to selling to over 7,000 paying customers (not recurring but whatever, it’s a start) is pretty neat.

Of course that’s not to say that there’s no company out there who would hire me – when my last startup went bust I had more than a few job offers (and still do because I know a few awesome people around the tech startup scene in Dublin).

I guess what I’m trying to say is that in my line of work there’s a certain liberty in not being “any one thing”.

Interested to hear feedback on this and any stories of people who have been there/done that when it comes to this stuff.