AIRActiveRecord is cool

Do you ever go back and look at code you wrote and think, “man, that’s pretty cool. I can’t believe how nice that turned out.”

I went back and wrote up some very minimal documentation on using AIRActiveRecord on my post about it and remembered how cool it is. True, more documentation would sure be nice, and I’ll get to that, eventually.

Using in-memory databases in AIR

Adobe Integrated Runtime (AIR) has support for creating and using SQLite databases through the use of the SQLConnection class in the flash.data package. Using this package you can store a database on a user’s hard drive and even name it with any extension you’d like (registering your app with that extension will let you double click on your “database” file and open the app with that as a parameter, pretty slick). One feature with the database package often overlooked is its ability to create and use in-memory databases. Using in-memory databases allows for faster read and write to the database and is perfect for operations such as building a code-completion lookup (someone going to rewrite Flex Builder in AIR? :), establishing a temporary search database of all the words in a list of documents that could be used for an autocomplete field, and any other situation where a database would be great to use but doesn’t need to be kept around between application launches. To create an in-memory database simply leave out the file parameter of the database connection.

 var db:SQLConnection = new SQLConnection(); db.open(); // no file is passed in 

Then you run your database setup script to create your tables and your good to go.

AIR ActiveRecord is Open Source

I wrote about an Active Record implementation for the Adobe Integrated Runtime using it’s SQLite database functionality. I put up all the code on Google Code under the name AIR Active Record. Please check it out, let me know of bugs or features, or better yet, submit fixes and add-ons. If you’re interested in being an active developer on it let me know. Update: To use the ActiveRecord (sorry for the lack of documentation), you need to extend it with a class for each table you’ll use. For example, if I wanted an employee table I would create an Employee class like this:

 package { import flight.db.activeRecord.ActiveRecord; [RelatedTo(name="tasks", className="Task", multiple)] public dynamic class Employee extends ActiveRecord { public var name:String; public var position:String; public var hireDate:Date; public var salary:Number; public var created:Date; public var modified:Date; } } 
 package { import flight.db.activeRecord.ActiveRecord; [RelatedTo(name="employee", className="Employee")] public dynamic class Task extends ActiveRecord { public var employeeId:uint; public var todo:String; public var created:Date; public var modified:Date; public function Task(todo:String = null) { this.todo = todo; } } } 

In the metadata, the name is the property name which will be auto-generated and auto-populated (when you access it) for the relation. The className is the full class path (e.g. com.foo.Bar). multiple is a flag that specifies whether it’s a one-to-many relationship. You can have many-to-many relationships as well. If you aren’t creating the database yourself there is a handy-dandy feature which will do it for you off of code introspection. Many-to-many is not supported for creation this way unless you have a class for that join table and run it on that.

 var employee:Employee = new Employee(); TableCreator.updateTable(employee); 

This will create the table if it doesn’t exist, but it is also useful for updating the table if you’ve added new properties to the class. (right now it doesn’t delete columns if you remove properties) Then, you use it. You’ll need to look at the code comments, or maybe generate the AS3 docs off of it to see all you can do, but this is some of what you can do (let’s say you have an Employee class and a Task class):

 var employee = new Employee(); employee.loadBy("username = ?", "bobTheBuilder"); // accessing tasks will autoload them from the database for each (var task:Task in employee.tasks) trace(task); task = new Task("Call your mother"); employee.task.push(task); employee.saveTasks(); employee.firstName = "Bobby"; employee.save(); var employee2:Employee = new Employee(); employee2.firstName = "Sue"; .... employee2.save(); trace(employee2.id); // new id 

Pretty cool stuff. Maybe someone should document it! :D Anyone interested in figuring out and documenting super-cool code, please apply!

Why Open Source

I have to admit, it took me awhile to understand why anyone would want to open source their software. I understood perfectly well why I would want to use it, but as a developer who makes money from writing software I assumed you don’t make money writing open source software, thus, you starve. I understand now how it works and will explain simply for both developers and business why open source software makes sense.

First I’ll start with business.

If you are a business owner think about this. Who has the most money to spend on the products you make? Well, medium to large businesses. They spend hundreds of thousands to millions every year. You’d like to write software to target these businesses. You will need to spend quite a bit in advertising and sales to hit that market. And of course, lots of money will need to go into the production of the software.

What if you make this product and all of the little guys test it for you? What if non-customers help you bug fix it? And after your initial release, what if they continue to add features, fixes, and value to your product for free? What if they market it for you and provide an initial level of support? Well, this is open source software. You share the product with the world, let them use it, contribute to it, make it their own, and then you still make money from your original target market through something they couldn’t go without, support. Those with the money are still paying for it, but those without get to benefit from it and will add to it.

Developers look at things differently.

As a developer why would you want to write open source software? Think of all the code you’ve written that you just know has been written before. Think of all the wheel-reinventing you’ve done throughout your career. As an industry we aren’t moving forward in technology if we spend all our time writing the same thing for our own company or clients that everyone else is writing for THEIR companies and clients. But, if we start sharing this are we going to run out of a job? No, we’ll spend our time writing new code and implementing new ideas. Our clients will always want something more than what’s available. We as an industry can push the technology forward as a whole and everyone benefits.

That’s how I see it. Open source is a win-win-win for those writing it, those who use it, and those who support it. Better software comes out of it. People still make money and feed their families. And you make the world a better place.

Don’t use Number to iterate over for-loops

A while back I read a couple of blog posts about the slowness of using uint and int to iterate through for-loops. I needed to do some testing for a little project today and found this is false.

When using the “i” variable in for(var i = 0; i < length; i++) as an input into mathematical operation, especially when doing fractions, Number is faster for obvious reasons. This was established in the posts of the previously mentioned blogs. But when simply iterating over an array which is a very common use-case for for-loops uint is faster. Here is my test setup:

var value:Object;
var arr:Array = new Array(1000000);
var length:uint = arr.length;
var startTime:Number = getTimer();

for (var i:uint = 0; i < length; i++)
	value = arr[i];

var endTime:Number = getTimer();
trace("Total Time:", endTime - startTime);

I was getting around 210 for uints and around 230 for Number. Not a big difference, but I feel dumb for always using Number for this sort of thing without even thinking about how it works.

Using Your Own Custom Metadata in AS3

Flex 3 gives us a great new feature, custom metadata tags. Now, I know you could actually use custom metadata in Flex 2, but you would have to add “-keep-as3-metadata MyTag” to every single project that utilized these custom tags. In Flex 3, if you add “-keep-as3-metadata MyTag” to a library (using compc to compile a SWC or a Flex Builder Library project), then EVERY project that uses that SWC will automatically keep the “MyTag” metadata tags. This allows custom libraries that utilize these tags for development.

Would be cool to create a library to hook up listeners so you can create listeners like this:

[Listen(obj="this.closeButton", event="click")]
public function closeClickHandler(event:MouseEvent) {...}

You’d use -keep-as3-metadata Listen in the libraries compiler options. Maybe if you want I could post a tutorial on doing something like this. Drop me note and let me know if there is interest.

« Previous Page — Next Page »