Knockout Observable Array Performance Improvments

Today, I was working on a project that makes AJAX calls to an API and noticed that the application was very slow and unresponsive during the AJAX calls and ko updates. I checked that the requests weren't taking too long in Chrome's developer tools and found they were at max taking 300ms and minimum of 20ms to return. This was very odd behavior since the AJAX requests weren't blocking.

After some research, I found that when updating ko.observableArrays, you don't want to push each individual item to the array if you can help it. Each time an item is pushed to a ko.observableArray, it notifies its subscribers and depending on how many DOM elements you have on your page, that can be rather taxing. I had code like this:

$.ajax({
	url: 'api/Document/',
	method: 'GET',
	dataType: 'json',
	success: function(data) {
		Documents([])
		for(var i = 0; i < data.length; i++)
			Documents.push(data[i]);
	}
});

To fix this, you need to create temporary arrays to push your items to and then assign the ko.observableArray to the contents of the temporary array. Your code should look more like this:

$.ajax({
	url: 'api/Document/',
	method: 'GET',
	dataType: 'json',
	success: function(data) {
		var documents = [];
		for(var i = 0; i < data.length; i++)
			documents.push(data[i]);	

		Documents(documents);
	}
});

Qt Development on OS X Mountain Lion

1. October 2012 21:18 by Cameron in C++, Mac, Mac OS X, Qt  //  Tags: , , , , , ,   //   Comments

OS X Mountain Lion changes a few things on how XCode is installed. Xcode is now contained in an app bundle and the SDKs directory is part of the bundle contents, thus you will need to update your SDK paths in your project file like so:

MAC_SDK  = /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.7.sdk
    if( !exists( $$MAC_SDK) ) {
        error("The selected Mac OSX SDK does not exist at $$MAC_SDK!")
    }
    QMAKE_MAC_SDK = $$MAC_SDK

If you want to support Snow Leopard as a target platform with your Qt applications, be sure to use the 10.7 SDK and not the default 10.8 SDK. Your app should work just fine in Snow Leopard provided that you don't make any calls to Lion specific features while using the 10.7 SDK.

Another important thing is to install the command line tools for GCC so that Qt can find the compiler during installation and successfully compile C/C++ applications from Qt Creator. If you do use the 10.8 SDK with any Qt version less than Qt 5.0, it will give warnings that OS X 10.8 is unsupported during compilation. These warnings can be safely ignored. Aside from that, everything else should be the same as before. Happy coding!

Started Development of a Collapsible Frame Qt Widget

5. May 2012 22:14 by Cameron in C++, Qt  //  Tags: , ,   //   Comments

Today, I started development of a collapsible frame Qt widget. I needed a widget that could show/hide its contents by clicking on an open/close button but couldn't find a solution online so I decided to create my own. The code is still a work in progress, but the basis of the code involves the use of nested layouts to achieve the hide/show effect. I had to write a custom clickable label class and subclass QLabel as QLabel doesn't have a clicked() signal. I'm using style sheets to apply the icon to the QLabel so that it has the correct image based on open/close and hover over states. The code is available on github under the Apache 2.0 license. Feel free to check out the code and see if you'd like to fork it yourself and contribute.

https://github.com/pcmantinker/Qt-Collapsible-Panel

Separation of Concerns

9. August 2011 17:57 by Cameron in Programming  //  Tags: , ,   //   Comments

A good programmer makes sure to provide proper separation of concerns while coding applications. This makes maintaining the application's source code much more manageable and it also prevents the application's source code from becoming one large function that does everything. Back in the days before object oriented programming and even procedural, it was difficult to separate functionality of one part of an application from another.

With procedural programming in a language like BASIC, many of you might remember the GOTO statement, quite possibly the worst programming language mechanism ever conceived. Using GOTO statements made application maintenance quite a challenge. People should never have to manage program flow manually through GOTO statements. They behave essentially like a JUMP instruction in assembly. However, in assembly, using constructs such as GOTO or JUMP is required as there is no other way to control program flow in assembly.

In languages such as C or later versions of Microsoft Quick Basic or QBASIC, the languages provide the ability to call functions from a main function, presenting a huge improvement in programming history. This made it possible to separate business logic from database/filesystem logic and thus was the beginning of better code.

With the continuing popularity of object oriented programming, separation of concerns is improved exponentially beyond what procedural programming had done previously. Programmers have the ability to separate their application's functions into objects that represent various parts of the application. For instance, in part of a user authentication system, one might create a user class that can then be instantiated and passed to the user data access object, the object that handles all the low level database interactions.

Using object oriented design, applications are clearly divided up into objects that serve their own individual purpose, while achieving the same end goal: a finished product. While there may be better approaches than object oriented design that become evident in the future, it is clearly one of the best way of modelling the real world in a virtual environment. Also, people think in terms of tangible items and enjoy representing application parts with objects. It will be interesting to see how the industry develops in the next 10 years and how design paradigms shift.

Month List

Tag cloud