这是previous post regarding the CHCSVParser from Dave DeLong的后续行动。简而言之,我使用CHCSVParser
处理一个非常大的CSV文件,并将这些行解析为Core Data实体(在iPhone上)。我已经将解析器连接起来并正在工作,现在我正在尝试向用户显示进度指示器。
我要求应用程序的用户提供要使用模态视图控制器导入的列表的名称,但我遇到的问题是,在解析器运行时屏幕看起来“卡住”并且模式VC显示为冻结(看起来并没有被解雇)。无论我如何安排代码,似乎解析器在运行时锁定所有内容,因此模态VC不会被动画化,并且隐藏了进度指示器(简单的UIView和UILabel)。
我以前从未编写过线程编程,但这听起来有点像我。我该如何解决这个问题?
谢谢!
代码:
==== CALLBACK METHOD FROM THE MODAL VC ====
//
// Dismiss the modal VC
//
[self dismissModalViewControllerAnimated:NO];
//
// Set up the progress indicator view
//
progressView = [[UIView alloc] initWithFrame:CGRectMake(0,0,320,480)];
progressView.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:.9];
UILabel *progressLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 150, 300, 60)];
progressLabel.backgroundColor = [UIColor clearColor];
progressLabel.textAlignment = UITextAlignmentCenter;
progressLabel.tag = 12345;
progressLabel.text = NSLocalizedString(@"Beginning import...",@"");
progressLabel.textColor = [UIColor whiteColor];
[progressView addSubview:progressLabel];
[self.view addSubview:progressView];
//
// If a name was provided, then create a List with the given name then parse the
// selected CSV file into it
//
if (listName != nil) {
// Create the new List object and set the currentList to point to it.
NSError *error = nil;
NSEntityDescription *ed = [NSEntityDescription entityForName:@"FOList" inManagedObjectContext:managedObjectContext];
FOList *theList = [NSEntityDescription insertNewObjectForEntityForName:[ed name] inManagedObjectContext:managedObjectContext];
theList.name = listName;
if (![managedObjectContext save:&error]) {
NSLog(@"Error saving the new list! %@ %@", [error localizedDescription], [error userInfo]);
}
currentList = theList;
//
// Grab the appropriate file
//
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSURL *inputFileURL = [NSURL fileURLWithPath: [documentsDirectory stringByAppendingPathComponent:selectedFileName]];
//
// Start the parsing
//
NSStringEncoding encoding = 0;
CHCSVParser *p = [[CHCSVParser alloc] initWithContentsOfCSVFile:[inputFileURL path] usedEncoding:&encoding error:nil];
[p setParserDelegate:self];
numRowsParsed = [NSNumber numberWithInt:0];
[p parse];
[p release];
答案 0 :(得分:1)
在这里回答了我自己的问题。经过一番挖掘后,我找到了一篇好文章here。 NSOperationQueues在这里有点矫枉过正,我所需要的只是方法– performSelectorInBackground:withObject:
和– performSelectorOnMainThread:withObject:waitUntilDone:
。这些方法将为您处理背景和主线程,并使其变得简单!
答案 1 :(得分:0)
NSOperation或NSInvocationOperation的完美工作。