我正在尝试使用大中央调度设置和 NSURL,但是在您尝试在大中央调度块之外访问它之前,该变量似乎已设置并可访问。
-(void)viewDidLoad {
[super viewDidLoad];
dispatch_queue_t backgroundQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
dispatch_async(backgroundQueue,^{
self.ubiquitousURL = [[NSFileManager defaultManager] URLForUbiquityContainerIdentifier:nil];
NSLog(@"ubiq inside: %@", self.ubiquitousURL);
if (self.ubiquitousURL) {
self.iCloudDocURL = [NSURL URLWithString:[NSString stringWithFormat:@"%@Documents", self.ubiquitousURL]];
self.iCloudDocString = [self.iCloudDocURL absoluteString];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(loadFiles) name: UIApplicationDidBecomeActiveNotification object:nil];
} else {
/* change to the main queue if you want to do something with the UI. For example: */
dispatch_async(dispatch_get_main_queue(),^{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Please enable iCloud" message:nil delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alert show];
});
}
});
NSLog(@"ubiq outside: %@", self.ubiquitousURL);
}
第一个NSLog
以ubiq inside
返回正确的 URL 开头,而ubiq outside
返回 NULL。我正在使用 ARC,因此无需提及内存或类似内容……这是 GCD 问题。
你知道为什么self.ubiquitousURL
在 GCD 块之外无法访问吗?谢谢。