KVO and Grand Central Dispatch

by Hasan on 1/03/2012

Today has part of the Portland app. I decided to use KVO pattern. My goal was to transfer information from a Loader object to the Controller and the controller could then update the UI. The Loader is a simple object whose job is to load in the initial data for the app into Core Data.

The problem:

1) I don’t want model related class to be updating the UI for many reasons
2) Didn’t want to hard wire all these classes to one another and create hard dependencies.

A solution:

I decided to use the KVO pattern. My controller monitors a property in the loader object and gets notified when it changes. So what happens is, the load objects works in a queue from grand central dispatch. When its property changs and my controller get notified. I was unable to see the changes on the ui. After some research, I learn that the notification that was sent is in the background thread of grand central dispatch. So in the controller instead of updating the UI, you have to call [self performSelectorOnMainThread....] and that will update the UI.


No Comments

Add Google Data API to your IOS (iphone,ipad) project

by hasan adil on 27/03/2011

Google has a great pure objective c implementation of the google data protocol. Further, almost all their servies are exposed in the API. To use it in your project, follow these steps:

- Download, unzip and save to some directory (we’ll need this path later)
- Open GData project file in xcode
- Open your project in xcode
- Drag GData node in the “Groups and Files” onto the Frameworks node in your project
- Drag libGDataTouchStaticLib.a file from frameworks to the Targs > Your project name > Link Binary W Libraries node
- Add -lxml2 to Other Linker Flags setting
- Add $(SDKROOT)/usr/include/libxml2 to Header Search Paths
- Add //gdata-objectivec-client-1/Source to User Header Search Paths setting

Voila done, have fun using google services in your iphone, ipad apps!


GWT 2.1

by hasan adil on 12/11/2010

BeforeMotion.com just got transitioned to GWT 2.1. It wasn’t easy. For one the panel system in the user interface has been redone. The classes of DockPanel for example have been marked deprecated, and replaced by DockLayoutPanel. However, replacing DockPanel with DockLayoutPanel is not a simple textual replace operation. So it has been somewhat of a headache for Google to change the UI foundations so fundamentally. It makes sense in the long run and so perhaps it is good.

I will write a longer post next time so detail the code changes needed in order to do the transition. Come back soon!



by hasan adil on 8/10/2010

I have been using MongoDB for a new project which is coming out soon. So far I am impressed with it. The development focus really shits towards user requirements rather than endlessly playing with SQL, prepared statements, ORM and on and on.

The one problem I have noticed is not really a problem with Mongo but is rather because it is still new to the industry. The hosting providers have not adopted it yet. So if you want to use Mongo and want to host it on and don’t want to host it yourself then the cheap hosting plans won’t work.

This is because the php driver mongo.so needs to be deployed and enabled in php.ini. Hosting providers don’t give you that ability in their base plans which cost $5/month. It’s not an issue if you get dedicated hosting or rent a virtual machine.


Coming back…

by hasan adil on 14/09/2010

Haven’t put up a new post in a while. Been looking for jobs, finding one and starting it… Well TrophyHound.com is up for anyone to use!

A amazing collection of lecture videos that my dad sent me. 100 Lectures


LabelTop For Food now available!

by hasan adil on 2/06/2010

You can find it here: http://www.beforemotion.com/food

Enter any specific food’s name or just a generic food and see the results. Examples are:
Big Mac
and so on…


What’s GMail doing?

by hasan adil on 24/05/2010

See the screenshot below and do you know if my gmail session is secure or not?


Google Calendar Bug on Mac OS X

by hasan adil on 24/05/2010

This was really surprising that Google missed this. Apple always surprises me when it doesn’t provide basic computer features and when it does do so then it is hailed as ‘magic’ or some other kind of break through. Meanwhile, other operating systems have been providing that feature for decades. Some examples are:

- Spaces on OS X, well having multiple screens has been provided by solaris and linux for years and years
- Multithreading on iphone. This is such an old concept and is a highly studied and solved problem. It was introduced first with the earliest preemptive kernels. Why is it so new on iphone? Maybe someone in the marketing department knows.

Anyway, I digressed. The bug is that Google Calendar relied on local OS to see what time it is. Normally this would be fine. But when travelling across timelines it caused a problem. I went from the East coast to the West and while I do have internet here, OS X did not bother to update the clock by subtracting 3 hours (though Windows XP would have, I have not used Win7 but I imagine it does too). Now my google calendar events started alerting me to events based on my computer clock which is 3 hours ahead of what it should be. And for a few seconds I entered panic mode that missed my events for last 3 hours till I noticed the issue :) .

Google Calendar should go to a time server for current time when an internet connection is available. They don’t even have to maintain one, just use the one provided by US Navy which is the official one anyway. If there is no internet connection available then it is a harder problem. But at least solve the the former of the two use cases.

I have updated my OS X clock manually and things are in order once again. Thanks for reading.
* I know you can set the clock to update automatically but why isn’t it checked to true by default. Who wants an inaccurate clock. Plus, the bug is not in OS X it is in Google Calendar.


Bubble sort in Java

by hasan adil on 18/05/2010

Bubblesort is one of the simplest sorting methods. It also very inefficient. Though in the best case when the elements are already sorted, then its run time is O(n). Otherwise it is O(n^2).

[sourcecode language="java"]
package com.labeltop.sort;

public class BubbleSort {
public void sort(Comparable[] comparables) {

int size = comparables.length;

// loop till swaps are being done
while (true) {
// reset it
boolean foundSwap = false;

// loop over values
int i = 0;
while (i < size) {
// don’t care about last value
if (i == size – 1)

// check for swap i.e. c1 > c2
if (comparables[i].compareTo(comparables[i + 1]) > 0) {
Comparable temp = comparables[i];
comparables[i] = comparables[i + 1];
comparables[i + 1] = temp;
foundSwap = true;

// keep on…

// check if there were any swaps
if (!foundSwap)


Quicksort in Java

by hasan adil on 17/05/2010

QuickSort is one of best sorting algorithms. While the worst case scenario is O(n^2), the average case is O(nlgn). Additionally, it is possible to implement in highly optimized way as well do several of its parts in parallel.

[sourcecode language="java"]
package com.labeltop.sort;

public class QuickSort {

public void sort(Comparable[] c) {
quicksort(c, 0, c.length-1);

private void quicksort(Comparable[] comparables, int start, int end) {

// get a pivot point
int pivot = start + ((int) (Math.random() * (end – start)));
Comparable cPivot = comparables[pivot];

// where to start on either side of pivot
int left = start;
int right = end;

// loop till they overlap
while (left <= right) {
// find first on left that is bigger than pivot
while (cPivot.compareTo(comparables[left]) > 0) {
// more towards pivot

// find first on right that is less than pivot
while (cPivot.compareTo(comparables[right]) < 0) {
// more towards pivot

// exchange them
if (left <= right) {
Comparable temp = comparables[left];
comparables[left] = comparables[right];
comparables[right] = temp;
//go to next ones towards the pivot

//recurse on left
if (right > start) {
quicksort(comparables, start, right);
//recurse on right
if (left < end) {
quicksort(comparables, left, end);