zoukankan      html  css  js  c++  java
  • iOS coredata 级联删除

    应用场景如下,每个用户可以设定多个提醒,当删除一个用户时,应当把相关的提醒都删除,而删除一个提醒时,应当把提醒从用户信息中删除。

    那么 Profile 应该建立一个如下图的relationship

    而 reminder 应该建立如下图的relationship

    当向一个profile添加reminder时,要设定好2个对象的relationship,见如下例子代码:

            
            if(profile != nil){
                
                reminder.profileId = CURRENT_PROFILE.pId;
                reminder.contents = @[@"first contents"];
                reminder.type = [NSNumber numberWithInt:self.type];
                reminder.repeatType = [NSNumber numberWithInt:eRepeatTypeWeekly];
                reminder.profile = profile;
                
                NSMutableSet *reminderSet = [[NSMutableSet alloc] initWithSet:profile.reminder];
                [reminderSet addObject:reminder];
                profile.reminder = reminderSet;

          //也可使用系统自动创建的函数设置reminder!

               // [profile addReminderObject:reminder];

    
            
            
            }

    经过以上的过程,当你从数据库删掉一个profile时,与之相关的reminder都会被自动删除。

    这里需要注意的就是设置一个对象的relationship 的delete rule时,指的是删除本对象时,要对这个relationship属性怎么处理。

  • 相关阅读:
    tp5 生成数据表
    tp5 事务
    时间
    api json
    php 函数学习
    win7 安装Ubuntu18.04 双系统后无法引导win7
    vite笔记
    tp5 excel导出
    tp5 sql查询
    idea 停止运行程序
  • 原文地址:https://www.cnblogs.com/breezemist/p/4548666.html
Copyright © 2011-2022 走看看