Iphone 使用MagicalRecord使用当前实体创建新实体

Iphone 使用MagicalRecord使用当前实体创建新实体,iphone,objective-c,ios,core-data,magicalrecord,Iphone,Objective C,Ios,Core Data,Magicalrecord,我正在尝试使用现有实体(传入)设置所有值来创建新实体。下面是我在托管对象子类中使用的类方法: +(FlightManifest *)getNextFlightManifestLegWithFlightManifest:(FlightManifest *)fm { // Get the current context NSManagedObjectContext *moc = [NSManagedObjectContext MR_contextForCurrentThread];

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

+(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对象后生成代码…并更改您尝试复制的对象…

您需要共享MR_CreateIncontext的代码,这是MagicalRecord APIYes的一部分,我正在尝试从传入的对象复制所有属性,然后更改其中一个属性(腿部)。由于ManagedObjects没有复制方法,我不确定除了如下设置每个属性之外还能做什么:newFM.tripid=fm.tripid。。。顺便说一句,这是可行的,但似乎不是最有效的方法。这正是你需要做的:(如果执行从一个托管对象到另一个托管对象的深度复制,实际上就是要复制整个对象图。在这种情况下,手动复制属性似乎是最好的方法。好的,如果您在回答中添加最后一条注释,我将接受。谢谢。