iPhone Core数据对多关系和取消对子对象的编辑 [英] iPhone Core Data to-many relationship and canceling edits to child objects

查看:128
本文介绍了iPhone Core数据对多关系和取消对子对象的编辑的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我设置了以下关系(网站实体在此处不重要): http: //i.stack.imgur.com/6pzHn.gif

I have set up the following relationships (the Site entity isn't important here): http://i.stack.imgur.com/6pzHn.gif

这里的主要是我有一个Find实体,可以有很多FindPhotos。在我的应用程序中有一个编辑屏幕,您可以在其中编辑查找的属性,还可以添加/删除其FindPhotos。这里是重要的代码:

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;
        }
    }
}

点击保存按钮,调用saveFind方法,保存上下文。
问题是,即使我点击取消按钮,FindPhotos仍会显示为已删除/添加,即使我不保存上下文。

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.

我想要Find Photo实体更改为仅当我点击保存按钮时查找。任何人都可以推荐我一种方式来处理这种情况?

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:


  1. 创建用于添加新对象的附加上下文,取消关闭上下文,保存将上下文与主上下文合并

  1. 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

这篇关于iPhone Core数据对多关系和取消对子对象的编辑的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆