I have a simple app that all开发者_如何学Pythonows 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.
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.
精彩评论