I have a question how can I listen to changes in my data model.
I have NSManagedObject email with property progress. So while app is sending email, I every time update property progress.
I want to listen to data model and if changed update my view.
I added:
for (SAPEmail *email in _emails)
{
[self addObserver:email forKeyPath:@"progress" options:NSKeyValueObservingOptionNew context:NULL];
}
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
if ([keyPath isEqualToString:@"progress"])
{
SAPEmail *email = object;
NSLog(@">>>>>>>>>>> progress: %@", email.progress);
}
}
But seems it does not work for me.
I also using MagicalRecord.
I also tried to observe context
[[NSNotificationCenter defaultCenter]
addObserver:self
selector:@selector(handleDataModelChange:)
name:NSManagedObjectContextObjectsDidChangeNotification
object:myManagedObjectContext];
But when I debug my data model already was being update 10 times (because I update progress from 0 - 9), but handleDataModelChange invoked just once after all update where made. But I need to get all 10 update each time when data model updated to update progress view.
One more if I use this
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(managedObjectContextDidSave:) name:NSManagedObjectContextDidSaveNotification object:nil];
and then for example try to refresh data:
- (void)managedObjectContextDidSave:(NSNotification *)notification
{
NSLog(@">>>>>>>>>+++++");
_emails = [SAPCoreDataEmailHelper emailsWithStatus:EmailStatusInProgress];
[_theTableView reloadData];
}
+ (NSArray *)emailsWithStatus:(EmailStatus)status
{
NSPredicate *prediacte = [NSPredicate predicateWithFormat:@"status == %d", status];
NSArray *emails = [SAPEmail MR_findAllWithPredicate:prediacte];
return emails;
}
I can see how works NSLog but then my app is freeze.