I have set up the following relationships (the Site entity isn't important here): http://i.stack.imgur.com/6pzHn.gif
The main thing here is that I have a Find entity which can have many FindPhotos. In my app there is an editing screen, where you can edit a Find's attributes and also add/remove its FindPhotos. Here's the important code:
/* tapping the Save button */
- (IBAction)saveFind:(id)sender {
[[self find] setFindName:findName];
[[self find] setNotes:[self notes]];
/* etc... */
NSError *error;
[[self context] save:&error];
if (error) {
NSLog(@"Error while saving find: %@", error);
}
[[self presentingViewController] dismissModalViewControllerAnimated:YES];
}
- (void) imagePickerController: (UIImagePickerController *) picker didFinishPickingMediaWithInfo: (NSDictionary *) info {
NSString *mediaType = [info objectForKey: UIImagePickerControllerMediaType];
UIImage *originalImage, *editedImage, *imageToSave;
// Handle a still image capture
if (CFStringCompare ((__bridge CFStringRef) mediaType, kUTTypeImage, 0) == kCFCompareEqualTo) {
editedImage = (UIImage *) [info objectForKey:UIImagePickerControllerEditedImage];
originalImage = (UIImage *) [info objectForKey:UIImagePickerControllerOriginalImage];
if (editedImage) {
imageToSave = editedImage;
} else {
imageToSave = originalImage;
}
NSString *documentsDirectory = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
NSString *pathName = [NSString stringWithFormat:@"/Photo%i.jpg", [NSString UUIDString]];
NSString *jpgPath = [documentsDirectory stringByAppendingPathComponent:pathName];
[UIImageJPEGRepresentation(imageToSave, 1.0) writeToFile:jpgPath atomically:YES];
NSData * thumbnail = UIImageJPEGRepresentation([originalImage thumbnailImage:100 transparentBorder:0 cornerRadius:0 interpolationQuality:kCGInterpolationMedium], 1.0);
FindPhoto *photo = [NSEntityDescription insertNewObjectForEntityForName:@"FindPhoto" inManagedObjectContext:[self context]];
[photo setThumbnail:thumbnail];
[photo setPath:jpgPath];
[photo setFind:[self find]];
[[self find] addPhotosObject:photo];
}
[picker dismissModalViewControllerAnimated: YES];
}
/* tapping the Cancel button */
- (IBAction)cancel:(id)sender {
[[self presentingViewController] dismissModalViewControllerAnimated:YES];
}
/* deleting a photo */
- (void)photoWasDeleted:(MWPhoto *)mwphoto {
for (FindPhoto *photo in [[self find] photos]) {
if ([[photo path] isEqualToString:[mwphoto photoPath]]) {
[[self find] removePhotosObject:photo];
NSLog(@"Photo deleted");
return;
}
}
}
By tapping the Save button, the saveFind method is called, which saves the context. The problem is that even if I tap the Cancel button, the FindPhotos will still show as deleted/added, even if I don't save the context.
I want the FindPhoto entities to be changed for the Find only when I tap the Save button. Can anyone recommend me a way to handle this kind of situation?
There are several ways to solve this problem:
create an additional context for adding the new objects, on cancel dismiss the context, on save merge the context with the main context
Use an undo manager, on cancel revert any changes. Use undo grouping to group all changes