I'm trying to fetch EKEvent
s from the Event Store to populate a UITableView
and display a month list view.
Basically it works and I'm doing it like this:
- (void) reloadEvents
{
for ( NSString *entry in self.calendarA )
{
NSMutableArray *tempArray = [[[NSMutableArray alloc] init] autorelease];
[tempArray addObjectsFromArray:[appDelegate.eventStore eventsMatchingPredicate:[appDelegate.eventStore predicateForEventsWithStartDate:[NSDate fromString:entry] endDate:[[NSDate fromString:entry] midnight] calendars:nil]]];
[tempArray addObjectsFromArray:[self initializeItems:[NSDate fromString:entry] withEndDate:[[NSDate fromString:entry] midnight]]];
[tempArray sortUsingDescriptors:[NSArray arrayWithObject:[[[NSSortDescriptor alloc] initWithKey:@"startDate" ascending:YES] autorelease]]];
[[self.calendarD objectForKey:entry] addObjectsFromArray:tempArray];
}
dispatch_async(dispatch_get_main_queue(), ^(void)
{
[self redrawTableCells];
});
}
reloadEvents
is called from within a
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^(void)
{
[self reloadEvents];
});
as fetching events happens synchronously and it locks the UI for that time, I'm using GCD. The NSDate
parts are my own categories on NSDate
.
Now, when my view controller loads, events are fetched from the Event Store and displayed correctly. The view controller also listens for EKEventStoreChangedNotification
and that's where my app crashes. When I change an event outside my app it receives a notification and tries to reload the event data, but then...
*** -[CFString length]:
message sent to deallocated instance 0x666f530
EDIT
I've changed reloadEvents
to the following:
- (void) reloadEvents
{
NSArray *daysArray = [[self.calendarD allKeys] sortedArrayUsingSelector:@selector(compare:)];
for ( NSString *entry in daysArray )
{
NSMutableArray *tempArray = [[NSMutableArray alloc] init];
[tempArray addObjectsFromArray:[appDelegate.eventStore eventsMatchingPredicate:[appDelegate.eventStore predicateForEventsWithStartDate:[NSDate fromString:entry] endDate:[[NSDate fromString:entry] midnight] calendars:nil]]];
[tempArray addObjectsFromArray:[self initializeItems:[NSDate fromString:entry] withEndDate:[[NSDate fromString:entry] midnight]]];
[tempArray sortUsingDescriptors:[NSArray arrayWithObject:[[[NSSortDescriptor alloc] initWithKey:@"startDate" ascending:YES] autorelease]]];
[[self.calendarD objectForKey:entry] addObjectsFromArray:tempArray];
[tempArray release];
}
dispatch_async(dispatch_get_main_queue(), ^(void)
{
[self redrawTableCells];
});
}
and with this, the app doesn't crash anymore.
Seems like something changed calendarA
and therefore the entry was already deallocated (which, after having found the cause of the problem, is absolutely logical).