CoreData IOS: how to create an object with values ​​without marking it with changes

I don’t know how difficult it is

I have an application that uses CoreData.

When I create a new managed entity, I populate its fields with the default values ​​of sotred in plist.

Then I show a bunch of forms (loading the saved values ​​into the forms and updating the values ​​of the object as it moves).

And at the end, I check if the object has hasChanges and suggests saving the object.

This part is working fine.

However, when I have problems, when the user clicks "Back" in the interface. I want to be able to display a warning if the user has changed something in the object or returned to the user if nothing has changed.

I would have hasChanges for you, however, since I populate the object with default data, CoreData is inferior that the object was modified to start.

So how can I create a new managed entity and initialize it if ti is not considered as changes

- (Company *)createNewDefaultCompany:(NSString *)name{
    Company *company = [Company insertInManagedObjectContext:managedObjectContext];
    [company setName:name];
    [company setDate:[NSDate date]];
    [company setLocalisation:@"en_UK"];

    Input *input = [Input insertInManagedObjectContext:managedObjectContext];
    [input populateWithDefault];
    [company setCompanyInputs:input];

    Formulas *formulas = [Formulas insertInManagedObjectContext: managedObjectContext];
    [formulas populateWithDefault];
    [company setCompanyFormula:formulas];

    [company createCalculus];
    return company;
} 

Creating a managed entity.

+(Company *)insertInManagedObjectContext:(NSManagedObjectContext *)managedObjectContext{
    return (Company *)[NSEntityDescription insertNewObjectForEntityForName:@"Company" inManagedObjectContext:managedObjectContext]; 
}
+3
source share
3 answers

, , , , company, . , , deleteObject , . , .

+3

NSUndoManager . , :

- (Company *)createNewDefaultCompany:(NSString *)name{
    [[managedObjectContext undoManager] disableUndoRegistration];

    Company *company = [Company insertInManagedObjectContext:managedObjectContext];
    [company setName:name];
    [company setDate:[NSDate date]];
    [company setLocalisation:@"en_UK"];

    Input *input = [Input insertInManagedObjectContext:managedObjectContext];
    [input populateWithDefault];
    [company setCompanyInputs:input];

    Formulas *formulas = [Formulas insertInManagedObjectContext: managedObjectContext];
    [formulas populateWithDefault];
    [company setCompanyFormula:formulas];

    [company createCalculus];

    [managedObjectContext processPendingChanges];
    [[managedObjectContext undoManager] enableUndoRegistration];

    return company;
} 

canUndo . , ( -removeAllActions ), , - NSManagedObject.

, , undo , .

0

, NSManagedObject , , setPrimitiveValue:. BTW: NSManagedObject awakeFromInsert.

0