Inside my iOS application, I am using Core Data to do a fetch, and delete of a very large data set. This process takes approximately 5-10 seconds. What I would like to do is perform a rollback in case the user decides to turn the device off before the process has completed. However, the problem is to have the SAME instance of the NSManagedObjectContext to call the rollback function from the appropriate AppDelegate method. Within my application, I call my Core Data methods using a Singleton object like this:

static MySingleton *sharedSingleton = nil;


+ (MySingleton *) sharedInstance {

    if (sharedSingleton == nil) {
        sharedSingleton = [[super alloc] init];
    }
    return sharedSingleton;
}

In my application, I return an instance of an NSManagedObjectContext like this:

- (NSManagedObjectContext *) managedObjectContext{

    if (_managedObjectContext != nil) {
        return _managedObjectContext;
    }
    NSPersistentStoreCoordinator *coordinator = [self persistentStoreCoordinator];

    if (coordinator != nil) {

        _managedObjectContext = [[NSManagedObjectContext alloc]initWithConcurrencyType:NSMainQueueConcurrencyType];
        [_managedObjectContext setPersistentStoreCoordinator:coordinator];

        //Undo Support
        NSUndoManager *anUndoManager = [[NSUndoManager alloc] init];
        [self.managedObjectContext setUndoManager:anUndoManager];
    }
    return _managedObjectContext;
}

I then call it, and assign it to a reference like this:

NSManagedObjectContext *context = [[MySingleton sharedInstance] managedObjectContext];

How would I make this instance of the ManagedObjectContext available to me for use in the AppDelegate, so that I can call the rollback function?

Thanks in advance to all who reply.

Related posts

Recent Viewed