2010-09-26 38 views
1

这是previous post regarding the CHCSVParser from Dave DeLong的后续操作。总之,我使用CHCSVParser来处理一个非常大的CSV文件,并将这些行解析到核心数据实体(在iPhone上)。我已经将解析器连接起来并工作,现在正在尝试向用户显示进度指示器。主线程中的CHCSVParser

我要求应用程序的用户提供一个名称为模态视图控制器导入列表,但我遇到的问题是屏幕看起来“卡住”,而解析器运行时,模态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]; 

回答

1

在这里回答了我自己的问题。经过一番挖掘,我发现一篇好文章here。 NSOperationQueues在这里有点矫枉过正,我需要的只是方法– performSelectorInBackground:withObject:– performSelectorOnMainThread:withObject:waitUntilDone:。这些方法将为您处理背景和主线,并使它像馅饼一样简单!

+0

+1这可能是最简单的方法(因为'CHCSVParser'是同步的),但我不确定你会显示什么样的进度,因为事先没有办法知道有多少csv行在文件(除非你以其他方式弄清楚)。如果你所做的只是增加一个计数器,那么这很好。 – 2010-09-26 23:05:14

+0

我正在做的是覆盖一个带有白色文本UILabel的黑色半透明UIView,它显示每100条记录左右处理的记录数。数据文件在手机上处理大约需要5分钟,所以我只想让用户知道应用程序未锁定。再次感谢! – 2010-09-27 01:36:22

0

为的NSOperation或NSInvocationOperation完美的工作。