- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试通过 Objective-C++ 中的异步调度队列执行 block 。这是我正在尝试做的类片段...
class Blah {
public:
void dispatch(const EventPtr& event) {
dispatch_queue_t queue = dispatch_queue_create(_queueName.c_str(), NULL);
dispatch_async(queue, ^{
this->dispatchEventToSubscribers(event);
});
dispatch_release(queue);
}
protected:
Dude _dude;
void dispatchEventToSubscribers(const EventPtr& event) {
_dude.dispatchToSubscribers(event);
}
}
我在 dispatchEventToSubscribers 方法中得到一个 EXC_BAD_ACCESS。当我检查 _dude
的值是什么时,XCode 告诉我它超出了范围。我只能假设我以某种方式丢失了 this
。检查并发文档:
For blocks that you plan to perform asynchronously using a dispatch queue, it is safe to capture scalar variables from the parent function or method and use them in the block. However, you should not try to capture large structures or other pointer-based variables that are allocated and deleted by the calling context. By the time your block is executed, the memory referenced by that pointer may be gone. Of course, it is safe to allocate memory (or an object) yourself and explicitly hand off ownership of that memory to the block.
那么如何在 this
对象上异步分派(dispatch)一个方法呢?
谢谢!
最佳答案
出于某种原因,使 event
的本地实例有效...我不确定为什么...即...
void dispatch(const EventPtr& event) {
dispatch_queue_t queue = dispatch_queue_create(_queueName.c_str(), NULL);
EventPtr eventPtr = event; //local instance...
dispatch_async(queue, ^{
this->dispatchEventToSubscribers(eventPtr);
});
dispatch_release(queue);
}
关于concurrency - 使用 dispatch_async 的 EXC_BAD_ACCESS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5557120/
我如何在 dispatch_async 中正确调用 dispatch_async 调用 dispatch_group_t downloadQueue = dispatch_group_create()
我想按顺序添加一个 dispatch_async,但我不希望它们随机启动。我想举个例子: dispatch_async 1 开始... dispatch_async 1 结束。 dispatch_as
我正在尝试从 Firebase 下载一些带有图像的帖子并将其显示在我的表格 View 中。在下面的代码中,我注意到 tableview 仅在从 preLoadImage 函数调用下载每个图像后才加载。
我正在尝试学习 GCD,所以我还没有完全掌握它的工作原理。出于某种原因,我在调用以下方法后遇到帧率永久下降的情况。如果我不使用调度函数而只是在主循环中写入数据,帧速率将保持在 60。我不知道为什么。
我在将 JSON 数据存储到单个对象数组中时遇到问题。看起来问题出在处理 JSON 请求的 dispatch_asynch 的执行中。当我在方法之前创建一个断点而不是单步执行应用程序时,它似乎只是通过
我知道 dispatch_async 可以处理线程。 dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,
我有一个使用dispatch_async的函数: - (IBAction)action:(id)sender { int i=10000; NSString * data; d
我猜不到它的输出。 dispatch_async(serial_queue,^{NSlog(@"1");}); NSlog(@"2"); dispatch_async(serial_queue,^{N
NSString *firstID = @"https://wallpaperbrowse.com/media/images/soap-bubble-1958650_960_720.jpg"; 想象一
dispatch_queue_t queue = dispatch_queue_create("setup_cell", NULL); dispatch_async(queue, ^{ //L
我正在使用 here 的解决方案使 titleView clipsToBounds 始终为真。 我的 ViewController 中有这个并且运行良好,但是,如果我通过按后退按钮离开 ViewCon
我正在开发一个应用程序,我必须在其中从 JSON 获取数据并在 UITableView 中显示。正在后台获取数据。但它似乎进入了无限循环。 任何帮助将不胜感激。 - (UITableViewCell
我是 Swift 的新手,正在研究 dispatch_async 函数的工作原理。 API 文档显示 dispatch_async 有两个参数。但是,我可以传递一个参数,这没关系。 dispatch_
所以我运行了一段代码,我想提高性能,我注意到删除需要很长时间才能完成(大约 0.003 秒),所以我决定将它放入另一个线程,然后删除数组。 现在创建和运行线程所花费的时间比删除数组快得多,但是现在我创
我有以下代码... -(void) SetSerialNumber { NSLog(@"SetSerialNumber"); NSString *serialNum = textFie
假设我有以下代码: dispatch_async(dispatch_get_main_queue()) { myFunction() } 这表示异步调用调用 myFunction 的 bloc
我正在使用一些下载数据的代码。该代码使用 block 作为回调。有几种代码非常相似的下载方法:在回调 block 中,如果出现问题,它们会显示 UIAlertView。警报 View 始终如下所示:
我是 Swift 的新手,正在研究 dispatch_async 函数的工作原理。 API 文档显示 dispatch_async 有两个参数。但是,我可以传递一个参数,这没关系。 dispatch_
那么第一个问题就是dispatch_async是如何决定使用哪个线程的呢?只是随机选择它?我需要做一些解析和核心数据的事情,所以我不想阻塞 UI 线程并使用 dispatch_async,但在那之后我
假设我将任务异步分派(dispatch)到队列: { // we are on main queue dispatch_async(dispatch_get_global_queue(
我是一名优秀的程序员,十分优秀!