Wednesday, November 09, 2011

There Must Be A Glitch In The Matrix Because I Just Perfectly Experienced Programming Serendipity

Sunk a lot of time researching and then trying to implement Mac OS X application sandboxing today, only to strike out. While watching one of the WWDC 2011 sandboxing videos for the third time, I saw Apple Events mentioned in the presentation. Slowly realized I was looking at the clue I needed to unravel a feature for SleepMute that I've poured many man-days of research into. With that clue, the rough version of this new feature was done in 40 minutes!

If that's not programming serendipity, I don't know what it is.

Tuesday, October 18, 2011

Implementing Objective-C Protocol Inheritance, Usually For Delegates

In a previous post, I explained why an Objective-C protocol might not implement respondToSelector:. Prior to Xcode 4.x, Apple's protocol template code didn't include the <NSObject> protocol in custom protocol definitions. This let to believe that you couldn't setup inheritance chains for protocols, but but you can!

This is usually important where you have a UIView subclass that's the base class for a bunch of subclasses. All instances must support the same message back to a delegate pattern, but subclasses might need to add more delegate callbacks than the base class should define.

Consider this class & protocol definition & class implementation:

@class VehicleView;

@protocol VehicleViewDelegate <NSObject>
- (void)vehicleView:(VehicleView *)vehicleView didTapToStartEngine:(Engine *)engine;

@optional
- (void)vehicleView:(VehicleView *)vehicleView didTapToOpenWindow:(VehicleWindow *)vehicleWindow;

@end

@interface VehicleView : NSObject {
 id<VehicleViewDelegate> _delegate;
 NSArray *_engines;
 NSArray *_vehicleWindows;
}

@property (non atomic, assign) id<VehicleViewDelegate> delegate;
@property (nonatomic, retain) NSArray *engines;
@property (nonatomic, retain) NSArray *vehiclesWindows;

@end

Now the implementation

@implementation VehicleView

@synthesize delegate = _delegate, engines = _engines, vehicleWindows = _vehicleWindows;

//Lots of other stuff to implement!

@end

What you can see there is a pretty typical class implementing a protocol then declaring a delegate instance variable and property pattern. But what does this look like when we want to subclass?

@class TruckView;

@protocol TruckViewDelegate <VehicleViewDelegate>

@optional
- (void)trunkView:(TruckView *)truckView didTapUseFourByFourMode;

@end

@interface TruckView: VehicleVIew {
}

//Subclass specific implementation

@end

Notice there's no delegate definition, what gives? That's true, but we are going to reuse our base class' instance variable for this:

@implementation TruckView

#pragma mark - Properties

- (id<TruckViewDelegate>)delegate
{
 return _delegate;
}

- (void)setDelegate:(id<TruckViewDelegate>)delegate
{
 if(_delegate != delegate) {
  _delegate = delegate;
 }
}

@end

So what's going on? The TruckView subclass overrode VehicleView's syntactic sugar property method declarations for delegate with the protocol definition that it wanted, but reused the base class instance variable. This allows TruckView to verify it has a pointer which implements the protocol, eliminates any dual-protocol declarations and management in TruckView, and maximizes protocol definition reuse.

Next time, I'll show how to optimize detection of @optional protocol methods so setting up @protocol inheritance hierarchies don't slow down performance.

Monday, October 10, 2011

A Tale of Computing Misery: How To Fix Seagate GoFlex External Drives Randomly Ejecting in Mac OS X 10.7 Lion

I don't know if I'll ever buy another Seagate drive. This is a tale of 3 months of computing misery.

Rewind

When I saw the news back in early July that Mac OS X 10.7 Lion had hit Gold Master (GM), the expected release to paying customers, I immediately downloaded and installed on my main machine, an iMac.

A few days later, I filed Radar 9727925 (OpenRadar link). Lion was randomly automatically ejecting my external Seagate HD. This had never happened under any version of OS X 10.6 Snow Leopard. I had all my code on this drive, the iMac's internal drive was near full, as well as a ton of media files I keep the Apple TV feed with. Also, all my photos were on this drive. I figured it was an obvious bug, some bizarre oversight, and should definitely get fixed. I thought it was bad enough that 10.7.0 would get the dubious distinction of needing GM2.

That didn't happen. 10.7.0 shipped as expected, and my external disk keep ejecting with a scary modal error dialog like clockwork. I was frustrated. My excitement for Lion was high coming back from WWDC 2011, but I had staid my hand on the primary machine until the OS reached GM. Of course, I had installed to a different external drive to do testing, and it was great on my iMac, I thought I was home free.

But I couldn't have been more wrong about Lion, or at least I thought for three months…

The Long Wait

After filing my bug, Apple responded before 10.7.0 shipping asking me to run a disk-debug command and capture the trace of what happened during the random ejection. I collected the information and supplied it to Apple. I eagerly awaited a response, but none came.

I installed 10.7.1 as soon as it came out, hopeful my bug would be stealth resolved. It wasn't and I grew despondent. How could Apple leave this bug unfixed? I searched the Apple Support Communities. Clearly other people were affected by this. This was the worst Mac OS X bug I had experienced since I started using it with 10.3.x.

As soon as the first 10.7.2 builds started arriving in the Mac developer center, I deployed it. The iMac I am putting this on is the whole house iTunes media server, I was risking wife and kid wrath making this move. They do not tolerate beta. Early 10.7.2 betas didn't resolve the bug. I started to panic. Could 10.7.2 make it through development without this issue getting fixed?

Desperation

If you've ever been to an Apple developer event, you know they say if you have any questions just email them. I've always admired that, and wouldn't know if I would feel comfortable if I were in those shoes encouraging public emails for help.

On August 30, 2011, I emailed Michael Jurewitz out of desperation:

Hi Mike,

Can you figure out what the real status is of Radar 9727925? In short, the external drive where I keep a huge chunk of my files, including my code, keeps randomly ejecting a dozen or more times a day. Never happened in Snow Leopard.

I hated to even think to ask you, but I filed this bug close to 3 months ago and it's driving me crazy. 

If anything it's gotten worse in the latest 10.7.2 seed. Yes I am on the bleeding edge hoping this one bug gets fixed.

Thanks so much, Dave

I couldn't stand sending that email. I just about cried when I got the automated Out of Office reply. What was I going to do now? I remembered that I could email Apple and ask for a status update on the bug. I did that and hoped for the best, but I couldn't take it anymore, time to start deep debugging to prove whether it was anything specific to my environment.

Taking Matters Into My Own Hands

Deep Debugging OS X is really not that complicated. Here's the rough outline:

  1. Verify your System disk for any corruption
  2. Repair Disk Permissions
  3. Create a new user account and see if you can reproduce the bug
  4. Install a new copy of the current OS on an internal disk partition and see if you can reproduce the bug
  5. Remove anything 3rd party that starts up with the OS one at a time until the bug stops such as:
    • StartupItems
    • LoginItems
    • LaunchAgents
    • LaunchDaemons
    • Kernel Extensions (.kexts)
  6. File a Radar or GTFO
I had started with number 6 because the bug seemed real clear. I installed Lion, external disk started randomly ejecting, case closed it's an OS bug!

But I started going through the whole debugging sequence just to be extra double sure this wasn't specific to my environment. I executed steps 1-4 and reproduced the bug. I was even more certain this had to be a Lion bug. With a bare OS X install, the external disk was randomly ejecting. This is the smoking gun right? Wrong!

I went through all the 3rd party code in step 5 that was loading on my system. Something interesting caught my eye. Lion wasn't loading some Seagate kernel extensions since they didn't include 64-bit versions. I went out to Seagate's site, and found GoFlex for Mac 1.1.2, but I didn't install it. Normally when debugging something, you try and remove 3rd party stuff to fix a problem, you don't add it back in. Besides, when I skimmed the description of this software, the usefulness of the package seemed limited to Drive Settings and Diagnostics, the only capitalized words in the summary that weren't MacOS or GoFlex. I'm sure I was trying to figure this out one night bleary eyed well past midnight, so I was probably in a hurry due to exhaustion and didn't feel like reading the paragraph, my mistake! I put the problem on hold for a few more days.

The Workaround

A few days go by, I install the latest 10.7.2 beta without it resolving the bug, I brainstorm on what it could be. During one of these sessions, it dawned on me that it looks like the drive is getting ejected if it's not being used. There is some kind of sleep timer being fired! I check the Energy Saver System Preference, but Put the hard disk(s) to sleep when possible is unchecked. This must be where the bug is, Lion is always sleeping the drive when possible but there is some incompatibility, thus the modal error dialog I reasoned.

I hadn't really done much with Automator, but I figured this was the kind of thing it might be good at. I whipped up a quick workflow app that did a search for a string in file names on the external drive, and had it open as a LoginItem. Voila! The external drive stopped ejecting! It was a sleep timer. The relief was immediate, it was like I just scored a touchdown, or for the non-sports inclined, like I had just solved an incredibly difficult puzzle. I could wait as long as it took for Apple to fix this Lion bug now, I wasn't really impacted anymore and I stopped thinking about it. Only it wasn't a Lion bug at all, not really.

Finally, The Solution

On October 3, 2011, Apple responds to my request for more information on the bug. I don't know if Michael Jurewitz was the invisible hand that got this moving, or it was just my place in the request for info queue, but I was excited when I received the mail. I paraphrase because of NDA restrictions. The message thanks me for my patience and told me that to fix the issue I must install GoFlex for Mac 1.1.2! How is it possible that to use a USB or FireWire (the GoFlex does both, reason I bought it) in OS X Lion you need 3rd party software? I then went back to the Seagate site and read the description of this software again, and it hit me like a shot of vodka:

drivers to disable the built-in sleep timer on the drive
I was floored, and both right and oh so wrong. It was a sleep timer, but I was digging in the wrong place! I'm sorry Lion and Apple, you weren't at fault. Why would Seagate build something like this into the drive? I can understand building in a lower power mode, but not one that causes the second more popular consumer operating system in the world to flat out not work without an otherwise useless driver. Worse, there are at least three pieces of code launching in OS X at startup that are from Seagate, but none are any kind of automatic update. I bought this drive in 2010, it's not a holdover from the Windows 95 era! Automatic update is table stakes for anyone shipping software, even more so on Apple products. Either you ship your apps through the App Store which gives you automatic update for free, you roll your own, or GTFO! All I wanted was external storage that just worked with OS X, instead I've acquired another piece of crap recurring task like I had to poll the Dell site in '95 for updated drivers. Greeaattt!, I've added an event to iCal and made this a WebClip in Dashboard, my first!

Friday, September 30, 2011

SPOILER ALERT: Astoundingly, I got a bit choked up by Gears of War 3

I haven't finished what a lot of people are calling a big & dumb shooter, the Transformers movie franchise of video game series, but I just got past a moment I think Gears of War 3 deserves some recognition as…artistic. Sure the games' loaded with ridiculous one liners among other sins, but the following single moment means this game and the whole series has more emotional depth and artistic merit than the Shia Labeouf Running and Shouting Optimus series.

Spoiler Alert
At the end of Act III, lead character Dom sacrifices himself to save lead character, his brother to the end, Marcus Fenix. This follows soon after Dom has said his peace to his dead wife and kids, so you think Dom's OK, he's going to be there to the end. But when you see what's going to happen, when it's clear Dom is going to sacrifice himself, the sequence really catches your attention because it's atypical for this kind of thing, and it's all because of the music. The game pulls the usual cacophony to barely audible levels and plays this:

You might remember that (with lyrics) from this Gears of War 2 ad which was mighty attention getting for being atypical for video game ads

Maybe I'm just a sucker for anyone willingly giving up their life to save their buddies, but I couldn't help but get a bit choked up playing to and then watched the cutscene where Dom sacrifices himself.

Thursday, September 29, 2011

A Personal Note About Why I'm Excited To Teach At CodeLesson.com

My younger brother used to call me Doctor Murdock or "Doc" Murdock. Seems like another life know, but I was a baseball pitcher until the end of high school. I always had a pretty mean fastball and wicked curve ball. I struck out a lot of batters. Dwight "Doc" Gooden was the NY Mets ace when I was in little league and he struck a lot of people out. Since I was a huge fan, my brother liked using Gooden's nickname for me and it stuck. That wasn't the only reason he coined that nickname for me. Since I was always trying to teach people around me the stuff I was learning, the name fit both ways. To be fair, my brother also in his best Han Solo impersonation yelling at C-3PO called me "The Professor" too. I loved both nick names.

I liked the sound of "Doc" Murdock so much in and out of baseball that I seriously considered becoming a medical doctor. I enrolled in AP Biology in high school but couldn't make it past frog dissection!

I still thought about becoming Doctor Murdock for real. Since I was heavily into computers through high school, I started thinking about becoming an actual professor. When I got to college for my bachelor's degree in computer science, I enrolled in the Honors program to try and position myself for moving on to higher education. I graduated with honors in comp sci, but I never went back to school for a masters degree. Building apps and life were just to much fun to go back into the ivory tower! I thought the door to becoming Doctor Murdock was closed forever.

When CodeLesson founder Jeffrey McManus asked me who would be good to teach iOS development, I really wanted to do it, but didn't have time in my schedule. Once my schedule freed up, I consider myself lucky that Jeff hadn't found an instructor yet. Sorry Jeff! He graciously accepted my offer to teach Introduction to iOS Programming.

The door to becoming "Doc" Murdock feels like it just cracked open, and it feels great to finally pursue this dream, even a little bit.

I'm Teaching Introduction to iOS Programming at CodeLesson.com Starting Monday October 3rd

If you or someone you know is interested in learning iOS programming, then head on over to Introduction to iOS Programming at CodeLesson.com to sign up. I'll be teaching this course starting Monday October 3rd, 2011. What's CodeLesson?

CodeLesson provides hands-on, practical, online instruction. Our instructors are real people with real-world experience. Because our courses are given online, you have flexibility. You can access your course 24 hours a day while your course is in session. This means that you can participate fully even if you've got a busy daytime schedule or if you're located in another time zone.

You can read the course description, prerequisites, and outline at CodeLesson to see if the course is for. I'm using iOS Programming: The Big Nerd Ranch Guide as the textbook for the course. The week to week work goes like this:

  • I assign unit(s)s to complete for each week. Units contain reading chapters or articles on the web, typing code into Xcode, or environment setup
  • Students complete each unit and turn in their work for grading
  • There are occasionally quizzes
  • Repeat!
If students have questions, I answer them in a timely fashion through CodeLesson.

I've always enjoyed teaching, and now I get to do it in a structured format using my favorite development tool stack ever! I'm really looking forward to it

Friday, September 16, 2011

5 Reasons Why Windows 8 Tablets Are Going To Have a Tough Time Competing Against iPad

This post won't be about what I think of the Windows 8 Metro UI, I haven't spent enough time with it to think through Microsoft's choices. This post won't be a feature comparison of previous versions of Windows to Windows 8 Developer Preview, that would be somewhat ridiculous with Windows 8 not shipping for a year This post is about how Windows 8 tablets are going to have a hard time competing with the simplicity of buying an iPad for these simple reasons:
  1. Users have to choose between two completely incompatible CPU architectures to buy a Windows 8 tablet.
    I can think of no examples where a consumer facing device had two incompatible CPU formats for normal people to choose from that did well. Even when Windows NT was shipping for Intel, DEC Alpha, and a few others for enterprise customers, getting supported apps was a challenge. I remember because I was trying to run an Exchange Server on Windows NT for Alpha, it was always like punching yourself in the face.

    Consumers will have to understand that "Metro style apps" (is that final marketing?) will work across CPU architectures, but all existing Windows apps do not. That's a terrible choice for regular people to have to make. By contrast, you get an iPad and you can run iPhone and iPad apps.

  2. If Windows is in the final tablet product name, then where have the fraking Windows gone?
    Windows tablets have been around for close to 10 years and never sold well. I had one of the early Compaq tablets, it ran hot with poor battery life using an Intel CPU. The name of the OS used to be Windows XP Tablet PC Edition. Since Windows 8 is going to run on notebooks, desktops, servers, and tablets just like Windows XP and its various editions, how are Windows 8 tablets going to be easily distinguished from other Windows 8 based computers? Dell Tablet 2012 with Microsoft Windows 8 for ARM Tablets? HP QZY12 with Windows 8 Intel? Compare again to iPad, iPad 2 with iOS 5, and the sure to come iPad 3 with iOS 5.2 (numbers just a guess, but a pretty good one). Obviously Metro on Windows 8 looks nothing like previous versions of Windows, except when it inevitably will, but when it doesn't it won't have any Windows on something named Windows.

    You can just imagine the commercials where Apple or even Google if their still shipping tablets shows a Dell Streak 2012 with Windows 8 for ARM CPUs and the Desktop environment.

    Hindsight is as they say 20/20, but I'd take your bet that Windows Phones would have sold better without Windows in the name. For a lot of people, when people sit down daily to work and see Windows XP, that's what they think of as Windows. They don't want that on their tablet!

    Even Apple has had this problem to a small degree with a simple two word name! How many times have you heard the iPod touch called iTouch? If Microsoft can't simplify the naming, they're going to lose sales because complexity is not a winning formula.

  3. There will be very few "Metro style apps" for Windows 8 for a long time.
    There are 0 Metro apps in existence. Windows 8 won't ship for a year. Make no mistake though, WinRT, the Metro app runtime is Microsoft once again trying to build an entirely new platform. The closest developers are right now to shipping Metro style apps, not even using WinRT, just the Metro visual design, are on Windows Phone 7.x, and the phones aren't selling well. Where's the evidence that consumers will actually buy Metro style apps?

    Developers have 1 year to build Metro apps for Windows 8. There will certainly be Metro apps when Windows 8 ships because hundreds of millions of copies will be sold. How many copies are going to be on tablet devices instead of Microsoft's traditional PCs? No one knows. The only data currently available on consumers preference for the Metro UI are Zune and Windows Phone sales, both doing poorly. There isn't going to be a halo effect for Metro apps for a long time if ever. There are going to be a lot of developers on the sidelines until Microsoft can actually sell, not ship, sell tablets in volume.

    Microsoft has previewed Windows features early for years from a position of strength because they were the only game in town. But the game has changed. They have zero tablet apps and must get as many developers on board now or the Windows 8 launch or the game is over.

  4. Windows 8 and PC tablets aren't competing against iPad 2 and iOS 5, there competing against iPad 3 and iOS 6
    iOS 5 ships in weeks for all existing iPads and 2-3 years worth of iPhones. Very few people outside of Apple know what iPad 3 will be. Most likely only Apple knows what's going to be in iOS 6. Sure, it may not be iOS 6 that ships around the same time as Windows 8, could be a 5.x, but I bet it will be iOS 6. Will Windows 8 with Metro look fresh against iOS 6? Google will ship a bunch of new Android releases between now and then too. The ship in Redmond is too big to seriously change course and still ship Windows 8 in time for Fall 2012.

    Fall 2012 will be one of those historical inflection points, like the launch of Windows 95 and iPhone, that technologist will remember as long as they live. Microsoft is either going to flame out or curtail Apple's incredible tablet growth. I seriously believe this is Microsoft's last chance to remain relevant. The mobile train has left the station and is accelerating, but Microsoft hasn't even finished their travel plans!

  5. Apple could have an App wildcard up it's sleeve
    I believe iOS and OS X will stay merged at the lowest layers, but at the UI layer they stay separated. There are certainly ways Apple could make OS X post-Lion even more like iOS without them truly merging. One thing I think Apple could do is merge the iOS and OS X apps stores and make iPhone, iPad, and Mac binaries in one app package the new Universal format. Forget legacy vs. Metro style apps in Windows 8, with the App Store, you could buy an app for use on all your Apple devices. Apple's put multiple binary images in one app bundle for years and OS X apps are getting more structured like iOS apps with sandboxing this year. I've actually been surprised this hasn't happened all ready. This would be fairly easy to do and makes whether an app runs an on any Apple device an implementation detail user's don't have to worry about. Or Apple could allow Cooca Touch apps to run directly on OS X by consumers since they already do in the iOS Simulator, but I think this is the wrong thing to do since iOS apps usually can't handle the multitude of screen sizes on Macs.

To be clear though, I hope Microsoft eliminates more of this complexity and can avoid unforced errors to compete effectively with Apple. One technology company with monopoly power is not a recipe for moving technology forward as seen during Microsoft's long reign as king of PCs. Apple products are what I use and develop for today and for the foreseeable future, but if the conditions are right, switches have to be made. After all, I was developing on Microsoft platforms for 10 years.

Tuesday, September 06, 2011

QuickTime X on OS X Lion 10.7 Been Acting Like It's Possessed By A Mischievous Spirit? Here's The Fix.

Have:

  • QuickTime Player window titles been unclickable?
  • Control HUD unmovable?
  • Creating screen recordings all but impossible?

Then call the QuickTime X Busters! We're ready to believe you!

The ectoplasmic residue that's been haunting your Lion install is the QuickTime Player X Preference Pane and the preferences it created. Back in the OS X 10.6 Snow Leopard days, this handy utility gave you the ability to configure certain hidden preferences that you might like, such as automatically playing a video once the file is opened.

This utility, as developer Megabyte Computing Solutions says is 10.6 only, but that doesn't help when doing an inlace upgrade. To fix QuickTime Player in Lion then, you need to trash the preference pane and any old preference files in ~/Library/Preferences. Quit and open QuickTime Player if it was running and BOOM, no more busted QuickTime Player.

I'd chalked up this behavior to simple beta and then 10.7.0 bugs. But it finally occurred to me today that there is no way  ships QuickTime Player in Lion that busted, then I figured this out.

Tuesday, August 30, 2011

Feature Requests For All Twitter Clients: Automatically Block & Report Obvious Spammers

Twitter itself seems unable or unwilling to stop obvious spammers.

Everyone using Twitter has seen this, mention a hot product like the iPad or iPhone, and BOOM your timeline fills up with spam from bots that have zero followers and follow zero accounts.

Tonight it hit me, why not have the Twitter client filter out and automatically report spammers? Even if Twitter eventually starts catching spam at the server where it should be caught, no reason why clients can't iterate quickly to stomp out this scourge now.

I envision this feature being like a muting feature, but instead of muting on keywords, muting happens based on account metrics. Then the client could delete obvious spam tweets out of the main timeline or put them into a spam timeline.

The user should be able to take it one step further and set the client to automatically report as spam the obvious 0/0 accounts.

Monday, August 29, 2011

Blog Update: Why I'm Pulling Ads

I can't believe I've been writing this blog for 7 years! I had no intention when I started writing of showing ads. Most ads are crap & I hate crap ads. But I figured I would give it a try and see what kind of revenue I brought in. I wrote about my first results in this piece $100.39 in only 1,307 Days. That was on March 28, 2008. I've been writing less on the blog since Twitter came out, like a lot of people, and I've been slowly climbing back to the magic $100 USD payment threshold. I'm currently inching up $89 USD.

I've been thinking about it for a while, but Dave Winers' post Indirect business models FTW really got me moving on doing it with this quote:

I say don't put ads on your blog, they just pull attention away from the ideas you're writing about.

As soon as I read that, I knew I had to pull the ads soon. It just rings true for my kind of blog. But I held back, I wanted to replace Google Adsense ads with banners for my company Tangerine Element and my apps iTimeZone for iOS & SleepMute for Mac OS X. I figured if anyone wants to say "thanks" for something they read, just click through to an app & buy it.

Then while re-reading some of my older posts to write something new, I saw this:
Not Ads From Dell

Of course I knew this was going to happen, but I'm not advertising for Dell! WTF!?! I in no way endorse using Dell computers any more than I would endorse getting a hammer and banging on your skull to cure a headache.

I'm done, the ads are gone. When I get some time, I'll put up my own banners, but better to stop the damage first.