Я установил следующие отношения (сущность сайта здесь не важна): http://i.stack.imgur.com/6pzHn.gif
Главное здесь - у меня есть объект Find, который может иметь много FindPhotos. В моем приложении есть экран редактирования, где вы можете редактировать атрибуты Find, а также добавлять / удалять его FindPhotos. Вот важный код:
/* 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;
}
}
}
При нажатии кнопки «Сохранить» вызывается метод saveFind, который сохраняет контекст.
Проблема в том, что даже если я нажму кнопку «Отмена», FindPhotos все равно будет отображаться как удаленный / добавленный, даже если я не сохраню контекст.
Я хочу, чтобы объекты FindPhoto изменялись для поиска только при нажатии кнопки Сохранить. Кто-нибудь может порекомендовать мне способ справиться с такой ситуацией?