I am pretty new to iOS and I am currently designing the “Create a new event” page for my app.
Some info:
- A user can click on create a new event and then press back to cancel creating this new event. (I am using UINavigationController)
- 1 video can be attached to a particular event.
- A user can create a new event, record a video, then decides not to go ahead with saving it (by pressing back)
- Each event must generate a uuid. This uuid is required to prevent entity collision between different clients creating events and submitting them to the server.
- Each video captured is named uuid.mov for storing into the file system before the event is uploaded to the server.
Coming from a rails background, I am used to doing the following:
- When the user click on “Create new event page”, an event is created but not saved to the db.
- The user then enters attributes to the page and decides if he/she wants to commit
- When commit take places, the event is attributed with the information provided. The event is then saved.
However, I don’t believe Core Data has an API that allows the developer to create an entity without saving it. Currently, I am doing something like this:
- When the user visits the “create new event page”, an empty object is created and stored into the db. The reference to the event instance is passed to the Controller of the create a new event page.
- When the user has inputted all the information and shot a video (the uuid is created at awakeFromInsert and is set during 1), saving is merely setting the entity with the required attribute.
- If the user decides to cancel creating an event, I am checking whether cancel is pressed. if it is, both the event and the video file is deleted.
Code to check if Cancel is pressed.
- (void)viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];
if (![[self.navigationController viewControllers] containsObject:self]) {
This is a bit complicated to be honest. Do you guys have a better suggestion?
It actually works as you described Rails working: new objects aren’t saved to the persistent store until you commit them (via
NSManagedObjectContext‘ssave:method).See the Creating and Deleting Managed Objects section of the Core Data Programming Guide. Excerpting…