iphone - 使用 MagicalRecord 使用当前实体创建新实体

标签 iphone objective-c ios core-data magicalrecord

我正在尝试使用现有实体(传入)集创建一个新实体并设置所有值。这是我在托管对象子类中使用的类方法:

+(FlightManifest *)getNextFlightManifestLegWithFlightManifest:(FlightManifest *)fm {
    // Get the current context
    NSManagedObjectContext *moc = [NSManagedObjectContext MR_contextForCurrentThread];

    // Set a var to the cur leg so we can use it to increment the leg number later
    NSInteger curLeg = [fm.leg intValue];

    // Check to see if we already have the next leg saved
    if ([self getFlightManifestWithTripID:fm.tripid andLeg:[NSNumber numberWithInt:curLeg + 1]] !=nil) {
        return [self getFlightManifestWithTripID:fm.tripid andLeg:[NSNumber numberWithInt:curLeg + 1]];
    } else {
        // Create a new leg using the passed in FlightManifest for the values
        FlightManifest *newFM = [FlightManifest MR_createInContext:moc];  

        // Set the value of the newly created object to the one passed in
        newFM = fm;

        // Increment the leg number
        newFM.leg = [NSNumber numberWithInt:curLeg + 1];

        // Save the object
        [moc MR_save];

        return newFM;
    }
}

我这样调用它:

- (IBAction)nextLegButtonPressed:(id)sender {

    currentFlight = [FlightManifest getNextFlightManifestLegWithFlightManifest:currentFlight];
    self.legNumberLabel.text = [currentFlight.leg stringValue];
    [self reloadLegsTableViewData];
}

发生的事情是我正在更改当前实体而不是创建新实体。关于我做错了什么的任何想法?

最佳答案

这看起来很明显,但这条线似乎很可疑:

// Set the value of the newly created object to the one passed in
    newFM = fm;

这将使代码在使用现有的 fm 对象之后...并更改您尝试复制的对象...

关于iphone - 使用 MagicalRecord 使用当前实体创建新实体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10937901/

相关文章:

ios - 在 AVPlayerViewController, Swift 中将 AVPlayer 设为全屏 View

iphone - 配置 UIImagePickerController 的帧大小

iphone - iOS:以编程方式访问家长控制操作?

iphone - 如何以编程方式添加 UITabBarController(无 xib 文件或 Storyboard)

ios - Xcode 7 动画中的 UITesting 重复动画使测试失败

ios7自动布局按钮调整大小问题

iphone - iOS模拟器认为文档目录是/var/root/Documents

iphone - 如何检查 UITextField 的文本是否是有效的电子邮件?

iphone - iOS应用程序中已下载文件的磁盘空间

ios - 容器 View Controller 如何在 sizeForChildContentContainer 中查找和提供 subview Controller 的大小