Android Master Class Listener Aware AsyncTasks

The AsyncTask class provided within the Android framework is a convenient and powerful mechanism for integrating background tasks into applications with very little effort or thought from the developer. So long as the semantics are followed, it’s almost trivial to perform complex background processing, asynchronous progress updates and UI manipulations on completion. However, some caveats can lay hidden landmines for the unwary. We’ll explore a Listener-based paradigm to provide a bit more safety to this fundamental tool.

Read on →

Objective-C Tricks: #1 Ternary Operations

Objective-C is a reflective and powerful object-oriented language. When first starting (or looking) at the syntax, many react fearfully to the verbosity and square brackets. Many rarely get beyond the basics they learned when making their first app, but there are many unused abilities in Objective-C.

Ternary Operations with Objective-C objects

Most people are familiar with ternary operations. For example:

1
NSString *myString = [self isHighlighted] ? @"Highlighted" : @"Not Highlighted";

Similar to this behavior in C:

1
int i = 1 ? : 0;

Objective-C supports omitting the true side of the ternary operator when working with objects. Consider a case where you need to set a string depending on whether another string is nil or not. You might try:

1
2
3
4
5
6
NSString *myString = nil;
if ([self otherString] != nil) {
  myString = [self otherString];
} else {
  myString = kSomeConstantString;
}

We can quickly clean this scenario up (down to one line, even). First, we remove the check against nil. When checking the state of an object ( if (obj != nil) { ... }), nil is a false statment and non-nil is a true statement. Therefore, we can update this to ( if (! obj) { ... } ). We can then expand that into a ternary operation.

1
NSString *myString = [self otherString] ? [self otherString] : kSomeConstantString;

We can go a bit further in this scenario, however. Since the object we are checking is also the true side of the ternary operation, we can omit it.

1
NSString *myString = [self otherString] ? : kSomeConstantString;

UPDATE: Another thing that makes this useful is that the message to self is only sent once. (Thanks to @bmf making this point.)


Welcome to ToastMo

On behalf of Two Toasters, welcome to ToastMo! We’re very excited to finally throw open the doors and share what we’re doing. What are we doing? First let’s talk about who we are and things will start getting clearer.

Who is Two Toasters?

Two Toasters is a growing, highly acclaimed mobile development company based out of Durham, NC and New York City. We love developing beautiful applications for our customers focused primarily on Android and iOS platforms. We’re pretty good at it, too, if we do say so ourselves. Since we’re growing, we’re always looking out for good young developers to turn into awesome veteran Android and iOS developers. We do a lot of teaching internally and decided as a company that it would be rewarding to extend that knowledge out the doors to the general mobile development community.

Why? Simply put, we like cool apps. More good mobile developers means more cool apps. And more good developers hopefully means fewer bad developers which leads to fewer bad apps. Bad apps make our souls bleed. We hate that.

What is ToastMo?

ToastMo is our repository of knowledge. It’s our place to post things we’ve learned, things we wish to teach, or things we think people should know. Some things on ToastMo will be basic, some will be advanced. Some things you might already know, some – hopefully most – you might not. We’ll be teaching things we teach our own junior developers, things that often aren’t necessarily obvious if you haven’t done it before. We’ll teach best practices we’ve developed over time based on our experience with our projects.

How can I get involved?

Thanks for joining us! Requests for future topics are always appreciated, so don’t hesitate to leave a comment.