在后台运行无效操作?

发布于 2024-10-18 06:23:04 字数 107 浏览 0 评论 0原文

我有一个简单的应用程序,允许人们将图片上传到服务器。由于某种原因,在上传每个文件时,应用程序会冻结,并且不允许您对其执行任何其他操作。

有没有办法在后台运行它?

谢谢。

I have a simple app that allows people to upload pictures to a server. For some reason, while it's uploading each file the app freezes and won't allow you to do anything else on it.

Is there anyway to run it in the background?

Thanks.

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

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

发布评论

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

评论(1

给我一枪 2024-10-25 06:23:04

在 iOS 或 Mac OS 上,您可以使用 NSThread, NSOperation,或 libDispatch(在 iOS 4 或 Mac OS 10.6 上)。还有 pthreads,不过我不确定 iOS 上是否可用。有关更多信息,您可能需要查看这个其他问题(其中针对 iOS 应用程序,但我认为同样适用于 Mac OS)。

编辑(回复评论):最简单的方法(或者我猜我更喜欢的方法,也许它不是那么简单,甚至不是一个好主意)可能是使用 libDispatch 和类似的东西:

- (IBAction) beginUpload {
    // grab a global concurrent queue
    dispatch_queue_t globalQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, NULL);
    // throw a block in it
    dispatch_async(^{
            [self uploadImages];
        }, globalQueue);
}

- (void) uploadImages {
    // where I suppose images is an array of images or something
    @synchronized(images) {
        // upload stuff here, or copy images and then
    }
    // upload stuff here
}

无论这是否是 正确的做事方式,我不知道。您必须为此做进一步的研究(再次参考我上面指出的另一个问题)。另外,我不确定该代码是否完全正确,因为我只是在文本编辑器中编写了它,而没有通过编译器运行它。

On either iOS or Mac OS, you could handle that in a separate thread using NSThread, NSOperation, or libDispatch (on iOS 4 or Mac OS 10.6). There's also pthreads, though I'm not sure whether that's available on iOS. For more information, you might want to check this other question (which is aimed at iOS apps, but I think could apply equally well to Mac OS).

Edit (response to comment): The simplest way (or I guess my preferred way, maybe it's not so simple, or even a good idea) would probably be to use libDispatch and something like this:

- (IBAction) beginUpload {
    // grab a global concurrent queue
    dispatch_queue_t globalQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, NULL);
    // throw a block in it
    dispatch_async(^{
            [self uploadImages];
        }, globalQueue);
}

- (void) uploadImages {
    // where I suppose images is an array of images or something
    @synchronized(images) {
        // upload stuff here, or copy images and then
    }
    // upload stuff here
}

Whether or not this is a correct way to do things, I don't know. You would have to do further research for that (again, refer to the other question I pointed out above). Also, I'm not sure if that code is entirely correct, since I just wrote it up in a text editor without running it through a compiler.

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