在后台运行特定方法...但是

发布于 2024-12-09 08:15:08 字数 902 浏览 3 评论 0原文

想象一下我想在后台调用这个方法。

- (void)loadView
{
    [super loadView];
    //Make a button which will fire the "buttonClicked" method
    //Make my own uitableview.
    //Fire method: "theBackGroundMethod" in the background.
}

对于其中的含糊之处,我深表歉意,但我会尽力说得清楚。 以下是我想做的事情。 我有一个视图,但它上面有这个按钮和一个空的 UITableView。 然后我希望 BackGroundMethod 运行后台,在运行时我希望能够单击按钮并触发 buttonClicked 方法,该方法会将数据射入 UITableView。

我尝试过以下操作,但结果是否定的:

[self performSelectorInBackground:@selector(theBackGroundMethod) withObject:nil];

这会启动 BackGroundMethod,但它不会加载我从服务器请求的数据。

[self performSelectorOnMainThread:@selector(theBackGroundMethod) withObject:nil waitUntilDone:NO];

这会启动BackGroundMethod,但由于某种原因它不允许我单击按钮,它允许单击按钮,但在该方法触发时我所做的每个操作都会将单击放入其后面的队列中,因此只有当backgroundmethod我按了 x 次按钮就完成了。

有谁知道什么可能会阻止使用上述任何执行选择器函数以及如何解决它们。或者有一个完整的(也许更好的)想法..?

Imagine I have this method I want to call in the background.

- (void)loadView
{
    [super loadView];
    //Make a button which will fire the "buttonClicked" method
    //Make my own uitableview.
    //Fire method: "theBackGroundMethod" in the background.
}

I'm sorry about the vagueness but I'll try to be as clear as I can.
The following is what I want to do.
I have a view, I but this button on it and an empty UITableView.
Then I want theBackGroundMethod to run the the background, while this is running I want to be able to click the button and fire the buttonClicked method which will shoot data into the UITableView.

The following I have tried with negative results:

[self performSelectorInBackground:@selector(theBackGroundMethod) withObject:nil];

This starts theBackGroundMethod but it doesn't load the data which I ask from the server.

[self performSelectorOnMainThread:@selector(theBackGroundMethod) withObject:nil waitUntilDone:NO];

This starts theBackGroundMethod but it doesn't allow me to click the button for some reason, well it allows be to click the button but every action I do while the method is firing the clicks are put in queue behind it, so only when the backgroundmethod is done the x times I pressed the button is handled.

Does anyone have an idea what might block the use of any of the performSelector-functions described above and how to resolve them. Or have an entire (maybe better) idea..?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

指尖上得阳光 2024-12-16 08:15:08

如果您正在运行异步 URL 请求或类似的内容,则需要在请求所在的线程中运行运行循环。这就是为什么它在已经有运行循环的主线程上工作的原因。

If you're running async URL requests or anything like that, you'll need to run a runloop in the thread the requests are on. That would be why it works on the main thread, which already has a runloop.

紅太極 2024-12-16 08:15:08

我有同样的问题,我使用的是 UITable 所以在后台运行的方法中,在它的末尾我添加了这一行

[self.tableView reloadData];

我不知道您是否使用表视图,但这可能会让您找到解决问题的方法,我应该感谢你在后台运行;)

I have the same problem, I was using a UITable so in the methode running at the background, at the end of it I added this line

[self.tableView reloadData];

I don't know if your using a tableview, but this might get you someway to solve your problem, I should thank you for run in background ;)

写给空气的情书 2024-12-16 08:15:08

尝试改为:

[NSThread detachNewThreadSelector:@selector(theBackGroundMethod:) toTarget:self withObject:nil];

Try instead:

[NSThread detachNewThreadSelector:@selector(theBackGroundMethod:) toTarget:self withObject:nil];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文