核心数据 - 与级联删除规则实现OneToMany关系

时间:2014-09-07 13:12:57

标签: ios core-data relationship cascading-deletes

在核心数据关系中需要帮助实现删除规则,我浏览了网上的所有文章但是无法理解实现删除规则,创建关系是否需要处理删除过程(我怀疑),我使用规则创建下面的实体,但是在删除List_Main中的条目时不会删除list_Item中的条目,我想连接中没有完成(不知道该怎么做)

我有两个实体

实体-------- Relation Ship ---- Destination ---- Inverse ---- Properties ----删除规则

List_main - Main_to_Item ---- List_Item ----- Item_to_Main - 可选 - 级联

List_Item - Item_to_Main ----- list_Main ------ Main_to_Item ---可选--- nullify

List_Main.h

@property (nonatomic, retain) NSString * list_Name;
@property (nonatomic, retain) NSDate * list_Reminder_Date;
@property (nonatomic, retain) NSNumber * list_Reminder_Flag;
@property (nonatomic, retain) NSSet *main_to_item;
@end

@interface List_Main (CoreDataGeneratedAccessors)

- (void)addMain_to_itemObject:(List_Item *)value;
- (void)removeMain_to_itemObject:(List_Item *)value;
- (void)addMain_to_item:(NSSet *)values;
- (void)removeMain_to_item:(NSSet *)values;

@end

List_Item.h

@interface List_Item : NSManagedObject

@property (nonatomic, retain) NSString * list_Items;
@property (nonatomic, retain) NSString * list_Name;
@property (nonatomic, retain) List_Main *item_to_main;

现在,在第一个视图中,用户在List_main中输入列表名称 FirstView.m

    NSManagedObjectContext *context = [self managedObjectContext];
    NSManagedObject *newItem = [NSEntityDescription insertNewObjectForEntityForName:@"List_Main" inManagedObjectContext:context];


[newItem setValue:self.listName.text forKey:@"list_Name"];
    NSDate *currentDate = [NSDate date];
    currentDate = [self  dateWithOutTime:currentDate];
    [newItem setValue:currentDate forKey:@"list_Reminder_Date"];
    [newItem setValue:NO forKey:@"list_Reminder_Flag"];
    // Save 
    NSError *error = nil;
    if (![context save:&error]) {
        NSAssert(NO, @"Save should not fail");
        [self showAlert];
    }

之后控件进入secondView进入Item并在List_Item中进行entires

secondView.m

    NSManagedObject *category1 = [NSEntityDescription insertNewObjectForEntityForName:@"List_Item" inManagedObjectContext:self.context];

    [category1 setValue:self.listName forKey:@"list_Name"];
    [category1 setValue:combinedThumbNail forKey:@"list_Items"];

    NSError *error = nil;
    if (![self.context save:&error]) {
        NSAssert(NO, @"Save should not fail");
        [self showAlert];
    }

我有各自实体的删除命令

    NSManagedObjectContext *context = [self.fetchedResultsController managedObjectContext];
    [context deleteObject:[self.fetchedResultsController objectAtIndexPath:indexPath]];

现在我应该如何使用关系实现删除规则,对我来说真的是一个showstopper :(( 如果你能指出任何好的教程也会做

0 个答案:

没有答案