Monthly Archives: August 2008

 
27
Aug
2008
 

Cocoa Tutorial: Sync Services without Core Data

by Marcus Zarra

Sync Services have come a long way in Leopard. Before Leopard it was an extremely complex operation that was almost completely manual. Needless to say, this sucked and it was probably one of the reasons it was shunned by most developers.

If you are using Core Data in a Leopard application then Sync Services is so trivial that you should be syncing if it makes sense. In this article we are going to cover syncing in a non-Core Data situation as that is quite a bit more complex.

If you have read the Sync Services documentation then you know it is complex. Let me dispel an illusion right away. It is hard. It is not poor documentation, syncing is very hard and very few people get it right. Take a look at Omnifocus to see an example of a company thinking it is easy and losing data. Therefore if you are expecting this subject to be trivial you will be disappointed.

In this example we will be syncing with the bookmarks schema and displaying them in a simple outline view. The outline view itself will be editable and those edits can be synced back. Not terribly useful but provides a very simple example.
(more…)

 
24
Aug
2008
 

Cocoa Tutorial: C# LINQ Or Cocoa Key Paths And NSPredicate

by Matt Long

I think it may be helpful to demonstrate how to perform certain tasks in Cocoa from the perspective of a Windows programmer. And considering that I was one, it seems that I may be able to help shine some light on some of the issues that programmers face when coming over to the Mac platform. In this post I want to highlight one of Objective-C’s coolest features, key-value coding, in light of a Microsoft technology that was introduced in the most recent edition of the .NET framework (v3.5).

Microsoft’s new technology called LINQ, which stands for Language Integrated Query provides a way for developers to massage data collections using a SQL-like syntax that is built right into the language. You can perform simple data queries like this.

List products = GetProductList();

var soldOutProducts =
    from p in products
    where p.UnitsInStock == 0
    select p;
    
Console.WriteLine("Sold out products:");
foreach (var product in soldOutProducts)
{
    Console.WriteLine("{0} is sold out!", product.ProductName);
}

Notice how it has a very SQL like syntax. It’s actually very powerful and robust. There are a lot of capabilities. Take a look at the 101 LINQ Samples page if you would like to see more examples. What’s most interesting to me though is that the ideas behind this technology are not new.

If you’re new to Cocoa, you’ve probably heard of key-value coding and have probably even worked through some examples of its use without really understanding it. While the usage is different between the two, these technologies provide similar capabilities. Objective-C just had them first.
(more…)

 
18
Aug
2008
 

Cocoa Tutorial: libxml and xmlreader

by Marcus Zarra

Let us pretend for a moment that NSXMLDocument was not available to your Cocoa application for some reason. Perhaps you have low memory requirements, perhaps you are running on a slimmed down version of OS X. Whatever the reason, for the purposes of this exercise, NSXMLDocument does not exist.

Let us now assume that we have a requirement to parse an xml document quickly and without loading the entire tree into memory in a object structure. In a situation like this libxml comes in handy. Unfortunately it is quite a bit more complicated than calling alloc init on NSXMLDocument.

libxml is a C library that is included with all current releases of OS X. With this library we can quickly read in a document, scrape the information we need out of that document and avoid loading the entire tree into memory at once. In addition, libxml (and more specifically xmlReader) does this very quickly, far faster than NSXMLDocument which is very useful when you have a lower end CPU. In this project we are going to create a simple application that reads in an xml file containing a list of people, their names and their ages. For the purposes of demonstration we are going to load that data into an array of NSDictionary objects and display it in a standard Cocoa window.

(more…)