gpt4 book ai didi

objective-c - copyItemAtPath -path 显示在后台线程中创建的 plist 为 null

转载 作者:行者123 更新时间:2023-11-28 20:40:30 25 4
gpt4 key购买 nike

我有一个检查 plist 是否存在的函数,如果不存在,它将创建 plist。

此函数在后台调用,即[self performSelectorInBackground:@selector(CreatePlist:) withObject:xyz];

一切都很完美。

但是当我尝试从主线程的 plist 中获取数据时,即

NSString *path = [[NSBundle mainBundle] pathForResource:@"calendar" ofType:@"plist"];
NSLog(@"path is %@", path); // path comes null
fileName = [fileName stringByReplacingOccurrencesOfString:@"/" withString:@""];
NSLog(@"file name data contain %@",fileName);
NSFileManager *fileManager = [NSFileManager defaultManager];

//see if Data.plist exists in the Documents directory
if (![fileManager fileExistsAtPath:[self saveMyPath:fileName] ]) {
[fileManager copyItemAtPath:path toPath:[self saveMyPath:@""] error:nil]; //at this point the app crashes saying path is null
}

当我将我的应用程序从后台移至主线程时,它运行良好。这里有什么问题?

最佳答案

看起来当主线程到达时后台线程还没有完成。

你可以尝试使用 grand central dispatch.. 例如

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^{
[self CreatePlist:xyz];
dispatch_async(dispatch_get_main_queue(), ^{
NSString *path = [[NSBundle mainBundle] pathForResource:@"calendar" ofType:@"plist"];
NSLog(@"path is %@", path); // path comes null
fileName = [fileName stringByReplacingOccurrencesOfString:@"/" withString:@""];
NSLog(@"file name data contain %@",fileName);
NSFileManager *fileManager = [NSFileManager defaultManager];

//see if Data.plist exists in the Documents directory
if (![fileManager fileExistsAtPath:[self saveMyPath:fileName] ]) {
[fileManager copyItemAtPath:path toPath:[self saveMyPath:@""] error:nil]; //at this point the app crashes saying path is null
});
});

关于objective-c - copyItemAtPath -path 显示在后台线程中创建的 plist 为 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8738882/

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