Snippet: Running Tasks in the Background While Updating the UI

This is just a very quick note about multithreading in iOS 4.0+. If you want to put a long-running task to the background but keep your users updated about the state of the task you can use this snippet.

// start background execution
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
            
      // do background work here (e.g. in a loop)
            
      // everytime you want to update the ui call this
      dispatch_async(dispatch_get_main_queue(), ^{
                
            // update your ui here .. e.g.
            [myUIProgressView setProgress:myProgress];
            // you need to provide these variables yourself of course ;)
                
      });
});

ComboBox like UITableViewCells

When I came across the Twitter App for iPhone, I saw something I immediately wanted to have for my own app: UITableViewCells acting like ComboBoxes, opening on tap, closing when a new option is chosen. The idea was so nice that I decided to write a small prototype which can now be found at a public github repository of mine. There is also some basic documentation that might be useful for those who want to re-implement this code.

The key facts in a nutshell:

  • You need a custom UITableViewCell in order to display a status indicator as shown in the “screenshot” above and to make it easier to maintain a status within the UITableViewCell itself. There are plenty of tutorials on the web demonstrating how to create those custom UITableViewCells and loading them from a XIB file.
  • Everytime you change the rowcount in a section (insert or delete cells) you need to adjust the number that ‘numberOfRowsInSection’ returns so you can check the state of your ComboBox cell and return the number of options in that cell plus 1 for the cell itself if the state is ‘open’. If you don’t adjust this value, you run into an inconsistency exception.
  • Although the option cells are not visible from the start, you need to return these cells from the ‘cellForRowAtIndexPath’ method. A good way to realize a dynamic number of option cells is to make use of the ‘default’ statement in the switch and only specify a static cell for the first row in that section (id 0).
            switch ([indexPath row]) {
                case 0: {
                 
                    // return the ComboBox cell here
                    
                    break;
                }
                default: {
                    
                    // return the Option cells here
                    
                    break;
                }
            }

If you’re interested, feel welcome to check the full prototype and it’s documentation in the repository.

Introduction to NSPredicate

As I continued to work on my Rumford1797 app I made heavy usage of an Cocoa framework component called NSPredicate. Most of you that have come across NSPredicate have seen it in combination with CoreData when fetching objects from the persistent store. That was my first point of contact with it as well but lately I discovered that it is also useful for cleansing your code when working with any amount of data stored in structures like NSSet or NSArray.

WHAT IS NSPREDICATE?

NSPredicate is basically a predicate to filter objects. The advantage is, that it can be used to filter many collections of objects, e.g. NSSet or NSArray.

// create an array of (ns)strings
NSArray *looneyTunes = [NSArray arrayWithObjects:@"Bugs Bunny", @"Daffy Duck", @"Elmer Fudd", nil];
	
// create a predicate that looks for objects that begin with the character 'B'
NSPredicate *beginsWithB = [NSPredicate predicateWithFormat:@"SELF beginswith[c] 'B'"];
	
// this is where the magic happens
NSArray *looneyTunesThatBeginWithB = [looneyTunes filteredArrayUsingPredicate:beginsWithB];
	
// returns 'Results: 1'
NSLog(@"Results: %d", [looneyTunesThatBeginWithB count]);

EXAMPLES

As you can see, filtering an array by a simple predicate is just a single line of code. Even the most complex predicates work like this. So lets pretend we have an objectset of a class called LooneyTune that has the following structure:

Now we want to retrieve these objects from an array that have 'Bunny' as lastName.

NSArray *looneyTunes = [NSArray arrayWithObjects:bugsBunny,honeyBunny,lolaBunny,elmerFudd,nil];
NSPredicate *lastNameBunny = [NSPredicate predicateWithFormat:@"lastName like[c] 'Bunny'"];
NSArray *bunnies = [looneyTunes filteredArrayUsingPredicate:lastNameBunny];
	
// returns 'Results: 3'
NSLog(@"Results: %d", [bunnies count]);

That was again easy, wasn’t it? The next and last example will show you that it is also very easy to use more complex data types as strings like NSDate in you NSPredicates. Let’s say we want to find all LooneyTunes characters that had their first appearance before November 1st, 1966

NSDate *date1966 = [NSDate dateWithNaturalLanguageString:@"November 1, 1966"];
NSPredicate *appearanceBefore1966 = [NSPredicate predicateWithFormat:@"firstAppearance < %@",date1966];
NSArray *firstLooneyTunes = [looneyTunes filteredArrayUsingPredicate:appearanceBefore1966];
	
// returns 'Results: 2'
NSLog(@"Results: %d", [firstLooneyTunes count]);

ONE MORE THING ..

You might have guessed it but I felt the need to emphasize on this: You are able to combine multiple predicates with logical operators like AND and OR. A quick example will show you what I mean:

NSPredicate *filter = [NSPredicate predicateWithFormat:@"(start >= %@) AND (end <= %@)", earlyDate, lateDate];

This simple line of code is able to extract objects (having the properties start and end) that are valid in a given period which is limited by earlyDate and lateDate. In my experience, this method is faster and much more reliable compared to a ‘for’ loop or similar classical approaches for this purpose.

For more information about NSPredicate and its usage please refer to the Predicates Programming Guide.

(The name LooneyTunes and the LooneyTunes characters which have been used for the code examples are property of Warner Bros.)

Using the AddressBook API to scan for contact details on iOS 4.0+

OBJECTIVES

This quick tutorial will show you the basic usage of the AddressBook API of iOS and give you an example how to

  • obtain a list of all contacts in the address book
  • access a contact
  • access details of a contact

OUT OF SCOPE

In this article I will not show how to modify data or present it with the built-in view controllers. The first thing is too much for a quick start and the second thing is very good documented in the official Apple docs.

PRELIMINARIES

Before you can start accessing your address book you need to add two frameworks to your project: AddressBook and AddressBookUI. Also, do not forget to insert some test-contacts into the contacts app of the simulator as you might not want to test address book operations directly on your real address book.

GETTING STARTED

The first thing you need is the reference to your address book. There is a nice function in the API that does that work for you.

ABAddressBookRef addressBook = ABAddressBookCreate();

Now that was an easy start, wasn’t it? Next thing you need is to query the address book reference for some data. As I just want to give a simple example here I am querying for a list of all contacts.

NSArray *people = (NSArray *)ABAddressBookCopyArrayOfAllPeople(addressBook);

As you might have guessed, this function copies all of the contact information of all contacts from the address book into an array which might then be type-casted into a NSArray.

But what exactly is now pulled from the address book and stored inside the NSArray? The answer: ABRecordRef objects. These objects are generic wrappers around all kinds of entries in the address book, hence ‘record’.

By now, it is already possible to determine the number of contacts in the address book as each of the ABRecordRef objects represents one contact. To check whether there have been pulled some contacts at all, one could simply use something like

if ((people != nil) && [people count]) { 
	/* do something */ 
}

The next step would be to select a single record from the NSArray which I will do with a simple for loop to iterate over all records.

for (int i=0 ; i < [people count];i++) {
	ABRecordRef ref = (ABRecordRef)[people objectAtIndex:i];
}

Up to now nothing really magical has happened and this is not going to change very much. However, the next part is the part which cost me an hour or so to figure out because there is a small lack of information concerning E-Mail addresses in the documentation.

Before we continue, I have to explain what ABSingleValueRefs and ABMultiValueRefs are. In the AddressBook API, there is a difference between properties which can only contain a single value (e.g. ‘Last Name’) and properties which contain more than one value (e.g. ‘Addresses’). Single value properties are directly accessed via their key (e.g. ‘kABPersonFirstNameProperty'):

CFStringRef first = ABRecordCopyValue(someRecord, kABPersonFirstNameProperty); 
CFStringRef last  = ABRecordCopyValue(someRecord, kABPersonLastNameProperty);

Multi value properties are accessed by a key as well but as they return more than one value they have to be temporary stored inside another NSArray:

ABMultiValueRef emails = ABRecordCopyValue(someRecord, kABPersonEmailProperty); 
NSArray *mailAdresses = (NSArray *)ABMultiValueCopyArrayOfAllValues(emails);

Afterwards, each value can be extracted in some kind of a loop, for example another for loop:

for (int j=0 ; j<[mailAdresses count] ; j++) { 
	NSString *emailAddress = (NSString *)[mailAdresses objectAtIndex:j]; 
}

For more information on which keys are available and to which of the two categories they belong, please refer to the official documentation.

EXAMPLE CODE

This is the complete code of the example with some NSLog() output:

// Fetch the address book
ABAddressBookRef addressBook = ABAddressBookCreate();
// get all people from the addressbook
NSArray *people = (NSArray *)ABAddressBookCopyArrayOfAllPeople(addressBook);
if ((people != nil) && [people count]) {
	NSLog(@"%d contacts have been extracted from the address book", [people count]);
	for (int i=0;i < [people count];i++) {	
		ABRecordRef ref = (ABRecordRef)[people objectAtIndex:i];
		ABMultiValueRef emails = ABRecordCopyValue(ref, kABPersonEmailProperty);
		NSArray *mailAdresses = (NSArray *)ABMultiValueCopyArrayOfAllValues(emails);
		NSLog(@"contact %d has %d E-Mail addresses",i,[mailAdresses count]);
		for (int j=0;j < [mailAdresses count];j++) {
			NSString *emailAddress = (NSString *)[mailAdresses objectAtIndex:j];
			NSLog(@"address %d: %@", j, emailAddress);
		}
	}
} else {
	NSLog(@"No contacts have been extracted from the address book");		
}
[people release];
CFRelease(addressBook);

NIGHTNIGHT by DEDDY