1

既存のエンティティ (渡された) set set all values を使用して新しいエンティティを作成しようとしています。以下は、マネージド オブジェクト サブクラスで使用しているクラス メソッドです。

+(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 に答える