Sanoj Kashyap
Sanoj Kashyap

Reputation: 5060

child context not saving in core data

I am using child context to save core data object but it's not saving at all. What could be the issue with the code? I tried by removing perform block and use performblockAndwait as well but not working. Like prepareDataForCustomYearlyOption method I have others three more methods for daily, weekly and monthly as same code as below almost.

Another place same code is working.

- (NSManagedObjectContext *)backgroundManagedObjectContext
{
    if (_managedObjectContext == nil) {
         [[OUCSCoreDataManager privateInstance]managedObjectContext];
    }
    NSManagedObjectContext *temporaryContext = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSPrivateQueueConcurrencyType];
    temporaryContext.parentContext = _managedObjectContext;
    return temporaryContext;
}
- (void)prepareDataForCustomYearlyOption {

    if ([CoreDataManager countForEntity:kCALENDAR_CUSTOM_YEARLY_REPEAT_OPTIONS] != 0) {
        return;
    }

    NSManagedObjectContext *temporaryContext = [[CoreDataManager privateInstance]backgroundManagedObjectContext];

    [temporaryContext performBlock:^{
        @try {
            RepeatOptionsCustomYearly *yearlyOption = [RepeatOptionsCustomYearly insertInManagedObjectContext:temporaryContext];
            yearlyOption.title = NSLocalizedString(k_Yearly, @"REPEAT_OPTIONS_TITILE3");
            yearlyOption.isSelected = [NSNumber numberWithBool:NO];
            yearlyOption.every = [NSNumber numberWithInt:1];
            yearlyOption.startOn = [NSDate date];
            yearlyOption.endsOn = [NSDate date];
            yearlyOption.neverEnds = [NSNumber numberWithBool:NO];
            yearlyOption.summary = @"";
            yearlyOption.everyRange = [NSKeyedArchiver archivedDataWithRootObject:[self getYearRange]];

        } @catch (NSException *exception) {
            NSLog(@"Calendar Manager Exception: --> %@ %@",exception.name, exception.reason);
        }
        // push to parent
        [temporaryContext performBlock:^{
            // push to parent
            NSError *errorTemp;
            if (![temporaryContext save:&errorTemp])
            {
                // handle error
                NSLog(@"Temp MOC Save Error: %@",errorTemp.description);
            }
        }];

    }];
}

Upvotes: 0

Views: 1391

Answers (2)

Bjørn Ruthberg
Bjørn Ruthberg

Reputation: 344

You could try something like this, but the error handling is NOT an example to follow:

Make sure you get your private queue set up correctly with the main context.

func getPrivateQueueMOC() -> NSManagedObjectContext?
{
    if let moc = delegate.managedObjectContext
    {
        let privateMOC = NSManagedObjectContext(concurrencyType: NSManagedObjectContextConcurrencyType.privateQueueConcurrencyType)
        privateMOC.parent = moc
        return privateMOC
    }

    return nil
}

This makes sure that you save your changes and updates the main context as soon as possible

func savePrivateContext(privateContext:NSManagedObjectContext)
{
    do {
        try privateContext.save()

        guard let moc = delegate.managedObjectContext else { return }
        moc.performAndWait {
            do {
                try moc.save()
            } catch {
                fatalError("Failure to save context: \(error)")
            }
        }
    } catch {
        fatalError("Failure to save context: \(error)")
    }
}

The correct way to use the private queue

func persistStuff(_ stuff:[Dictionary<String, AnyObject>])
{
    if stuff.isEmpty
    {
        return
    }

    if let context = getPrivateQueueMOC()
    {
        context.perform({ () -> Void in
            // Do your stuff here

            if context.hasChanges
            {
                self.savePrivateContext(privateContext: context)
            }

            DispatchQueue.main.async(execute: { () -> Void in
                // Tell UI to update
            })
        })
    }
}

Upvotes: 1

yageek
yageek

Reputation: 4455

I suppose your code contains two issues.

The first one is inside the backgroundManagedObjectContext method. The ivar _managedObjectContext is never set:

- (NSManagedObjectContext *)backgroundManagedObjectContext
{
    if (_managedObjectContext == nil) {
         _managedObjectContext = [[OUCSCoreDataManager privateInstance]managedObjectContext];
    }
    NSManagedObjectContext *temporaryContext = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSPrivateQueueConcurrencyType];
    temporaryContext.parentContext = _managedObjectContext;
    return temporaryContext;
}

The second one could be that you call twice performBlock:

- (void)prepareDataForCustomYearlyOption {

    if ([CoreDataManager countForEntity:kCALENDAR_CUSTOM_YEARLY_REPEAT_OPTIONS] != 0) {
        return;
    }

    NSManagedObjectContext *temporaryContext = [[CoreDataManager privateInstance]backgroundManagedObjectContext];

    [temporaryContext performBlock:^{
        @try {
            RepeatOptionsCustomYearly *yearlyOption = [RepeatOptionsCustomYearly insertInManagedObjectContext:temporaryContext];
            yearlyOption.title = NSLocalizedString(k_Yearly, @"REPEAT_OPTIONS_TITILE3");
            yearlyOption.isSelected = [NSNumber numberWithBool:NO];
            yearlyOption.every = [NSNumber numberWithInt:1];
            yearlyOption.startOn = [NSDate date];
            yearlyOption.endsOn = [NSDate date];
            yearlyOption.neverEnds = [NSNumber numberWithBool:NO];
            yearlyOption.summary = @"";
            yearlyOption.everyRange = [NSKeyedArchiver archivedDataWithRootObject:[self getYearRange]];

        } @catch (NSException *exception) {
            NSLog(@"Calendar Manager Exception: --> %@ %@",exception.name, exception.reason);
        }
        // push to parent
        // Comment this ->[temporaryContext performBlock:^{
            // push to parent
            NSError *errorTemp;
            if (![temporaryContext save:&errorTemp])
            {
                // handle error
                NSLog(@"Temp MOC Save Error: %@",errorTemp.description);
            }
        // Comment this -> }];

    }];
}

Do not forget to save it in the parent context of temporaryContext if needed.

Upvotes: 0

Related Questions