A hard-to-catch iOS Core Data programming bug

Consider the following code:

NSFetchRequest *request = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription 
                               entityForName:@"Post" 
                               inManagedObjectContext:context];
request.entity = entity;
request.fetchLimit = 0;
NSPredicate *pred = [NSPredicate 
                     predicateWithFormat:@"ANY followers.name CONTAINS[c] %@", username];
request.predicate = pred;
NSError *error = nil;
NSArray *results = [context executeFetchRequest:request error:&error];

It works properly to retrieve a list of posts whose to-many relationship “followers” contains a user whose name contains “username” case-insentively. However, if the code was written carelessly as follow:

NSFetchRequest *request = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription 
                               entityForName:@"Post" 
                               inManagedObjectContext:context];
request.entity = entity;
request.fetchLimit = 0;
NSPredicate *pred = [NSPredicate 
                     predicateWithFormat:@"ANY users.name CONTAINS[c] %@", username];
request.predicate = pred;
NSError *error = nil;
NSArray *results = [context executeFetchRequest:request error:&error];

If there is no to-many relationship “users” in the NSManagedObject “Post”, the app will crash with a EXC_BAD_ACCESS error and provide no useful traceback information. Without knowing exactly which line is causing the error, the programmer has to spend some time to carefully scan probably 50 lines of code (probably mixed with other Core Data code, such as NSExpression and NSExpressionDescription).

Advertisements
Posted in Uncategorized | Tagged , , | Leave a comment

Test WordPress Link Insertion

Dropbox

20120815-115737.jpg

Posted in Uncategorized | Tagged , | Leave a comment

Pyramid vs Django

Although I’ve always been a dedicated Django programmer, I have to admit that it cannot solve every web problem in the world. Django started as a nice, simple, and easy-to-use web framework for python programmers, and slowly matured into a do-it-all-for-you monolithic API. If you’re just starting to program the web, then Django is a very good choice.

However, the power of Django is somewhat limited by its ORM, which is a thin layer above raw sql. Admittedly, Django’s ORM is sufficient for most common website data management work. But its raw power cannot compare to that of SQLAlchemy, which is the de-facto ORM in the python community. In Django, simple things are easy to do, while complicated things are near-damn-impossible to do. In SQLAlchemy, simple things require more preparation than Django, but complicated things are possible to accomplish.

When I saw Pyramid for the first time, I was seduced by the clichéd “100% test coverage”, “clean, small code size of only 5000 lines”, “comprehensive documentation”, etc. I decided to give a try. As soon as I opened the documentation to the first page, I was hooked and read the whole documentation in one day (with a little bit coding along the way). Man, I love this framework. URL mapping can be done by either normal routes or Resource Traversal; ORM is by default ZODB or SQLAlchemy; the application runs on top of WSGI, which exposes access to raw HTTP requests / responses if you really want to tamper the data; testing is done by utilizing nose or any other framework you like; and numerous other clever design. You can develop your website in a truly orthogonal way: one part of the website doesn’t have to know anything about the other part, and they can share the same authentication back-end and common public database models. Suppose your client asked for a new feature on a running website, you can develop the new feature as a separate website, demo it to the client when it’s done, and copy-and-paste / git-submodule the new feature onto the running website without any downtime. With a nginx-based load-balancer running on a front-end VPS, your web application can grow by simply adding one line of configuration to the file nginx.conf and becomes truly scalable and maintainable.

Maybe you don’t believe me, but check out several websites who’re using Pyramid: dropbox.com, digg.com, bittorrent.com, o’reilly.com, opera.com …

In conclusion, I think Pyramid is much more than a mere web framework, but a proved way to organize and design your website modules in a comfortably orthogonal way so that you won’t get a phone call during the middle of the night when the website is down…

Posted in Uncategorized | 2 Comments

Why do I hate Windows?

Windows is the most popular operating system in the world. However, being the most popular does not mean it is the best. I tried to install one copy of Windows Pro 64bit on one of my computers. During the installation process, the computer screen spits out “Required cd/dvd drive device driver is missing.” Seriously? Why would any OS designer in their right mind decide to figure out whether there is a correct driver for the cd / dvd ? The initial load files for preparing the installation process has already been loaded into the memory, for god’s sake! After a little bit of google-ing, I found several explanations, some of which were obvious bull-shit. Only one explanation made sense — the ISO downloaded is corrupted. Then other people reported that only copies downloaded from the MSDN library have this problem while other types of copies worked fine (i.e., purchased from Microsoft store). So, someone in the distribution team of MSDN library screwed this up, pretty badly. The “official” solution is to get an external DVD drive with USB connection and use that drive to install the Windows copy. Uh….. No way I’m going to buy an external drive that I will never use again for installation. I’ll burrow one for sure. But, what a pain in the ass, Windows.

Posted in Uncategorized | Leave a comment

Debian vs. Ubuntu

Just got a Lenovo x120e, an affordable netbook, from the campus computer store. I installed Ubuntu 11.04 first, used it for about several hours. Then I shut it down, brought it home, and Bang — the laptop refuses to start, not even from the recovery mode. Reason? Kernel panic! The machine froze at “Loading initial ramdisk …” which includes the kernel 2.6.38. After a quick google which turns up nothing, I replaced Ubuntu with debian lenny, which runs happily ever after since. IMHO, I think Debian has a higher stability than Ubuntu since Debian is not chasing all the new “hot” software and upgrades like Ubuntu.

Posted in Uncategorized | 1 Comment

Linux

From “GNU/Linux Application Programming, Second Edition”:

“The history of GNU/Linux is an interesting one because at three levels, it’s a storyof frustration. Thompson and Ritchie designed the original UNIX as a way to replacethe existing Multics operating system. Richard Stallman created the GNUand FSF as a way to create a free operating system that anyone could use, free ofproprietary licenses. Linus Torvalds created Linux out of frustration with the Minix[Minix04] operating system that was used primarily as an educational tool at thetime. Whatever their motivations, they and countless others around the world succeededin ways that no one at the time would have ever believed. GNU/Linux todaycompetes with commercial operating systems and offers a real and useful alternative.GNU/Linux is predominantly the operating system for other operating systems(speaking virtually). Even in the embedded systems domain, Linux has begunto dominate and operates in the smallest devices, including smartphones.”

Posted in Uncategorized | Leave a comment

iPad 2 — Engineering Masterpiece

iPad 2 — 33% thinner than iPad 1, dual-core processor, and SAME battery life. This is a rare computer engineering masterpiece from Apple, considering the fact that it is even thinner than iPhone 4 by about .2 mm. Another obvious improvement is the graphics engine which is up to 9X faster than iPad 1. The engineering department in Apple must have lots of talented people in order to create such a wonder.

Posted in Uncategorized | Leave a comment