@achievelimitless is correct. Just save after you finished all the questions. Anyway, the behavior of your app should rely on user expectations. Maybe for 4 questions could be ok, but what about for 100 questions? What if the user stops or close the app after the second question? In this way, he will repeat questions again.
So, a little modification could be to associate to each questionnaire session an identifier. Through the identifier (and the user id, not the name) you could fetch the specific row and so modify it.
Update 1
The problem you have is that in each controller you create a new managed object.
[NSEntityDescription insertNewObjectForEntityForName:@"Questionnaire" inManagedObjectContext:context];
So, at the end you will find as many rows as many object you created during the app.
My preferred way to fix it is to crate the managed object one, for example, after the welcome screen, and pass it to each view controller when you do next.
nextViewController.currentQuestionnaire = [self currentQuestionnaire];
Where each view controller will expose a property like
@property (nonatomic, strong) NSManagedObject* currentQuestionnaire;
When you reach the end, you will do a save
. You don't need to access the application delegate to grab the context since each managed object knows the context is registered in.
[[[self currentQuestionnaire] managedObjectContext] save:&error];
Alternatively (I would not follow this approach) you should create a managed object and put it as a reference in the application delegate. By means of this you can access that object like you with
NSManagedObjectContext *context = [appDelegate managedObjectContext];
In other words within each controller
NSManagedObject* currentQuestionnaire = [appDelegate currentQuestionnaire];
// update or save here
Update 2
You need to add each property in each view controller.
So, for example, Q1ViewController
would become
@interface Q1ViewController : UIViewController
@property (nonatomic, strong) NSManagedObject* currentQuestionnaire;
@end
Then within this controller, when you are ready to go to the next view controller
[[self currentQuestionnaire] setValue:@"aValue" forKey:@"q1"];
Q2ViewController* nextController = // alloc init here
nextController.currentQuestionnaire = [self currentQuestionnaire];
The creation of a new questionnaire has to be managed in the welcome controller. Here when you are ready to go to question 1, you need to do the following.
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];