Skip to content

Commit

Permalink
CRITICAL BUG FIX: today's date and iOS 4.0 multitasking.
Browse files Browse the repository at this point in the history
Root cause: If an application using Kal is suspended in the background for an extended period of time (specifically, crossing the midnight threshold), when the app is resumed, Kal will still think that "today" has not changed. In my original implementation, I had naively assumed that the app would never be used for such an extended period of time, but iOS 4.0 multitasking makes my assumption invalid.

Solution: Listen for the UIApplicationSignificantTimeChangeNotification from the OS and respond appropriately by recaching today's date and refreshing the UI.
  • Loading branch information
klazuka committed Jul 23, 2010
1 parent a6a06b5 commit f2f9ff8
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
14 changes: 13 additions & 1 deletion src/KalDate.m
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,24 @@

static KalDate *today;


@interface KalDate ()
+ (void)cacheTodaysDate;
@end


@implementation KalDate

+ (void)initialize
{
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(cacheTodaysDate) name:UIApplicationSignificantTimeChangeNotification object:nil];
[self cacheTodaysDate];
}

+ (void)cacheTodaysDate
{
[today release];
today = [[KalDate dateFromNSDate:[NSDate date]] retain];
// TODO set a timer for midnight to recache this value
}

+ (KalDate *)dateForDay:(unsigned int)day month:(unsigned int)month year:(unsigned int)year
Expand Down
2 changes: 2 additions & 0 deletions src/KalViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ - (id)initWithSelectedDate:(NSDate *)selectedDate
if ((self = [super init])) {
logic = [[KalLogic alloc] initForDate:selectedDate];
initialSelectedDate = [selectedDate retain];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reloadData) name:UIApplicationSignificantTimeChangeNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reloadData) name:KalDataSourceChangedNotification object:nil];
}
return self;
Expand Down Expand Up @@ -191,6 +192,7 @@ - (void)viewDidAppear:(BOOL)animated

- (void)dealloc
{
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIApplicationSignificantTimeChangeNotification object:nil];
[[NSNotificationCenter defaultCenter] removeObserver:self name:KalDataSourceChangedNotification object:nil];
[initialSelectedDate release];
[logic release];
Expand Down

0 comments on commit f2f9ff8

Please sign in to comment.