18

I want to save & retrieve the timestamp of when the note was created in Core Data

Please guide me on how I can do this.

Elijah
  • 8,381
  • 2
  • 55
  • 49
user440485
  • 787
  • 2
  • 11
  • 20
  • If you followed the Core Data tutorial it shows you how. – BoltClock Jan 03 '11 at 11:07
  • can you give me some link .....have example of it. – user440485 Jan 03 '11 at 11:18
  • Google is your friend - https://developer.apple.com/library/ios/#documentation/DataManagement/Conceptual/iPhoneCoreData01/Introduction/Introduction.html – Abizern Jan 03 '11 at 11:51
  • That link doesn't work anymore. Try this one: https://developer.apple.com/library/mac/Documentation/Cocoa/Conceptual/CoreData/Articles/cdManagedObjects.html. Look under "Object Life-Cycle—Initialization and Deallocation" – pietrorea May 14 '14 at 13:10

4 Answers4

51

You can have your custom NSManagedObject subclass set an attribute as soon as it's inserted in a context by overriding the -awakeFromInsert method:

@interface Person : NSManagedObject
@property (nonatomic, copy) NSDate *creationDate; // modeled property
@end

@implementation Person
@dynamic creationDate; // modeled property

- (void)awakeFromInsert
{
    [super awakeFromInsert];

    self.creationDate = [NSDate date];
}
@end

Note that creationDate above is a modeled property of Core Data attribute type "date", so its accessor methods are generated automatically. Be sure to set your entity's custom NSManagedObject class name appropriately as well.

Chris Hanson
  • 54,380
  • 8
  • 73
  • 102
  • 2
    I prefer this approach, especially since I made an abstract class with this property in it. That way all my models that use a created/modified value are automatically handled with very little code. – Michael Ozeryansky Sep 21 '12 at 02:46
  • This is a better approach. Plus it's the one Apple recommends. – Diego Barros Nov 01 '13 at 06:53
  • If you chose to create your NSManagedObject subclass with primitive values, then you'll have to do something like self.creationDate = [NSDAte timeIntervalSinceReferenceDate]; – pietrorea May 14 '14 at 13:16
  • This approach is better, but I recommend adding some kind of check (like `if (self.creationDate == nil) { ... }`) to account for cases where `-awakeFromInsert` is called multiple times. Yes, you would think it's only called once, but it's called _once per managed object context_. So if you have nested contexts, you'll get your creation date updated multiple times. – Shinigami Jul 06 '17 at 19:12
11

Make a Core Data entity called Note, give it an attribute called timeStamp Set it to type Date.

When creating a new Note assign it the current time:

[note setTimeStamp:[NSDate date]];

Persist the Note, that is it.

RickiG
  • 11,380
  • 13
  • 81
  • 120
  • 3
    The problem is that you'll have to remember to do this every time you crate a new Note. awakeFromInsert is a better place for it – pietrorea May 14 '14 at 13:02
4

Just to keep this question up-to-date with Swift, in the xcdatamodelid click the entity you wish to create a default date. Under the Data Model Inspector change Codegen from "Class Definition" to "Manual/None":

enter image description here

Then from the menu bar click Editor -> Create NSManagedObject Subclass... and create the two Swift classes: Note+CoreDataClass.swift and Note+CoreDataProperties.swift

Open the Note+CoreDataClass.swift file and add the following code:

import Foundation
import CoreData

@objc(Note)
public class Note: NSManagedObject {
    override public func awakeFromInsert() {
        super.awakeFromInsert()
        self.creationDate = Date() as NSDate
    }
}

Now the creationDate will get a default date of the current date/time.

Elijah
  • 8,381
  • 2
  • 55
  • 49
Rafael
  • 7,002
  • 5
  • 43
  • 52
2

You have a Note entity with a created attribute?

Set the default value to now()

Edit

Be aware that setting now() as the default means it will use the build time, not the current time. If you want to set it properly - you need to use an NSManagedObject subclass.

Elijah
  • 8,381
  • 2
  • 55
  • 49
Abizern
  • 146,289
  • 39
  • 203
  • 257