I have an NSManagedObject with the following interface.
@class Organization, Person; @interface Assistance : NSManagedObject @property (nonatomic, retain) NSDate * date; @property (nonatomic, retain) Organization *organization; // Not optional, 1 to 1 @property (nonatomic, retain) NSSet *persons; // 1 to many @end
A person has a 1-1 relationship with the Organization. Help has a 1-1 relationship with the organization and a 1-can relationship with the Person.
The first time I tried to save the recording, it works fine, but not the second time.
Random scenario: I add an object with the date 2013-07-26 00:00:00 +0000, its corresponding organization and person (s).
I will restart the application. He searches for the current date (7/27) and finds nothing. I'm starting to edit the material in the application, so it creates a new object with the convenient method MagicalRecord [Assistance createEntity]; I immediately set the date (7/27) and the organization. Person objects are added or removed. When another date is selected, I try to save the current object of the Help object, for this I myself implemented the setCurrentDate method:
- (void)setCurrentDate:(NSDate *)currentDate { if (assistanceWasChanged) { NSLog(@"> > > Has changed, need to save with Persons {%i}, org {%@}, date {%@}", [self.assistance.persons count], self.organization.name, self.currentDate);
But this time it will not save. I get Cocoa error 1570 (not all filled fields are filled):
> > > Has changed, need to save with Persons {1}, org {MSF}, date {2013-07-27 00:00:00 +0000} 2013-07-27 20:23:41.017 -[NSManagedObjectContext(MagicalSaves) MR_saveWithOptions:completion:](0x8d61e50) β Saving <NSManagedObjectContext (0x8d61e50): *** DEFAULT ***> on *** MAIN THREAD *** 2013-07-27 20:23:41.018 -[NSManagedObjectContext(MagicalSaves) MR_saveWithOptions:completion:](0x8d61e50) β Save Parents? 0 2013-07-27 20:23:41.018 -[NSManagedObjectContext(MagicalSaves) MR_saveWithOptions:completion:](0x8d61e50) β Save Synchronously? 1 2013-07-27 20:23:41.018 -[NSManagedObjectContext(MagicalRecord) MR_contextWillSave:](0x8d61e50) Context DEFAULT is about to save. Obtaining permanent IDs for new 1 inserted objects 2013-07-27 20:23:41.019 CoreData: sql: BEGIN EXCLUSIVE 2013-07-27 20:23:41.019 CoreData: sql: SELECT Z_MAX FROM Z_PRIMARYKEY WHERE Z_ENT = ? CoreData: sql: UPDATE Z_PRIMARYKEY SET Z_MAX = ? WHERE Z_ENT = ? AND Z_MAX = ? 2013-07-27 20:23:41.020 CoreData: sql: COMMIT 2013-07-27 20:23:41.021 +[MagicalRecord(ErrorHandling) defaultErrorHandler:](0x6b3a0) Error: <Assistance: 0x9cb16c0> (entity: Assistance; id: 0x9e5ac00 <x-coredata:
At no time is the organization equal to zero. In addition, an object with a date of "2013-07-26" was never selected / edited. The new object I'm trying to add has a date of "2013-07-27", as my log statement shows. At first, I thought that CoreData had a pointer to old data, but this is not so, the date with the first input was never received. (When I restart the application, the first object (7/26) still exists. In case it matters, the object of the Help object is a property in my implementation file:
@property (strong, nonatomic) Assistance *assistance;
Am I missing something regarding Core Data? Why is the first object inserted when saving a new object object? and why is organization zero?