不随意保存的神奇记录

时间:2014-10-22 10:15:51

标签: ios objective-c iphone core-data magicalrecord

我在保存记录时遇到问题,我不确定我是否已正确完成。我很感激你的评论

VC1转向VC2

[[NSManagedObjectContext MR_defaultContext] reset];
    self.plan = nil;
    if ([segue.identifier isEqualToString:@"doAboutItToDoSomething"])
    {
        MakeItHappenViewController *vc = segue.destinationViewController;
        if (![self.plan.type isEqualToString:@"do"])
        {
            self.plan = [Plan MR_createEntity];
            self.plan.type = @"do";
            vc.plan = self.plan;
            return;
        }
        else
        {
            vc.plan = self.plan;
            NSLog(@"plan not empty");
        }

}

在VC2中有3个选择VC,它将值传递给self.plan

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{        
    if ([segue.identifier isEqualToString:@"makeItToWhat"])
    {
        CategoryCollectionViewController *cat = segue.destinationViewController;
        cat.plan = self.plan;
    }
    else if([segue.identifier isEqualToString:@"makeitToWho"])
    {
        PersonCollectionViewController *per = segue.destinationViewController;
        per.plan = self.plan;
    }
    else if([segue.identifier isEqualToString:@"makeItToWhen"])
    {
        WhenViewController *when = segue.destinationViewController;
        when.plan = self.plan;
    }
}

在VC2中保存计划

-(IBAction)savePlan:(id)sender
{
    if (self.plan.what != nil  || [self.plan.who.name length] >0 || self.plan.when !=nil)
    {
       // NSLog(@"plan Activity name %@, people name %@ date %@",self.plan.what.name, self.plan.who.name,self.plan.when);
        [MagicalRecord saveWithBlock:^(NSManagedObjectContext *localContext) {

            Plan *p = [Plan MR_createEntityInContext:localContext];
            p.type = self.plan.type;
            p.when = self.plan.when;

            if ([self.plan.what MR_inContext:localContext]!=nil)
            {
                NSLog(@"jasjksajklsakjl p.what %@",[self.plan.what MR_inContext:localContext].name);
                p.what = [self.plan.what MR_inContext:localContext];
            }
            p.who = [self.plan.who MR_inContext:localContext];
            p.completed = @(0);
            p.what.plans =p;
            NSLog(@"p %@",p);

        } completion:^(BOOL success, NSError *error)
        {
            if (success)
            {
                [self.plan MR_deleteEntityInContext:[NSManagedObjectContext MR_defaultContext]];
                [self performSegueWithIdentifier:@"makeItHappenToActivities" sender:self];
            }
            else
                NSLog(@"error %@",error);
        }];
    }
}

这是coredata中的关系 enter image description here

enter image description here

enter image description here

0 个答案:

没有答案