gpt4 book ai didi

objective-c - 主线程中的 CHCSVParser

转载 作者:行者123 更新时间:2023-12-03 17:17:02 25 4
gpt4 key购买 nike

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

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

最佳答案

在这里回答我自己的问题。经过一番挖掘,我发现了一篇好文章 here 。 NSOperationQueues 在这里有点矫枉过正,我所需要的只是方法 -performSelectorInBackground:withObject:-performSelectorOnMainThread:withObject:waitUntilDone:。这些方法将为您处理后台和主线程,让一切变得简单!

关于objective-c - 主线程中的 CHCSVParser,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3798577/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com