[英]“initialiser element is not a compile-time constant” while creating GCD queue
我的错实际上是在实施之外编写此代码,这真是个天真的错误
我正在学习GCD队列并创建要在后台运行的代码块,
-(IBAction) refresh:(id) sender
{
dispatch_queue_t downloadQueue = dispatch_queue_create("app data", NULL);
dispatch_async(downloadQueue, ^{
//. . . Call a method which download XML file from server . . .
dispatch_async(dispatch_get_main_queue(), ^{
//. . . Update UI with dowanloaded data . . .
});
});
dispatch_release(downloadQueue);
}
但是这行代码显示了编译错误
dispatch_queue_t downloadQueue = dispatch_queue_create("eiap data", NULL);
错误 : 初始化程序元素不是编译时常量
我可以说我正在动态创建A的“应用数据” const char出了点问题,但是我不知道这是怎么回事?
谢谢
如果执行此操作,它将编译:
dispatch_queue_t downloadQueue = dispatch_queue_create("app data", NULL);
但是如果这样做,我会得到您提到的错误:
static dispatch_queue_t downloadQueue = dispatch_queue_create("app data", NULL);
我认为您是在声明它为静态。 或者您实际上是在方法主体之外将其声明为全局变量。
摆脱初始化字符串中的空格。
使用类似:
dispatch_queue_t downloadQueue = dispatch_queue_create("com.eiap.dataTask", NULL);
Apple建议使用反向DNS表示法样式字符串。
这是您可以参考的教程,希望我的回答对您有所帮助!
尝试使用dispatch_queue_t downloadQueue = dispatch_queue_create(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
这是您描述的我的实现:
UIAlertView *av =[[UIAlertView alloc] initWithTitle:@"Loading Data" message:@"" delegate:self cancelButtonTitle:nil otherButtonTitles:nil];
UIActivityIndicatorView *ActInd=[[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
[UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
[ActInd startAnimating];
[ActInd setFrame:CGRectMake(125, 60, 37, 37)];
[av addSubview:ActInd];
[av show];
dispatch_queue_t callerQueue = dispatch_get_main_queue();
//dispatch_retain(callerQueue);
dispatch_queue_t downloadQueue = dispatch_queue_create(DISPATCH_QUEUE_PRIORITY_DEFAULT,
0);
dispatch_async(downloadQueue, ^{
[self doLoadData];
dispatch_async(callerQueue, ^{
[av dismissWithClickedButtonIndex:0 animated:YES];
[av release];
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
[self.tableView performSelectorOnMainThread:@selector(reloadData) withObject:nil waitUntilDone:NO];
});
dispatch_release(downloadQueue);
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.