iPhone App Development Design Patterns
Not only is delegation powerful on its own merits, but the concept
is employed widely across numerous iOS SDK frameworks. For example, if
your app needs to retrieve updated data from a server, you will use a
class called NSURLConnection
to
initiate the request. Naturally, you don’t want the app to be locked up
while it waits for the server to return data (especially if the wireless
network is slow), so the NSURLConnection
class can send an asynchronous
request to the server and wait in the background for a response, whether it’s data or perhaps an
error. How is your app supposed to know when data has arrived so the app
can work on that data? That’s where the delegate model comes in: while it
waits and receives data, the NSURLConnection
object sends a series of
messages to whichever object has been assigned as the delegate (usually
the same object that issued the request). In that object’s class
definition are a group of methods that match the NSURLConnection
messages to act on the incoming
data. One of the messages signals that all data has arrived, and the
matching delegate method can then send other messages to begin processing
and displaying the data in the app.
Delegation is one of several design patterns
associated with iOS app programming. In object-oriented programming, a
design pattern suggests the relationships among objects and how objects
should work together. Going back to the NSURLConnection
class, your app uses that class without ...
Get Learning the iOS 4 SDK for JavaScript Programmers now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.