1

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

+(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];
}

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

4

1 回答 1

2

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

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

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

于 2012-06-07T19:34:38.890 回答