views:

22

answers:

1

I have the following piece of code (that may be invoked multiple times or even at the same time because it is in response to some NSURLConnection didFinishLoading method) that should invoke the willSave method for every object registered in the context.

NSLog(@"Before save.");
NSLog(@"Object count: %d", [[managedObjectContext registeredObjects] count]);
[managedObjectContext save:nil];
NSLog(@"After save.");

The problem is, the willSave (and didSave) methods are called just once in the last save operation. I really don't know what's happening here since I'm not using threads or anything like that.

Every object in the context is a subclass of my own MSRemoteObject class that has the following willSave implementation:

- (void)willSave {
    [super willSave];

    NSLog(@"Will save.");
}

This is an example of the produced log when I run the application:

2010-07-12 12:07:45.638 Manistone[9902:207] Before save.
2010-07-12 12:07:45.638 Manistone[9902:207] Object count: 1
2010-07-12 12:07:45.643 Manistone[9902:207] Will save.
2010-07-12 12:07:45.650 Manistone[9902:207] Did save.
2010-07-12 12:07:45.651 Manistone[9902:207] After save.
2010-07-12 12:07:46.141 Manistone[9902:207] Before save.
2010-07-12 12:07:46.141 Manistone[9902:207] Object count: 11
2010-07-12 12:07:46.142 Manistone[9902:207] After save.
2010-07-12 12:07:50.574 Manistone[9902:207] Before save.
2010-07-12 12:07:50.574 Manistone[9902:207] Object count: 16
2010-07-12 12:07:50.575 Manistone[9902:207] After save.
2010-07-12 12:07:51.136 Manistone[9902:207] Before save.
2010-07-12 12:07:51.136 Manistone[9902:207] Object count: 18
2010-07-12 12:07:51.136 Manistone[9902:207] After save.
2010-07-12 12:07:51.137 Manistone[9902:207] Before save.
2010-07-12 12:07:51.137 Manistone[9902:207] Object count: 18
2010-07-12 12:07:51.138 Manistone[9902:207] After save.
2010-07-12 12:07:51.144 Manistone[9902:207] Before save.
2010-07-12 12:07:51.144 Manistone[9902:207] Object count: 18
2010-07-12 12:07:51.145 Manistone[9902:207] Will save.
2010-07-12 12:07:51.145 Manistone[9902:207] Will save.
2010-07-12 12:07:51.145 Manistone[9902:207] Will save.
2010-07-12 12:07:51.145 Manistone[9902:207] Will save.
2010-07-12 12:07:51.145 Manistone[9902:207] Will save.
2010-07-12 12:07:51.145 Manistone[9902:207] Will save.
2010-07-12 12:07:51.145 Manistone[9902:207] Will save.
2010-07-12 12:07:51.145 Manistone[9902:207] Will save.
2010-07-12 12:07:51.145 Manistone[9902:207] Will save.
2010-07-12 12:07:51.146 Manistone[9902:207] Will save.
2010-07-12 12:07:51.146 Manistone[9902:207] Will save.
2010-07-12 12:07:51.146 Manistone[9902:207] Will save.
2010-07-12 12:07:51.146 Manistone[9902:207] Will save.
2010-07-12 12:07:51.146 Manistone[9902:207] Will save.
2010-07-12 12:07:51.146 Manistone[9902:207] Will save.
2010-07-12 12:07:51.146 Manistone[9902:207] Will save.
2010-07-12 12:07:51.146 Manistone[9902:207] Will save.
2010-07-12 12:07:51.147 Manistone[9902:207] Will save.
2010-07-12 12:07:51.158 Manistone[9902:207] After save.

I really want to trigger the willSave method in every save operation. Is there any way to force this? Or are there some conditions under which the willSave method is not called which I'm not aware of?

A: 

The problem is that willSave an didSave are called only when there is no error occurring in the save operation. Otherwise they're just skipped. I understand this for didSave but for willSave it should not be the case.

Eugenio Depalo