我有以下代码(可能多次调用,甚至同时调用,因为它是对某些NSURLConnection didFinishLoading方法的响应),它应该为上下文中注册的每个对象调用willSave方法。
NSLog(@"Before save.");
NSLog(@"Object count: %d", [[managedObjectContext registeredObjects] count]);
[managedObjectContext save:nil];
NSLog(@"After save.");
问题是,willSave(和didSave)方法在最后一次保存操作中只调用一次。我真的不知道这里发生了什么,因为我没有使用线程或类似的东西。
上下文中的每个对象都是我自己的MSRemoteObject类的子类,它具有以下willSave实现:
- (void)willSave {
[super willSave];
NSLog(@"Will save.");
}
这是我运行应用程序时生成的日志的示例:
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.
我真的想在每次保存操作中触发willSave方法。有没有办法强迫这个?或者是否存在一些条件,在这些条件下我没有调用willSave方法?
答案 0 :(得分:0)
问题是willSave只有在保存操作中没有发生错误时才会调用didSave。否则他们只是被跳过了。我理解这对于didSave,但对于willSave它不应该是这种情况。