Hashmat Khalil
Hashmat Khalil

Reputation: 1816

NSManagedObjectContext cancel operation while in performBlockAndWait

how to cancel an ongoing NSManagedObjectContext performBlockAndWait process? is there anyway to do that. i want to cancel in case the user gets deleted and do not want to save 1000 entries.

[context performBlockAndWait:^{
        NSError *childError = nil;
        if ([context save:&childError])
        {
            [context.parentContext performBlockAndWait:^{
                NSError *parentError = nil;
                if ([context.parentContext save:&parentError])
                {
                    // something here
                }
                else
                {
                    // error
                }
            }];
        }
        else
        {
            // error
        }
    }];

Upvotes: 0

Views: 266

Answers (1)

Tom Harrington
Tom Harrington

Reputation: 70946

You don't cancel performBlockAndWait, there is no API support for this.

The call runs until the block you provide completes. If you need to exit early for some reason, you would need to check a flag from within the block and stop working if the flag said it was time to exit. You can't force it to stop from the outside; you need to finish up and exit from the inside. If that doesn't work with your current calls to performBlockAndWait, you can either refactor that code so that it's possible or else live with the current situation.

Upvotes: 2

Related Questions