我很糟糕我实际上是在外面写这个代码,这是一个很大的错误
我正在学习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);
错误:初始化元素不是编译时常量
我可以说“app data”是一个错误,我正在创建一个const char,但是我不知道它有什么问题?
由于
答案 0 :(得分:4)
如果我这样做,它会编译:
dispatch_queue_t downloadQueue = dispatch_queue_create("app data", NULL);
但如果我这样做,我会得到你提到的错误:
static dispatch_queue_t downloadQueue = dispatch_queue_create("app data", NULL);
我认为你宣称它是静态的。或者你实际上是在方法体之外将其声明为全局变量。
答案 1 :(得分:2)
删除初始化字符串中的空格。
使用类似:
dispatch_queue_t downloadQueue = dispatch_queue_create("com.eiap.dataTask", NULL);
Apple建议使用反向DNS表示法样式字符串。
Here's a tutorial you can refer to, b.t.w.我希望我的回答可以帮助你!
答案 2 :(得分:1)
尝试使用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);
});