Basic data. Keep track of changes and log local notifications.

I am relatively new to Core Data and KVC, but I would like some pointers to register listeners for changes to Core Data objects. Here's the situation:

I have one NSManagedObject, called Patient, and the other - Medication. A Patientcan have many Medications, and a Medicationhas dates startOnand endOn.

I would like to somehow listen to property changes endOnfor all objects Medication. When a change occurs, I would like to schedule a local notification on the iOS device. I used to work with local notifications, but I don’t know where to place the code in this context.

Create scheduling code in the App Delegate and register the application delegate to listen for changes to the objects Medication? Should it be tied to NSManagedObjectContext?

How it's done? Pointers will be very grateful!

Thank!

+5
source share
2 answers

- . , -setEndOn: on Medication; - . , MedicationManager, . , , MedicationManager -createMedicationWithName: startOn: endOn: :

- (Medication*) createMedicationWithName:(NSString*)medName startOn:(NSDate*)startDate endOn:(NSDate*)endDate
    {
    //  Create and configure a new instance of the Compound entity 
    Medication *newMedication = (Medication *)[NSEntityDescription insertNewObjectForEntityForName:@"Medication"
                                                inManagedObjectContext:[self managedObjectContext]];
    [newMedication setName:medName];
    [newMedication setStartOn:startDate];
    [newMedication setEndOn:endDate];

    //  Set up KVO
    [newMedication addObserver:self
                    forKeyPath:@"endOn"
                    options:NSKeyValueObservingOptionNew
                    context:nil];

    return newCompound;
    }


- (void) observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object 
                                                    change:(NSDictionary *)change 
                                                    context:(void *)context
    {
    if ([keyPath isEqualToString:@"endOn"])
        {
        //  ... schedule local notification on the iOS device for (Medication*)object.
        return;
        }
    }

- .

, Observer... , MedicationManager . , , addObserver . , "" ( -awakeFromFetch).

+7

, , . , , ( , iPhone, , ) awakeFromFetch awakeFromInsert.

, , , , . endOn " " " "

- (void)addMyObservers
{
    registeredObservers_ = YES;
    [self addObserver:self forKeyPath:@"endOn" options:NSKeyValueObservingOptionNew context:nil]; 
}

- (void)awakeFromInsert 
{
    // called when you create this object
    [super awakeFromInsert];
    [self addMyObservers];
}

- (void)awakeFromFetch
{
    // called when you fetch this old object from the store
    [super awakeFromFetch];
    [self addMyObservers];
}

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
    if([keyPath isEqualToString:@"endOn"]) 
    {
        id newValue = [change objectForKey:NSKeyValueChangeNewKey];
        // someone changed endOn so do something with this "newValue"
        // check to see if the Patient needs the transient property for the soonest medication updated
        // update any local notification schedule 
    }
}

// this is only required if you want to update the Patient transient property for the soonest endOn or 
- (void)setPatient:(Patient *)patient
{
    [self willChangeValueForKey:@"patient"];
    [self setPrimitivePatient:patient];
    [self didChangeValueForKey:@"patient"];

    // check to see if the Patient needs the transient property for the soonest medication updated
}
+3

All Articles