如何在加载数据时显示UIActivityInductionView
我试图在 viewDidLoad 中加载数据时显示指示器视图,但加载在我显示指示器之前开始,我想这是因为视图直到之后才加载。我已经阅读了一些有关此内容的内容,但无法使其发挥作用。
我想要的是在将文件加载到数据库期间显示活动指示器。
我一直在进行测试,因此代码结构可能看起来有点奇怪。
有人可以给我一个提示或链接,告诉我如何解决这个问题,以便在/如果数据从 .txt 文件加载到数据库中时显示活动指示器吗?
- (void)viewDidLoad {
[super viewDidLoad];
self.title = @" ";
loadActivityIndicator.hidden = TRUE; // Hide UIActivityIndicationView at start
[self loadDataIfNeeded];
}
-(void)loadDataIfNeeded {
NSFileManager *fileManager = [NSFileManager defaultManager];
myFileArray = [[NSMutableArray alloc]init];
//======SET THE FILE INPUT NAME======//
qFileTxtName = @"110615";
[myFileArray addObject:qFileTxtName];
//===================================//
// CHECK IF THE FILE EXISTS
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *path = [documentsDirectory stringByAppendingPathComponent:@"checkQuestionFile.plist"];
if([fileManager fileExistsAtPath:path]) {
NSArray *readKeyFileName = [[NSArray alloc] initWithContentsOfFile:path];
if ([[readKeyFileName objectAtIndex:0] isEqualToString:qFileTxtName]) {
//==== THERE IS NO UPDATED QUESTION .txt FILE ====//
}
else {
//==== THERE IS AN UPDATED QUESTION .txt FILE ====//
// SET UP UIActivityIndicator view to show that work is ongoing
loadActivityIndicator.hidden = FALSE;
[loadActivityIndicator startAnimating];
//==== SET UP PATH FOR ALL FILES====//
NSString *aString = [[NSString alloc]init];
aString = [@"questions_famquiz_hard_" stringByAppendingString:qFileTxtName];
NSString *path1 = [[NSBundle mainBundle] pathForResource:aString ofType:@"txt"];
aString = [@"questions_famquiz_medium_" stringByAppendingString:qFileTxtName];
NSString *path2 = [[NSBundle mainBundle] pathForResource:aString ofType:@"txt"];
aString = [@"questions_famquiz_easy_" stringByAppendingString:qFileTxtName];
NSString *path3 = [[NSBundle mainBundle] pathForResource:aString ofType:@"txt"];
AccessQuestionsDB *accessQuestionDataFunction = [AccessQuestionsDB new];
idCounter = [accessQuestionDataFunction populateTheDatabase: path1 theID:0 firstTime: YES];
idCounter = [accessQuestionDataFunction populateTheDatabase: path2 theID:idCounter firstTime: NO];
idCounter = [accessQuestionDataFunction populateTheDatabase: path3 theID:idCounter firstTime: NO];
//==UPDATE THE PLIST==//
[myFileArray writeToFile:path atomically: TRUE];
// Stop UIActivityIndicator as activity is over
loadActivityIndicator.hidden = TRUE;
[loadActivityIndicator stopAnimating];
}
} else {
//== If file not found write a new file ==//
[myFileArray addObject:qFileTxtName];
[myFileArray writeToFile:path atomically: TRUE];
}
}
问题已解决
我更换了通话
[自加载数据如果需要];
和;
[NSThread detachNewThreadSelector:@selector(loadDataIfNeeded) toTarget:self withObject:nil];
根据 Jonah 的建议实现多线程:-)
I am trying to show an indicator view when loading data in viewDidLoad but the loading starts before i display the indicator, i guess that is because the view is not loaded until after. I have been reading a bit about this but just cannot get it to work.
What i would want is to display the activity indicator during the loading of the files into the database.
I have been doing testing so the code structure may look a bit weird.
Could someone nice please give me a hint, or a link, how to fix this so the activity indicator is shown when/if data is loaded from the .txt files into the DB?
- (void)viewDidLoad {
[super viewDidLoad];
self.title = @" ";
loadActivityIndicator.hidden = TRUE; // Hide UIActivityIndicationView at start
[self loadDataIfNeeded];
}
-(void)loadDataIfNeeded {
NSFileManager *fileManager = [NSFileManager defaultManager];
myFileArray = [[NSMutableArray alloc]init];
//======SET THE FILE INPUT NAME======//
qFileTxtName = @"110615";
[myFileArray addObject:qFileTxtName];
//===================================//
// CHECK IF THE FILE EXISTS
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *path = [documentsDirectory stringByAppendingPathComponent:@"checkQuestionFile.plist"];
if([fileManager fileExistsAtPath:path]) {
NSArray *readKeyFileName = [[NSArray alloc] initWithContentsOfFile:path];
if ([[readKeyFileName objectAtIndex:0] isEqualToString:qFileTxtName]) {
//==== THERE IS NO UPDATED QUESTION .txt FILE ====//
}
else {
//==== THERE IS AN UPDATED QUESTION .txt FILE ====//
// SET UP UIActivityIndicator view to show that work is ongoing
loadActivityIndicator.hidden = FALSE;
[loadActivityIndicator startAnimating];
//==== SET UP PATH FOR ALL FILES====//
NSString *aString = [[NSString alloc]init];
aString = [@"questions_famquiz_hard_" stringByAppendingString:qFileTxtName];
NSString *path1 = [[NSBundle mainBundle] pathForResource:aString ofType:@"txt"];
aString = [@"questions_famquiz_medium_" stringByAppendingString:qFileTxtName];
NSString *path2 = [[NSBundle mainBundle] pathForResource:aString ofType:@"txt"];
aString = [@"questions_famquiz_easy_" stringByAppendingString:qFileTxtName];
NSString *path3 = [[NSBundle mainBundle] pathForResource:aString ofType:@"txt"];
AccessQuestionsDB *accessQuestionDataFunction = [AccessQuestionsDB new];
idCounter = [accessQuestionDataFunction populateTheDatabase: path1 theID:0 firstTime: YES];
idCounter = [accessQuestionDataFunction populateTheDatabase: path2 theID:idCounter firstTime: NO];
idCounter = [accessQuestionDataFunction populateTheDatabase: path3 theID:idCounter firstTime: NO];
//==UPDATE THE PLIST==//
[myFileArray writeToFile:path atomically: TRUE];
// Stop UIActivityIndicator as activity is over
loadActivityIndicator.hidden = TRUE;
[loadActivityIndicator stopAnimating];
}
} else {
//== If file not found write a new file ==//
[myFileArray addObject:qFileTxtName];
[myFileArray writeToFile:path atomically: TRUE];
}
}
PROBLEM SOLVED
I replaced the call
[self loadDataIfNeeded];
with;
[NSThread detachNewThreadSelector:@selector(loadDataIfNeeded) toTarget:self withObject:nil];
to achieve multi threading according to the recommendation from Jonah :-)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您正在主线程中同步加载数据。这意味着负责绘制 UI 的主线程正忙于加载数据,并且在您的
loadDataIfNeeded
方法完成之前没有机会更新您的视图(此时您不需要无论如何,我不想再显示您的活动指示器)。在主线程上显示活动指示器,然后允许主线程的运行循环继续,并在辅助线程上异步执行昂贵的操作(例如加载数据或执行网络请求)。
查看
NSThread
、NSObject
的-performSelectorInBackground:withObject:
和NSOperationQueue
以了解执行任务的不同选项主线程的。You are loading your data synchronously in the main thread. That means that the main thread, the one responsible for drawing the UI, is busy loading your data and will not have a chance to update your view until after your
loadDataIfNeeded
method finishes (at which point you don't want to show your activity indicator to be visible anymore anyway).Display your activity indicator on the main thread but then allow the main thread's run loop to continue and instead perform expensive operations (like loading data or performing network requests) asynchronously on a secondary thread.
Look at
NSThread
,NSObject
's-performSelectorInBackground:withObject:
, andNSOperationQueue
for different options for performing tasks off of the main thread.马特·德兰斯 (Matt Drance) 所著的《IOS 食谱》一书中有一个食谱,讲述了如何最好地做到这一点。您可以从书中免费摘录此食谱,网址为 http://media.pragprog。 com/titles/cdirec/activity.pdf
您可以在书页上找到该食谱的源代码:http://pragprog.com/titles/cdirec/ios-recipes
The book "IOS Recipes" by Matt Drance has a recipe that talks about how to do this best. You can get this recipe as a free excerpt from the book at http://media.pragprog.com/titles/cdirec/activity.pdf
You can find the source code for the recipe on the book page at http://pragprog.com/titles/cdirec/ios-recipes
在您的 viewDidLoad 中添加
然后在您的加载方法中完成加载后,只需从超级视图中删除您的活动指示器。
因此,在加载后的 loadDataIfNeeded 中添加
[[self.view viewWithTag:99] removeFromSuperview];
In your viewDidLoad add
Then in your load method after you finish loading just remove your activity indicator from super view.
So in your loadDataIfNeeded after loading add
[[self.view viewWithTag:99] removeFromSuperview];