我正在尝试创建后台线程以获取核心数据以填充 NSOutlineView。该操作可能需要几秒钟,所以我想将操作放在后台线程中并向用户显示 UI,如下所示。随着每个部分的搜索完成,我正在主线程上调用一个函数来重新加载该部分的数据。这似乎工作正常,但我有一个实例,在核心数据获取操作期间引发了异常。用户真的不能做任何其他事情,所以我认为在后台线程上进行核心数据调用可能是可以的,只要同时没有其他东西可以这样做。随后,我在代码周围使用了 NSLock 来更新保存数据以供显示的数组。我现在似乎无法引起任何异常。
我使用的基本方法在下面的代码片段中进行了说明,如下所示: 1.initWithNibName
填充初始数据以进行显示 2.awakeFromNib
设置 outlineView 并启动后台线程以获取第一组数据 3. 运行更新共享对象时的后台线程和阻塞 4. 当线程完成时触发在主线程上执行一个函数,以 a) 重新加载 outlineView(希望我知道如何只重新加载 NSOutlineView 的部分),以及 b) 踢下一部分提取的后台线程。
一般来说,这似乎工作正常,并且似乎为用户创造了更好的视觉体验,而不是简单地在主线程上运行它并观看彩色雨伞旋转几秒钟......
任何人都可以确认这是否是一种强大的方法 - 我不确定我是否完全理解 NSLock 的作用,我怀疑这种方法可能存在一些漏洞,但似乎无法打破它。
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Initialization code here.
_reminderCategories = [NSArray arrayWithObjects:REMINDERS_PAST, REMINDERS_TODAY, REMINDERS_THIS_WEEK, REMINDERS_THIS_MONTH, REMINDERS_THIS_YEAR, nil];
_reminderItems = [NSMutableDictionary new];
[_reminderItems setObject:[NSArray arrayWithObjects:@"Searching...", nil] forKey:REMINDERS_PAST];
[_reminderItems setObject:[NSArray arrayWithObjects:@"Searching...", nil] forKey:REMINDERS_TODAY];
[_reminderItems setObject:[NSArray arrayWithObjects:@"Searching...", nil] forKey:REMINDERS_THIS_WEEK];
[_reminderItems setObject:[NSArray arrayWithObjects:@"Searching...", nil] forKey:REMINDERS_THIS_MONTH];
[_reminderItems setObject:[NSArray arrayWithObjects:@"Searching...", nil] forKey:REMINDERS_THIS_YEAR];
}
return self;
}
- (void)awakeFromNib;
{
// Make sure we only do this once
if (!_awake) {
_awake = YES;
LOG(@"awakeFromNib called !");
//set the start menu option
[_sidebarOutlineView sizeLastColumnToFit];
[_sidebarOutlineView reloadData];
[_sidebarOutlineView setFloatsGroupRows:NO];
// Expand all the root items; disable the expansion animation that normally happens
[NSAnimationContext beginGrouping];
[[NSAnimationContext currentContext] setDuration:0];
[_sidebarOutlineView expandItem:nil expandChildren:YES];
[NSAnimationContext endGrouping];
if (!_searched) {
_searched=YES;
[self getPastRemindersUsingThread];
}
}
}
- (void)getPastRemindersUsingThread
{ //LOG(@"getPastRemindersUsingThread called");
_loading = YES;
[self performSelectorInBackground:@selector(threadGetPastReminders) withObject:nil];
}
- (void)threadGetPastReminders
{
//LOG(@"threadGetReminders called");
assert( ! [NSThread isMainThread] );
[NSThread sleepForTimeInterval:2]; // Delays for testing
[self makeMenus];
[self performSelectorOnMainThread:@selector(pastRemindersDone) withObject:nil waitUntilDone:NO];
}
- (void)pastRemindersDone {
//LOG(@"pastRemindersDone called");
[_sidebarOutlineView reloadData];
[self getTodaysRemindersUsingThread];
}
- (void)getTodaysRemindersUsingThread
{ //LOG(@"getTodaysRemindersUsingThread called");
_loading = YES;
[self performSelectorInBackground:@selector(threadGetReminders) withObject:nil];
}
- (void)threadGetReminders
{
//LOG(@"threadGetReminders called");
assert( ! [NSThread isMainThread] );
[self makeTodaysMenusX];
[self performSelectorOnMainThread:@selector(todaysRemindersDone) withObject:nil waitUntilDone:NO];
}
- (void)todaysRemindersDone {
//LOG(@"todaysRemindersDone called");
[_sidebarOutlineView reloadData];
[self getThisWeeksRemindersUsingThread];
}
// etc…
-(void)makeMenus {
//LOG(@"makeMenus called");
// Make sure nothing else is going on while we execute this code
NSLock *theLock = [[NSLock alloc] init];
if ([theLock tryLock]) {
[self makeDates];
[self getAllReminders];
[self makePastMenus];
}
[theLock unlock];
}
- (void)makePastMenus {
//LOG(@"makePastMenus called");
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"nextReminderDate < %@",_todayStartDate];
[self makeMenu:REMINDERS_PAST predicate:predicate];
}
- (void)makeTodaysMenus {…}
- (void)makeThisWeeksMenus {…}
- (void)makeThisYearsMenus {…}
// Use this to get the subset that fall within the specified dates (in the predicate)
// nextReminderDate is calculated, and not an attribute stored in the database
- (void)makeMenu:(NSString*)menu predicate:(NSPredicate*)predicate {
LOG(@"makeMenuX called");
NSLock *theLock = [[NSLock alloc] init];
if ([theLock tryLock]) {
if (_yearlyReminders) {
NSMutableArray *reminders = [[NSMutableArray alloc] init];
[reminders addObjectsFromArray:[_yearlyReminders filteredArrayUsingPredicate:predicate]];
NSSortDescriptor *indexSort = [[NSSortDescriptor alloc] initWithKey:@"nextReminderDate" ascending:YES];
NSArray *sorters = [NSArray arrayWithObject:indexSort]; indexSort = nil;
[_reminderItems setObject:[reminders sortedArrayUsingDescriptors:sorters] forKey:menu];
} else {
LOG(@" error utilities is nil!");
}
}
[theLock unlock];
LOG(@"makePastYearsMenus finished");
}
// Get all the reminders from the Core Data store
// _utilities getData is a helper function that performs the Core Data fetch and returns an array of NSManagedObjects
- (void)getAllReminders {
//LOG(@"getYearlyReminders called");
NSLock *theLock = [[NSLock alloc] init];
if ([theLock tryLock]) {
if (_utilities) {
NSPredicate *predicate = [...];
NSMutableArray *reminders = [[NSMutableArray alloc] init];
[reminders addObjectsFromArray:[_utilities getData:@"Entity1" sortKey:@"reminderDate" predicate:predicate]];
[reminders addObjectsFromArray:[_utilities getData:@"Entity2" sortKey:@"reminderDate" predicate:predicate]];
[reminders addObjectsFromArray:[_utilities getData:@"Entity3" sortKey:@"reminderDate" predicate:predicate]];
[reminders addObjectsFromArray:[_utilities getData:@"Entity4" sortKey:@"reminderDate" predicate:predicate]];
NSSortDescriptor *indexSort = [[NSSortDescriptor alloc] initWithKey:@"nextReminderDate" ascending:YES];
NSArray *sorters = [NSArray arrayWithObject:indexSort]; indexSort = nil;
_yearlyReminders = [[NSArray alloc] initWithArray:[reminders sortedArrayUsingDescriptors:sorters]];
} else {
LOG(@" error utilities is nil!");
}
}
[theLock unlock];
}