Implementing long running tasks in background IOS

前端 未结 3 663
北恋
北恋 2020-12-14 04:45

I have been working on an app in which user can record video using AVFoundation and send to the server, video has maximum size up to 15M, depending on the inter

相关标签:
3条回答
  • 2020-12-14 04:51

    From your response to Dwayne, you do not need to be able to download in background mode. Rather what you need is to do your download in another thread (background thread) beside main thread. Something like this for GCD:

        dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
    //        Do you download here...
        });
    
    0 讨论(0)
  • 2020-12-14 04:58

    NSOperationQueue is the recommended way to perform multi-threaded tasks to avoid blocking the main thread. Background thread is used for tasks that you want to perform while your application is inactive, like GPS indications or Audio streaming.

    If your application is running in foreground, you don't need background thread at all.

    For simple tasks, you can add a operation to a queue using a block:

    NSOperationQueue* operationQueue = [[NSOperationQueue alloc] init];
    [operationQueue addOperationWithBlock:^{
        // Perform long-running tasks without blocking main thread
    }];
    

    More info about NSOperationQueue and how to use it.

    The upload process will continue while in background, but your application will be eligible to be suspended, and thus the upload may cancel. To avoid it, you can add the following code to application delegate to tell the OS when the App is ready to be suspended:

    - (void)applicationWillResignActive:(UIApplication *)application {
        bgTask = [application beginBackgroundTaskWithExpirationHandler:^{
    
          // Wait until the pending operations finish
          [operationQueue waitUntilAllOperationsAreFinished];
    
          [application endBackgroundTask: bgTask];
          bgTask = UIBackgroundTaskInvalid;
        }]; 
    }
    
    0 讨论(0)
  • 2020-12-14 05:13

    Your requirement qualifies to run in background. You do not need to register any background modes supported in the info plist. All you need to do is, when the app is about to go into background, request for additional time using background task handler and perform your task in that block. Make sure you stop your handler before 10 mins so as to not get force terminated by the OS.

    You may use the below code from Apple.

    - (void)applicationDidEnterBackground:(UIApplication *)application
    {
    bgTask = [application beginBackgroundTaskWithExpirationHandler:^{
        // Clean up any unfinished task business by marking where you
        // stopped or ending the task outright.
        [application endBackgroundTask:bgTask];
        bgTask = UIBackgroundTaskInvalid;
    }];
    
    // Start the long-running task and return immediately.
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
    
        // Do the work associated with the task, preferably in chunks.
    
        [application endBackgroundTask:bgTask];
        bgTask = UIBackgroundTaskInvalid;
    });}
    
    0 讨论(0)
提交回复
热议问题