-[CFString hash]:发送到已释放实例的消息
我正在尝试从事件存储中获取 EKEvent 来填充 UITableView 并显示月份列表视图。
基本上它是有效的,我是这样做的:
- (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];
});
}
当获取事件同步发生时,从 a 中调用 reloadEvents
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^(void)
{
[self reloadEvents];
});
并锁定当时的 UI,我正在使用 GCD。 NSDate
部分是我在 NSDate
上自己的类别。
现在,当我的视图控制器加载时,将从事件存储中获取事件并正确显示。视图控制器还侦听 EKEventStoreChangedNotification,这就是我的应用程序崩溃的地方。当我在应用程序外部更改事件时,它会收到通知并尝试重新加载事件数据,但随后...
*** -[CFString length]:
消息发送到已释放实例 0x666f530
编辑 我已将 reloadEvents
更改为以下内容:
- (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];
});
}
这样,应用程序就不会再崩溃了。 似乎发生了 calendarA
的变化,因此该条目已经被释放(在找到问题的原因之后,这绝对是合乎逻辑的)。
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).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论