当对象从关系中添加/删除时更新属性 [英] Update attribute when an object is added/removed from a relationship

查看:126
本文介绍了当对象从关系中添加/删除时更新属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请考虑这种模式:

@class Patient;

@interface Hospital : NSManagedObject

@property (nonatomic, retain) NSString * name;
@property (nonatomic, retain) NSNumber * patientCount;
@property (nonatomic, retain) NSSet *patients;
@end

@interface Hospital (CoreDataGeneratedAccessors)

- (void)addPatientsObject:(Patient *)value;
- (void)removePatientsObject:(Patient *)value;
- (void)addPatients:(NSSet *)values;
- (void)removePatients:(NSSet *)values;

@end

我要更新 patientCount 每次添加或删除病人。如果这是一个正常的属性,我会简单的覆盖setters / getters,但是由于它们是由Core Data生成的,我不知道该怎么做。

I want to update patientCount every time a patient is added or removed. If this was a normal attribute I would simple override the setters/getters, but since they were generated by Core Data, I don't know how to do it.

以正确的方式?我不想获取病人只是为了计数他们,我不想使用KVO的东西这个简单。

What's to correct way? I don't want to fetch the patients just to count them and I don't want to use KVO for something this simple.

推荐答案

您是否知道您可以直接从hospital.patients.count获取患者计数?

Are you aware you can get patient count directly from hospital.patients.count ? Why bother keeping an attribute for this ?

但是如果你必须,那么在你的managedObjectSubclass中实现类似这些的方法,并更新这些方法中的相关属性。

But if you must, then implement methods similar to these in your managedObjectSubclass and update the relevant attribute within these methods.

    - (void)addTasksObject:(TreeNode *)value {    
        NSSet *changedObjects = [[NSSet alloc] initWithObjects:&value count:1];
        [self willChangeValueForKey:@"tasks" withSetMutation:NSKeyValueUnionSetMutation usingObjects:changedObjects];
        [[self primitiveValueForKey:@"tasks"] addObject:value];
        [self didChangeValueForKey:@"tasks" withSetMutation:NSKeyValueUnionSetMutation usingObjects:changedObjects];
        // Add code to update self.patientsCount here
        self.patientsCount = [NSNumber numberWithInt:[self.patientsCount intValue] + 1];
    }

    - (void)removeTasksObject:(TreeNode *)value {
        NSSet *changedObjects = [[NSSet alloc] initWithObjects:&value count:1];
        [self willChangeValueForKey:@"tasks" withSetMutation:NSKeyValueMinusSetMutation usingObjects:changedObjects];
        [[self primitiveValueForKey:@"tasks"] removeObject:value];
        [self didChangeValueForKey:@"tasks" withSetMutation:NSKeyValueMinusSetMutation usingObjects:changedObjects];
        // Add code to update self.patientsCount here (better check not negative)
        self.patientsCount = [NSNumber numberWithInt:[self.patientsCount intValue] - 1];
    }

    - (void)addTasks:(NSSet *)value {    
        [self willChangeValueForKey:@"tasks" withSetMutation:NSKeyValueUnionSetMutation usingObjects:value];
        [[self primitiveValueForKey:@"tasks"] unionSet:value];
        [self didChangeValueForKey:@"tasks" withSetMutation:NSKeyValueUnionSetMutation usingObjects:value];
        self.patientsCount = [NSNumber numberWithInt:self.patients.count];
    }

    - (void)removeTasks:(NSSet *)value {
        [self willChangeValueForKey:@"tasks" withSetMutation:NSKeyValueMinusSetMutation usingObjects:value];
        [[self primitiveValueForKey:@"tasks"] minusSet:value];
        [self didChangeValueForKey:@"tasks" withSetMutation:NSKeyValueMinusSetMutation usingObjects:value];
        self.patientsCount = [NSNumber numberWithInt:self.patients.count];
    }

这篇关于当对象从关系中添加/删除时更新属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆