Tired of having to find out where an exception occurred, that was only captured in the main()? Now I’m going to show you the best Xcode tip that no one talks about. Don’t ask me why.

These are the very few steps that will save your life and make your day better.

Creating an “Exception Breakpoint”

The first thing to do is to access the Breakpoint Navigator, which is the little breakpoint marker on the top of your Project Navigator. If you didn’t know, the Breakpoint Navigator show all your breakpoints in your project, where you can easily manage them. (Click on the image to see it bigger)

best-xcode-hint-1

Now that you’re in the Breakpoint Navigator, click on the little “+” button on the bottom left of the screen. A little dialog window will show up and choose the “Add Exception Breakpoint…” option.

best-xcode-hint-3

Then, the Xcode will ask you some information about the breakpoint you’re creating:

best-xcode-hint-4

  • Exception: There are three options: “All” to get all types of exceptions; “C++” to get only C++ exceptions; and “Objective-C” to get only ObjC exceptions. I like to leave it as “All”.
  • Break: Leave it as “On Throw”. This way, the execution will stop when your code throws an exception and not when it catches, which is the default on main().
  • Action: You can set some action to be performed when the breakpoint is reached like run an Apple Script, shell command and others. I’ve never used it.
  • Options: if you check this option, your code won’t stop when the breakpoint hits, making it useless. So leave it unchecked.

Congratulations! You’ve successfully created an Exception Breakpoint that will stop whenever something odd happens! Let’s try it out!

As an example I’ve created an empty array and tried to access the object at index 0, which doesn’t exists. This is what happened:

best-xcode-hint-5

Whoa! The Xcode stopped precisely where the problem was! Quite awesome, huh? But, sometimes, it’s not that easy to visualise what the problem really is, so if that’s happening to you, just hit “play”, and the code will keep executing as if your breakpoint doesn’t exists. What happens now, is the main() catching whatever exceptions that may happen.

So you can look at the console to see what the problem was:

best-xcode-hint-6

”-[__NSArrayI objectAtIndex:]: index 0 beyond bounds for empty array”

Ta daa! I know it’s not perfect. It won’t get any signals (SIGABRT, SIGTRAP), flags or bad access kinds of witchcraft, but I think it’s super helpful and I think you guys should know.

SQLite sucks. I think everybody knows, but I had to say it. But, sometimes, we have no choice other than using it. Even though it’s horrible and we know we`ll be frustrated, once in a lifetime, SQLite may be the best choice. Wether developing a program with a tiny or fast persistence. Sure there are other alternatives that may suit you best, but the platform we’re using may not support it.

For instance, I’m currently working on an iPad app that will have a fairly big amount of data which must be available offline and it will have to be updated sometimes. The best solution we came with is a background server task wich will build an SQLite database populated with the most recent data and, when the user requests, the iPad will download the whole database, overwriting any existing ones.

I’m no DBA, but I don’t think anyone writes database creation scripts anymore. At least not for the usual CREATE TABLE, etc. I’m mostly a MySQL guy and, for a long time, I’ve been using MySQL Workbench to create my DB models and syncing with my existing database. It automatically creates everything that needs to be created and alter all tables that must be altered without losing any data (most of the time). It’s pretty good and I never had any problems with it.

Now, back to listing reasons why SQLite sucks (dude, you can’t add a constraint when altering a table!). As any platform/framework with no “official” team, SQLite depends on the community to increase it’s awesomeness. Therefore, as everybody knows SQLite is not awesome, no one really waste time developing a good administration tool for it. What may happen, is a company own a DB management program (such as Navicat) and something like this goes by:

- Hey, we're supporting MySQL, SQL Server, MongoDB, Cassandra, NoSQL and Postgree,
don't you think people will notice we're ignoring SQLite?

- God dammit *gasps*, add support to it too... but don't lose too much time with it!

Unfortunately, there are no really good tools to work with SQLite available. “Hey, what about Navicat?” It’s okay, but I found tons of bugs in the Mac version. If that’s not enough, are you really willing to pay to use a management tool for a free, open source database? But if you search deep enough - like the 3rd page of Google - there are also some weird stuff that appears such as SQLiteStudio (for Mac). When I see things I like that I think “why the hell would people lose time developing something for SQLite?” But hey, there are weirdos for everything, so I’d like to thank for the SQLiteStudio team for being strange enough to create it.

Even though it’s not the best tool ever, SQLiteStudio sure comes in handy and helps a lot, but it doesn’t solve the mentioned problem of modelling your SQLite database and not dealing with SQL scripts. So, searching for something that would fill this void I found this excellent script in Lua for MySQL Workbench that automagically generates the SQLite CREATE script from a model. It’s super easy to install and use. Just follow this steps:

  • Download the .lua script (duh!)
  • Click in the “Scripting” menu in MySQL Workbench.
  • Click “Install Plugin/Module”.
  • Choose the downloaded .lua script.
  • ???
  • Profit.

That’s it. Now, if you want to try, open a model, click the “Plugins” menu and under “Utilities” there must be an option named “Export SQLite CREATE Script” which may be the first one. Just click it and choose where to save the .sql script and execute it. Voilà! If you have any problems executing the generated script, you may need to comment the BEGIN; at the top and the COMMIT; at the bottom of the .sql script.

Super easy, isn’t it? Give it a try. Even if, like me, you hate SQLite. The plugin solves the modelling problem, but creates a new one, because you can’t ALTER an existing table since it only generates the CREATE script. But if that’s not a problem go for it and be happy. When I found it, life suddenly became joyful again.

“Once upon a time, there was a tiny invisible force named Luck. Everybody loved it and gave it gifts, hoping that, when needed, Luck would come for them. But Luck had a younger sibling called Bad Luck, who hated Luck with all it’s strength and always tried to be the opposite of Luck, because, while everybody smiled and cheered for Luck, no one liked when Bad Luck was around. People twisted their noses at Bad Luck and everybody blamed it for every bad thing that happened in their lives.”

This is that time of year when joy and happiness take over people. Some folks are going on vacations and we won’t see again for a while, and every single one of them, without exception, while saying goodbye, wish “happy holidays, and good luck!” If there’s one thing I can’t understand is this misconception people have on luck and bad luck. I see all those people on new year’s eve wearing white, jumping waves, and doing all kinds of foolishness hoping it “will bring them good luck” and I can’t understand.

Luck isn’t something you can “force” or hope to come. “Luck” is the name we give for a chain of events that, coincidentally, affect you in a good (luckily) or bad (unluckily) way. Let’s picture an hypothetical scenario: you’re riding a bike and, right before you cross another street, you hit a pedestrian. Was bad luck that put that asshole there, right? Now, what if, one second after you hit him, a car goes by at 60mph crossing the street you are at. Not so bad luck hitting that good citizen now, huh? So, although the result is the same, the denomination we give to that chain of events changed because of one single variable: some douchebag driving his car while talking on the phone. So, what was bad luck for you, became luck. But what about for that poor guy you hit? For him, independently of the result, he’s still pretty banged up, so he had bad luck. It didn’t randomly pointed at him and said “you, sir, are gonna get screwed!” It did just happened. Bad Luck. When you do something that goes badly and you knew the odds were against you, that’s not bad luck. It’s not the universe flipping you the finger and screaming “fuck you, Jimmywobble!” That’s stupidity. YOLO my ass!

I not always wear white on new year’s eve, but when I do, it’s not because I think it will bring me peace in the upcoming year. It’s because I want to get into the spirit of the party. Those thousands of people in white, happy, drunk and with the promise of better things to come are what drive me to join them and put on a white shirt.

But don’t think I’m crazy just yet. If I’m walking down the street and I see a ladder landing on a wall, of course I’ll pass around it. Not only I do not want anything to fall on my head, but hey, what if I’m wrong? Why would I bet against it when I don’t know if it will get angry and come back to bite me in the ass?

So, in this year’s ending, I wish that every single little incident that happens to you in 2013 may be chained to good fortune. I hope that slam you’ll give with your pinky finger in your table helps you find that earring you’re looking for for three years. Or that tongue bite you’ll have may lead you into meeting some super hot nurse.

So I wish you well in the new year. I hope you all the best.

And good luck.

Fork me on GitHub

One of the main reasons I like Django is it’s awesome auto-generated admin interface. It’s  just awesome. When you start to play with it and discover apps like django-admin-tools, the faded jq-mobile and other cool  stuff you realise how versatile Django really is.

The Context

When I started learning Django I created a small manager for some of my hobbies. The first one was a book manager in which I have a log of every single book I’ve ever read, when I read it, where the book is located, if it’s  borrowed to somebody, a “GIVE IT BACK TO ME” button that sends an email with that exact message to whoever has it, and so on. Among everything, I also added a field for the book’s cover but I never really used it.

More recently, I restarted to improve my card deck collection (yes, I’m weird) and added this new feature to my Django manager. But this time, since it’s cataloging a collection and decks are mainly visual, the photos of the decks are  very important. Yes, I said imageS. Each deck has five photos:

  • The Front of the Box
  • The Back of the Box
  • The Back of the Cards
  • The Joker
  • The Ace of Spades

There are decks with a different back for each card, but that’s not important,. Those are the options I’m contemplating. But, since not all of them are opened (I have one numbered deck that goes to 2500 copies. I won’t open it. Never!) only  the first two images are requirements.

So, now that you are aware of the context, this is when the problem (and the fun) started.

The Problem

I started looking for some Django solution that could easily show thumbnails for my images in my admin module. Guess what was my surprise when I realised there was N**one.

There was absolutely nothing that would do what wanted and needed. The closest things I found were the automatic thumbnail creator sorl-thumbnail and Ace Devs solution  which didn’t solve my problem. Dude, my images have 1936x2592 pixels of resolution. I can’t just show them as they are.

That’s when I realised I would have to develop a new solution for that.

The Solution (aka The Fun Part)

I’m a modest guy, I don’t like bragging about what I do.

No forget it. Bullshit. I do like to show everyone what I did. Specially when I think it’s awesome, I mean, It’s awesome. Don’t you like sharing awesome stuff with your friends? Dude, I’m even writing an article about it! In TWO languages!

So, what I did was basically to join and improve those two solutions I found. “Basically” because, in the deep, it was a little more complex than that.

I came up with the stupidest yet simplest name I could find: django-admin-thumbnail. I know, it’s über stupid. But it’s also simple, so it works.

django-admin-thumbnail

The package automatically works on your list_display of your ModelAdmin. It automagically detects your ImageFields, converts it to thumbnail (physically allocated) using sorl-thumbnail and displays to you in a user friendly way.

This is how it looks when you hover your cursor over an ImageField in your model grid.

The image is in portuguese. Imagem = Image, Abrir Imagem = Open Image. django-admin-thumbnail is localizable but, currently, only pt-BR and en-US are the officially supported languages. If you want it to be in your language, just send me your .po file and I’ll validate it and share. It’s ridiculous, there are only two tags. Give it a try!

When I finished, I realised how simple and effective it was. I had to share it with the community. So I took the opportunity to create my first python package. It was a pain in the ass because the documentation is lame, specially the long-description part. I had to generate it a zillion times to get the reStructured Text working until I found the py2html  command.

Wrap and Finish

That’s it. If you want to know more about django-admin-thumbnail  just click the link and it will send you to the Github repository. There’s a quick start guide. Here’s the link for the python package

If you have any trouble to use it, don’t hesitate to send me a message.

Have fun!

Hi there, stranger.

If you found this blog, you’re probably my friend or already know what I do for a living. But anyways I’m a mobile app developer (I mean, iOS developer), Python fan and Django explorer.

In this blog, which I’ll try to update at least once a month, I’ll primarily talk about my approaches in some aspects of my developer life, but if I digress and start talking about karting, movies, cooking or anything, please, bare with me  for I’ll eventually come back to topic – or not. Those are some of my current hobbies and, in the future, more wil come and some will be forgotten.

That’s me. Oh, btw, my name is Flávio (duh!), I’m 22 brazilian and you can reach me at blog@flaviocaetano.com.

These are some of my links you may find interesting:

That’s it for now. Soon will finish the next article I’m currently working on and, when I’m satisfied – which will probably be never, but I’ll post it anyway – we’ll talk more.

See you next time.