leke
leke

Reputation: 155

How not to save a managedobjectcontext

I'm trying to create a new core data object, and fill it with some information of the subviews on a view. (just to fill an array with template_control objects)

What's the problem? The object can't be saved, but later on when I do a real save of the managedobjectcontect, the object is saved.

What did I try? I created a new managedobjectcontext which I don't save, but the control is still saved when I do a savemanagedobjectcontext on the normal managedobjectcontext.

Anyone who has a solution for this?

Thanks in advance.

   Template_Control * temp_control = [NSEntityDescription  
      insertNewObjectForEntityForName:@"Template_Control"
                                                                          inManagedObjectContext:self.tmpManagedObjectContext];

for(UIView *view in self.view.subviews){
    if(![view isEqual:self.templateEditView.leftSideView]){
        NSLog(@"DE TAG VAN DE view is %i",view.tag);

        temp_control.height = [NSNumber numberWithFloat:view.bounds.size.height];
        temp_control.width = [NSNumber numberWithFloat:view.bounds.size.width];

        [temp_control setX:[NSNumber numberWithFloat:view.frame.origin.x]];
        [temp_control setY:[NSNumber numberWithFloat:view.frame.origin.y]];
        NSLog(@"%i",view.tag);
        [temp_control setControl:[self getControlForTemplateControl:view.tag]];

        [self.controlsOnTemplate addObject:temp_control];
    }
}

NSLog(@"aantal controls: op de template %i", [self.controlsOnTemplate count]);
temp_control = nil;

This is image of my table

enter image description here

To make it a little clearer, here's the code where I save the normal managedobjectcontext:

-(void)saveNewTemplateControls{

NSError *error;

//template voor de contorl ophalen
NSFetchRequest *request = [[NSFetchRequest alloc]init];
[request setEntity:[NSEntityDescription entityForName:@"Template" inManagedObjectContext:self.managedObjectContext]];

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"name == %@",self.template.name];
[request setPredicate:predicate];

NSArray *results = [self.managedObjectContext executeFetchRequest:request error:&error];


NSEntityDescription *entitydescTemplateControl = [NSEntityDescription entityForName:@"Template_Control"
                                                     inManagedObjectContext:self.managedObjectContext];

for(Template_Control *template_control in self.theNewControls){
    NSManagedObject *newtemplatecontrol = [[NSManagedObject alloc]initWithEntity:entitydescTemplateControl
                                                  insertIntoManagedObjectContext:self.managedObjectContext];


    [newtemplatecontrol setValue:template_control.width forKey:@"width"];
    [newtemplatecontrol setValue:template_control.height forKey:@"height"];

    [newtemplatecontrol setValue:template_control.x forKey:@"x"];
    [newtemplatecontrol setValue:template_control.y forKey:@"y"];

    [newtemplatecontrol setValue:[results objectAtIndex:0] forKey:@"template"];

    [newtemplatecontrol setValue:template_control.control forKey:@"control"];

    [self.managedObjectContext save:&error];
}

}

Upvotes: 1

Views: 295

Answers (1)

Levi
Levi

Reputation: 7343

Your objects will not be written (or "saved") in the sqlite db that backs Core Data, until you save your Managed Object Context. No pint in checking out the db until you call save on it.

Upvotes: 2

Related Questions