// sorting the list by comparing the "date" property on the objects in the array
NSSortDescriptor *dateSortDescriptor = [[[NSSortDescriptor alloc] initWithKey:@"date" ascending:NO selector:@selector(compare:)] autorelease];
[list sortUsingDescriptors:[NSArray arrayWithObjects:dateSortDescriptor, nil]];
static BOOL checkNetwork = YES;
if (checkNetwork) { // Since checking the reachability of a host can be expensive, cache the result and perform the reachability check once.
checkNetwork = NO;
Boolean success;
const char *host_name = "earthquake.usgs.gov";
SCNetworkReachabilityRef reachability = SCNetworkReachabilityCreateWithName(NULL, host_name);
SCNetworkReachabilityFlags flags;
success = SCNetworkReachabilityGetFlags(reachability, &flags);
_isDataSourceAvailable = success && (flags & kSCNetworkFlagsReachable) && !(flags & kSCNetworkFlagsConnectionRequired);
CFRelease(reachability);
}
return _isDataSourceAvailable;
NSNotificationCenter.
Mutliple observers can be notified of events.
notification sent by a specific object...
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(myFooHappened:) name:MyFooNotification object:foo];
set object to nil to receive notifications of that name sent by ANY object.
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(myFooHappened:) name:MyFooNotification object:nil];
Format for myFooHappened is..
-(void) myFooHappened:(NSNotification*)note {
// [note name] - name of notification [note object] - sender
// [note userInfo] is an NSDictionary of key-value pairs filled by the sender.
}
Note: notifications are posted on the SAME THREAD as they were posted.
MUST unregister from all notifications in your dealloc
-(void) dealloc
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
Posting a notification..
Define a constant for the name of the notification..
in .h
extern NSString* const MyFooNotification;
in .m
NSString* const MyFooNotification = @"MyFooNotification";
-(void) postFoo {
[[NSNotificationCenter defaultCenter] postNotificationName:MyFooNotification object:self userInfo:[NSDictionary ...]];
// NOTE: this uses place holders.
// PART 1 - update url of path to the image
// PART 2 - update the variable reference to the UIImageView object
// -----------------------------------------------
// ===== PART 1 =====
UIImage *imgURL = [[UIImage imageWithData: [NSData dataWithContentsOfURL: [NSURL URLWithString: @"${part 1 image path:http://www.example.com/img.jpg}"]]] retain];
if (imgURL != nil) { // Image was loaded successfully.
// ===== PART 2 =====
[${part 2 UIImageView Ref:myMainImg} setImage:imgURL];
// ===== PART 2 =====
[${part 2 UIImageView Ref:myMainImg} setUserInteractionEnabled:NO];
[imgURL release]; // Release the image now that we have a UIImageView that contains it.
}