NSMutableSet: Filtering Duplicate Elements

Suppose that your app has X amount cached objects. And suppose that there is a slight chance that.. since the backend doesn´t actually know what it is exactly that you have cached (and what you don´t have cached), it might send duplicates.

So… what do we do?. Shall we write a for routine checking dupes?. No way. There is a far more performant way to achieve this, and it requires less lines of code.

Steps…

  • Implement your own ‘isEquals’ and ‘hash’ methods, in your model object. For instance…

[cc lang=”objc”]
– (BOOL)isEqual:(id)object
{
    SomeClass* secondObject = (SomeClass*)object;
    return ([secondObject isKindOfClass:[self class]] && 
            [[secondObject id] isEqual:[self id]]);
}

– (NSUInteger)hash 
{
   return [_id hash];
}
[/cc]

  • Simply instantiate a NSMutableSet and add the cached objects plus the objects retrieved from the backend. If the set already has an object for any given ID, and you attempt to insert a new one… but with the same id, it´ll just not work.

So… no more filtering things by hand. NextSTEP’s code will do the trick, from now on!

Grand Central Dispatch: Perform Block After Delay

We have seen in a previous post the proper way to write a singleton, with the help of our good friend Grand Central Dispatch.

In this opportunity, i´d like to share a goodie i´ve learnt few days ago. I suppose many of you had problems with the following scenario. You have to perform a task, which is time consuming, and you need to place, onscreen, an Activity Indicator.

Although the Activity Indicator performs the animation on its own thread, you need to “spare some time” in the main thread for it to begin working. I mean… you can execute [indicator startAnimating] right away. But… if you don´t release the main thread, even for a short moment, surprise!. The spinner won´t work.

So… the common solution is to split that method into two parts, and execute ‘performSelector: withObject: afterDelay:’. That´s where GCD comes in.
We can do the exact same thing… but with blocks!!. That´s SO MUCH COOL…!

[cc lang=”objc”]
dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW,
0.1f * NSEC_PER_SEC);

dispatch_after(popTime, dispatch_get_main_queue(), ^(void)
{
     // …  Your code
});
[/cc]

Note: The delay, in this example, is of about 0.1 seconds.

OSX iStats

Apple did a pretty good job with OSX. You buy a new mac… and you get almost everything you need, ‘for free’. Any mac comes with iPhoto preinstalled, Mail, Calendar, Safari and even Garage Band. Basically it covers everything an average user needs to do.

There are really really few weak spots to OSX. I dare to say that… one of the few things OSX lacks is a nice widget to check out things such as the download speed… cpu load and temperature and even uptime. Good news is that there is a nice company called iSlayer, that has a reaaaaally cool Dashboard widget named ‘iStat‘ , which does exactly that.

It’s free… so you definitely don’t have an excuse not to download it. It has two modes: horizontal and vertical. You can tweak which indicators you want (and which ones you don’t want). Plus.. there is an iOS version, too. Go check it out!

Google Chrome: pageRank Tool

If you haven’t ever heard about it.. pageRank is one of the core components of it’s awesome algorithm. Roughly speaking, the concept is… you’re popular if people speaks about you. Right?. And if you’re popular, whatever you say should have a better ranking in google’s results.

Well, the idea is… there are several ‘root’ webpages that have a high pageRank (which is a number ranged from 0 to 10), and there is a formula to calculate the pagerank of a page.. based on the pagerank of the backlinks. It’s a graph.. and the pageRank flows from those ‘root webpages’.

So… this chrome plugin will help you check, seamlessly, the PR of any website you’re currently viewing. Why would you need that?. Simply curiosity… or SEO purposes. Of course!

Automating file download…!

I really don’t know if we’ll have file sharing services in… say… 5 years from now. Since megaupload has been taken down, and it’s CEO imprisoned, several other file sharing services have begun shutting down their services.

But many of them are still alive and kicking. So… whenever you need to download a huge amount of different files, manually entering the captchas (plus opening the links, one my one) turns into a tedious task.

That’s where jDownloader comes in. It’s a java based, multi platform app, which helps you download files form multiple sources. It has lots of plugins, and gets updated periodically. So chances are… it supports whatever filehosting service you’re using.

It has automatic captcha recognition (at least for several hostings). And if the hosting you’re using doesn’t have ‘auto-captcha-recog’, you’ll simply get a popup with the image itself.

It’s REALLY useful!