0

I am developing a application that uses core data. In my application i would like every user to have the same database. So i want all of the devices to share the same core data objects.

In order to do that 'synchronization' I found a presentation on this answer that discussed this matter. In the presentation, it is suggested that I add attributes such as 'creationDate' and 'modificationDate' to every object in Core Data.

So to do that, i tried to subclass NSManagedObject (MyManagedObject) in order to add those properties to every object in core data. I also made every NSManagedObject subclass I had (generated automatically by the model) subclass MyManagedObject.

The problem is that the properties I added to the NSManagedObject subclass do not persist in the database. So when I close and reopen the application, 'creationDate' becomes (null).

Here's some of my code (header):

@interface MyManagedObject : NSManagedObject
@property (nonatomic, retain) NSDate * modificationDate;
@property (nonatomic, retain) NSDate * creationDate;
@end

And here's the .m file:

@implementation MyManagedObject
@synthesize modificationDate, creationDate;
-(void)willSave {
    [super willSave];    
    self.modificationDate = [NSDate date];
}
-(void)awakeFromInsert {
    [super awakeFromInsert];
    [self setCreationDate:[NSDate date]];
    [self setModificationDate:[NSDate date]];
}

Any help, directions or reading would be greatly appreciated. I have been struggling with this for a while now. I need a better way than adding these attributes to EVERY single entity in the model. Thanks in advance

PS: i intend to have a 'truth database' (a .sqlite file) in a server. The synchronization will be between the server and the iPhone. Such as suggested in the presentation

Community
  • 1
  • 1
luksfarris
  • 1,313
  • 19
  • 38

1 Answers1

1

If you create MyManagedObject in the data model design tool and also register it as the parent of other entities inside that tool, this should almost work. (I suspect you didn't do that because of the @synthesize statement...those would usually be @dynamic.)

The one other problem to fix is that you have to test whether you've already changed an object's property (self.modificationDate in your case) before changing it again or else willSave continues to get called. A BOOL value that gets set after the first change and cleared in didSave is a simple thing to test.

Phillip Mills
  • 30,888
  • 4
  • 42
  • 57