gpt4 book ai didi

ios - 后台线程困惑

转载 作者:行者123 更新时间:2023-11-28 22:31:47 25 4
gpt4 key购买 nike

我正在努力提高我的应用程序的响应能力,但我是线程的新手并且感到困惑。

启动时会显示一个警告:

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (buttonIndex == 0)
{
NSString *user = [[alertView textFieldAtIndex:0] text];
NSString *pass = [[alertView textFieldAtIndex:1] text];
[self loginToServerWithUsername:user andPassword:pass];
}
}

loginToServerWithUsername: 方法中,应用调用一个方法:

[self checkFiles:sessionID];

这可能需要几秒钟,所以我尝试在后台执行它:

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
[self checkFiles:sessionID];
});

检查文件方法:

fileList = [[NSMutableString alloc] init];

NSArray *directoryPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [directoryPaths objectAtIndex:0];
NSString *downloadsFolderString = [documentsDirectory stringByAppendingPathComponent:DOWNLOADS_FOLDER];
NSError *error = nil;
NSString* file;
NSDirectoryEnumerator* enumerator = [[NSFileManager defaultManager] enumeratorAtPath:downloadsFolderString];
while (file = [enumerator nextObject])
{
BOOL isDirectory = NO;
[[NSFileManager defaultManager] fileExistsAtPath: [NSString stringWithFormat:@"%@/%@",downloadsFolderString,file]
isDirectory: &isDirectory];

if (!isDirectory)
{
[fileList appendString:[NSString stringWithFormat:@"%@|", file]];
}
}
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"dd/MM/yyyy HH:mm"];
NSString *timeOpened = [formatter stringFromDate:[NSDate date]];

NSString *post = [NSString stringWithFormat:@"sessionID=%@&fileList=%@&dateTime=%@&userID=%@", sessionID, fileList, timeOpened, userID];
NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]];
NSString *comparisonURLString = SERVER_COMPARE_URL_STRING;
NSURL *comparisonURL = [NSURL URLWithString:comparisonURLString];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:comparisonURL];
[request setHTTPMethod:@"POST"];
[request addValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setHTTPBody:postData];
NSHTTPURLResponse *urlResponse = nil;
error = nil;
NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&urlResponse error:&error];

if (responseData)
{
NSString *requiredFilesList = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];

NSArray *lines = [requiredFilesList componentsSeparatedByString: @"\n"];
if (lines.count > 2)
{
dispatch_async(dispatch_get_main_queue(), ^(void){

NSRange theRange;
theRange.location = 2;
theRange.length = [lines count] -3;
numberOfFilesToBeDownloaded = theRange.length;

if (numberOfFilesToBeDownloaded <= 0)
{
_jobStatusLabel.text = @"Documents up to date"; // is this the issue?
}
if (numberOfFilesToBeDownloaded > 0)
{
NSArray *subArray = [lines subarrayWithRange:theRange];
[self animateProgressBar];
if (![eraseDevice isEqualToString:@"true"])
{
[self getFiles:subArray];
}
}
});
}
}
else
{
NSLog(@"no response data from check files");
}

但是,在 checkFiles 方法完成之前,alertView 不会被关闭。谁能告诉我如何在 checkFiles 在后台运行时关闭警报?

最佳答案

UI操作应该在主线程中完成。

例如:

_jobStatusLabel.text = @"Documents up to date"; // is this the issue?

应该是

dispatch_async(dispatch_get_main_queue(), ^(void){
_jobStatusLabel.text = @"Documents up to date"; // is this the issue?
});

改变所有这些可能的情况。

关于ios - 后台线程困惑,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17210950/

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