Note that there are some explanatory texts on larger screens.

plurals
  1. POBackground task block function not finishing
    primarykey
    data
    text
    <p>I'm working on an iphone app that occasionally fires a task in the background to rearrange some data and upload it to a server. I've used a lot of the principles from <a href="https://stackoverflow.com/questions/4264540/grand-central-dispatch-gcd-with-coredata">Grand Central Dispatch (GCD) with CoreData</a> to get things running, since I'm editing objects that persist in Core Data, but the code only occasionally finishes running despite the application saying it has almost the full 600 seconds of execution time remaining.</p> <p>The code i'm using:</p> <pre><code>__block UIBackgroundTaskIdentifier bgTask; UIApplication *application = [UIApplication sharedApplication]; //Get the shared application instance NSLog(@"BackgroundTimeRemaining before block: %f", application.backgroundTimeRemaining); 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. NSLog(@"BackgroundTimeRemaining after block: %f", application.backgroundTimeRemaining); NSLog(@"Fixing item in the background"); //Create secondary managed object context for new thread NSManagedObjectContext *backgroundContext = [[NSManagedObjectContext alloc] init]; [backgroundContext setPersistentStoreCoordinator:[self.managedObjectContext persistentStoreCoordinator]]; /* Save the background context and handle the save notification */ [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(backgroundContextDidSave:) name:NSManagedObjectContextDidSaveNotification object:backgroundContext]; //creating runloop to kill location manager when done NSDate *stopDate = [[NSDate date] dateByAddingTimeInterval:60]; [[NSRunLoop currentRunLoop] runUntilDate:stopDate]; NSLog(@"Stop time = %@", stopDate); MasterViewController *masterViewContoller = [[MasterViewController alloc] init]; masterViewContoller.managedObjectContext = backgroundContext; [[masterViewContoller locationManager] startUpdatingLocation]; NSLog(@"Successfully fired up masterViewController class"); [masterViewContoller adjustDataInBackground:FALSE]; NSLog(@"Fixed Object!"); //save background context [backgroundContext save:NULL]; //unregister self for notifications [[NSNotificationCenter defaultCenter] removeObserver:self name:NSManagedObjectContextDidSaveNotification object:backgroundContext]; [application endBackgroundTask:bgTask]; bgTask = UIBackgroundTaskInvalid; }); </code></pre> <p>The issue is that "adjustDataInBackground:FALSE" is a pretty long method that calls additional supporting methods (including creation and saving of core data objects), and when the background task doesn't allow all of those methods to finish it corrupts my data.</p> <p>Is there a better way of handling this kind of an operation? Do i need to put all my raw code into the background task block directly?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload