@achievelimitless是正确的。完成所有问题后保存。无论如何,你的应用程序的行为应该依赖于用户的期望。也许4个问题可以确定,但100个问题呢?如果用户在第二个问题后停止或关闭应用程序会怎么样?这样,他会再次重复提问。
因此,稍作修改就可以将每个问卷会话关联一个标识符。通过标识符(和用户标识,而不是名称),您可以获取特定的行并对其进行修改。
更新1
你的问题是,在每个控制器创建一个新的管理对象。
[NSEntityDescription insertNewObjectForEntityForName:@"Questionnaire" inManagedObjectContext:context];
因此,在最后,您会发现尽可能多的行数与您在应用程序中创建的许多对象一样多。
我最喜欢的解决方法是在受欢迎屏幕后打开托管对象,然后在下一次执行时将其传递给每个视图控制器。
nextViewController.currentQuestionnaire = [self currentQuestionnaire];
如果每个视图控制器会暴露像
@property (nonatomic, strong) NSManagedObject* currentQuestionnaire;
当你到达终点,你会做一个save
的属性。你并不需要访问应用程序委托抢上下文,因为每个管理对象知道的情况下被登记。
[[[self currentQuestionnaire] managedObjectContext] save:&error];
或者(我不会采用这种做法),你应该创建一个管理对象,并把它作为应用程序委托中的参考。通过这种方式,你可以在每个控制器内访问像你这样的对象与
NSManagedObjectContext *context = [appDelegate managedObjectContext];
换句话说
NSManagedObject* currentQuestionnaire = [appDelegate currentQuestionnaire];
// update or save here
更新2
你需要在每个视图控制器添加每个属性。
因此,例如,Q1ViewController
将成为
@interface Q1ViewController : UIViewController
@property (nonatomic, strong) NSManagedObject* currentQuestionnaire;
@end
那么这个控制器中,当你准备去下一个视图控制器
[[self currentQuestionnaire] setValue:@"aValue" forKey:@"q1"];
Q2ViewController* nextController = // alloc init here
nextController.currentQuestionnaire = [self currentQuestionnaire];
一个新的调查问卷的创建有在欢迎控制器中进行管理。在这里,当你准备好去问题1时,你需要做以下的事情。
NSManagedObject* currentQuestionnaire = [NSEntityDescription insertNewObjectForEntityForName:@"Questionnaire" inManagedObjectContext:context];
// set name and date for the currentQuestionnaire object
// also the screen controller could have a reference to the questionnaire,
// so it will have a property like declared above
self.currentQuestionnaire = currentQuestionnaire;
Q1ViewController* nextController = // alloc init here
nextController.currentQuestionnaire = [self currentQuestionnaire];
的
可能重复的[用于打开存储该模型与用于创建存储在一个不兼容](http://stackoverflow.com/questions/8881453/the-model-used-to-open -the-store-is-incompatible-one-one-used-to-the-the) – Kevin
嘿,我编辑了我的答案。检查它 –
谢谢@achievelimitless我确实认为这可能是答案...我会给它一个去看看它是否有效。 – Gethin