Implementing GCM Network Manager for periodic network requests on Android

In the process of rebuilding What’s Shaking, NZ?, I needed to implement a periodic network request (literally polling an API). I wanted to use the new Job Scheduler API, but unfortunately, this is only available on API 21 and above. Luckily we can get similar functionality by using GCM Network Manager, as suggested on StackOverflow. Note that the GCM Network Manager actually uses Job Scheduler behind the scenes in API 21+.

The documentation for this is somewhat hand-wavy. Here I attempt to provide a true step-by-step guide to implementing this. I assume you’re not already using GCM for something else in your app (as that was the case for me).

Read on →

Handling RealmMigrationNeededException on a fresh installation on Android

Back in November, I had just started using Realm on Android and was having some troubles. I’d occasionally need to make a model change, and being early on in development I was happy to just delete the Realm and start again - in production you’d want to perform a migration so you don’t lose any user data.

However, the “delete and re-install” approach wasn’t working as expected - I kept getting a RealmMigrationNeededException:

1
io.realm.exceptions.RealmMigrationNeededException: RealmMigration must be provided

This doesn’t make sense! I should be able to uninstall an app and upon reinstalling it I should have a fresh slate to work with. As it turns out, not quite. I couldn’t figure out why at the time, but the workaround was to simply add a deleteRealmMigrationIfNeeded() call to my Realm configuration when building it. I made a note to deal with this before release, and carried on my way.

Read on →

Automating iOS build numbers

I recently encountered some problems where I was accidentallly duplicating build numbers for our iOS app. At Bridgit we’re big fans of automation, so I went about finding a way to avoid this human error and let the machines do the work for me.

Read on →

A bug in (and a fix for) the way FragmentStatePagerAdapter handles fragment restoration

Ever used a FragmentStatePagerAdapter? We’re using one at work for our ticket purchasing wizard. The user enters the wizard, and can progress to the next page once they’ve completed the current one. We control this by manipulating the stack of pages and notifying the adapter that the data has changed when a new page is available.

Unfortunately, when changing pages that have already been loaded, there’s an unexpected bug. Specifically, when you load a page, remove it and then insert a new one in its place, the next time the fragment at that index is loaded, it receives the savedInstanceState bundle for the old fragment.

Read on →

20 Things, Week 20

Week 20 of my 20 Things in 20 Weeks project. I’ve finally made it. So, how did I do?

Read on →

20 Things, Week 19

Broke my Github streak this week, at 31 days - in fact, I didn’t get any programming done this week. My partner and I left Auckland on Wednesday afternoon and didn’t arrive home until late Sunday evening. We travelled around the Coromandel peninsula and spent time climbing mountains, visiting beaches, doing touristy things - and we got engaged! I’d say that that’s a pretty good excuse for missing a week.

20 Things, Week 18

Last week I wrote a Sudoku solver script in Python. That was fun and all, but as my girlfriend pointed out - it’s not really usable for, er, well, most people. So this week I decided to make it a bit more versatile and build it into a service, with a website to go with it. It’s currently up on Github and once I’ve added some polish to the website (heh, it’s pretty awful right now) I’ll get it up on App Engine.

Read on →

20 Things, Week 17

This week I finally got around to doing something I’ve wanted to do for a while; I wrote a Sudoku Solver in Python.

Unfortunately, it doesn’t work. At all. In fact, the more I look into it, the worse I fear it is. I’ve attempted this before, but I struck a minor obstacle with some Python syntax and did something else instead; this time, I’ve got the entire algorithm worked out and developed. There’s just some fairly significant logic bug(s) that’re causing problems. Re-reading the code, there’s also quite a bit of that which could do with a rewrite.

No matter! I’m pretty happy I got the majority of the work done, and I’ll keep working on it - a bit of refactoring should hopefully make the logical errors a bit more obvious.

Edit (Jan 21 2014): Fixed.

20 Things, Week 16

I had my first open-source pull request merged this week, into Dogr.io.

Read on →

20 Things, Week 15

I checked in some code every day this week. That being said, I don’t really have a ‘Thing’ that I’ve done. Turns out that I’m running out of ideas for small, week-long projects. My plan to do some small things and even a bigger project over my 2 week break didn’t work out so well - Christmas takes up a lot of time, and there was plenty of other things to do in my time off, too.

So while I haven’t done any one thing worthy of note, some of the stuff I have done (these are mostly quite trivial):

  • Implemented PullToRefresh library
  • Implemented the Crouton library for toasts
  • Decided that loading 1000+ items with ORMLite to back a ListView was not a good idea and actually got around to writing the plumbing code to use a Cursor
  • Added a check for ‘last checked date’ so we now query for the smallest number of quakes