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.

iosfreak
  • 5,228
  • 11
  • 59
  • 102

1 Answers1

1

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.

Community
  • 1
  • 1
  • Thanks for the quick response! I am making a iOS app. How exactally would I go from transforming a statement like this `-(void)doAction { //code }` to the methods you suggested? Thanks! – iosfreak Feb 21 '11 at 23:41
  • Bear in mind that I can't say the example is the correct or safe way to do this. There're a number of things you need to do and keep in mind when doing this stuff (for example, never do anything with the UI outside of the main/UI thread). So, you'll want to go read Apple's documentation on concurrency and such as well. –  Feb 21 '11 at 23:57
  • Thanks for your code and time. I tried the NSThread and it works perfectly! Instead of calling it from MYTIMER, I just used this bit of code: `[NSThread detachNewThreadSelector:@selector(startUploads) toTarget:self withObject:nil];` – iosfreak Feb 22 '11 at 00:56