- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
这是 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/
有人可以向我澄清主线 DHT 规范中的声明吗? Upon inserting the first node into its routing table and when starting up th
我正在尝试使用 USB 小工具驱动程序使嵌入式设备作为 MTP 设备工作。 我知道 Android 从大容量存储设备切换到 MTP 设备已经有一段时间了,并且找到了 source code for M
我是一名优秀的程序员,十分优秀!