Mac Developer Library

Developer

Core Data Framework Reference NSPersistentStore Class Reference

Options
Deployment Target:

On This Page
Language:

NSPersistentStore

This class is the abstract base class for all Core Data persistent stores.

Core Data provides four store types—SQLite, Binary, XML, and In-Memory (the XML store is not available on iOS); these are described in Persistent Store Features. Core Data also provides subclasses of NSPersistentStore that you can use to define your own store types: NSAtomicStore and NSIncrementalStore. The Binary and XML stores are examples of atomic stores that inherit functionality from NSAtomicStore.

Subclassing Notes

You should not subclass NSPersistentStore directly. Core Data only supports subclassing of NSAtomicStore and NSIncrementalStore.

The designated initializer is initWithPersistentStoreCoordinator:configurationName:URL:options:. When you implement the initializer, you must ensure you load metadata during initialization and set it using metadata.

You must override these methods:

  • type type Property

    The type string of the receiver. (read-only)

    Declaration

    Swift

    var type: String { get }

    Objective-C

    @property(readonly, copy) NSString *type

    Discussion

    This string is used when specifying the type of store to add to a persistent store coordinator.

    Special Considerations

    Subclasses must override this method to provide a unique type.

    Availability

    Available in OS X v10.5 and later.

  • The persistent store coordinator that loaded the receiver. (read-only)

    Declaration

    Swift

    weak var persistentStoreCoordinator: NSPersistentStoreCoordinator? { get }

    Objective-C

    @property(nonatomic, readonly, weak) NSPersistentStoreCoordinator *persistentStoreCoordinator

    Availability

    Available in OS X v10.5 and later.

  • The name of the managed object model configuration used to create the receiver. (read-only)

    Declaration

    Swift

    var configurationName: String { get }

    Objective-C

    @property(readonly, copy) NSString *configurationName

    Availability

    Available in OS X v10.5 and later.

  • The options the store was initialized with. (read-only)

    Declaration

    Swift

    var options: [NSObject : AnyObject]? { get }

    Objective-C

    @property(readonly, strong) NSDictionary *options

    Discussion

    See NSPersistentStoreCoordinator for a list of key names for options in this dictionary.

    Availability

    Available in OS X v10.5 and later.

  • URL URL Property

    The URL for the receiver.

    Declaration

    Swift

    var URL: NSURL?

    Objective-C

    @property(strong) NSURL *URL

    Discussion

    To alter the location of a store, send the persistent store coordinator a setURL:forPersistentStore: message.

    Availability

    Available in OS X v10.5 and later.

  • The unique identifier for the receiver.

    Declaration

    Swift

    var identifier: String!

    Objective-C

    @property(copy) NSString *identifier

    Discussion

    The identifier is used as part of the managed object IDs for each object in the store.

    Special Considerations

    NSPersistentStore provides a default implementation to provide a globally unique identifier for the store instance.

    Availability

    Available in OS X v10.5 and later.

    See Also

    metadata

  • A Boolean value that indicates whether the receiver is read-only.

    Declaration

    Swift

    var readOnly: Bool

    Objective-C

    @property(getter=isReadOnly) BOOL readOnly

    Discussion

    YEStrue if the receiver is read-only, otherwise NOfalse.

    Availability

    Available in OS X v10.5 and later.

  • Returns the metadata from the persistent store at the given URL.

    Declaration

    Swift

    class func metadataForPersistentStoreWithURL(_ url: NSURL) throws -> [String : AnyObject]

    Objective-C

    + (NSDictionary<NSString *,id> *)metadataForPersistentStoreWithURL:(NSURL *)url error:(NSError * _Nullable *)error

    Parameters

    url

    The location of the store.

    error

    If an error occurs, upon return contains an NSError object that describes the problem.

    Return Value

    The metadata from the persistent store at url. Returns nil if there is an error.

    Discussion

    Special Considerations

    Subclasses must override this method.

    Availability

    Available in OS X v10.5 and later.

  • Sets the metadata for the store at a given URL.

    Declaration

    Swift

    class func setMetadata(_ metadata: [String : AnyObject]?, forPersistentStoreWithURL url: NSURL) throws

    Objective-C

    + (BOOL)setMetadata:(NSDictionary<NSString *,id> *)metadata forPersistentStoreWithURL:(NSURL *)url error:(NSError * _Nullable *)error

    Parameters

    metadata

    The metadata for the store at url.

    url

    The location of the store.

    error

    If an error occurs, upon return contains an NSError object that describes the problem.

    Return Value

    YEStrue if the metadata was written correctly, otherwise NOfalse.

    Discussion

    Special Considerations

    Subclasses must override this method to set metadata appropriately.

    Availability

    Available in OS X v10.5 and later.

  • The metadata for the receiver.

    Declaration

    Swift

    var metadata: [String : AnyObject]!

    Objective-C

    @property(nonatomic, strong) NSDictionary <NSString *,id> *metadata

    Discussion

    The dictionary must include the store type (NSStoreTypeKey) and UUID (NSStoreUUIDKey).

    Special Considerations

    Subclasses must override this property to provide storage and persistence for the store metadata.

    Availability

    Available in OS X v10.5 and later.

  • Instructs the receiver to load its metadata.

    Declaration

    Swift

    func loadMetadata() throws

    Objective-C

    - (BOOL)loadMetadata:(NSError * _Nullable *)error

    Parameters

    error

    If an error occurs, upon return contains an NSError object that describes the problem.

    Return Value

    YEStrue if the metadata was loaded correctly, otherwise NOfalse.

    Discussion

    Special Considerations

    There is no way to return an error if the store is invalid.

    Availability

    Available in OS X v10.6 and later.

  • Returns the NSMigrationManager class for this store class.

    Declaration

    Swift

    class func migrationManagerClass() -> AnyClass

    Objective-C

    + (Class)migrationManagerClass

    Return Value

    The NSMigrationManager class for this store class

    Discussion

    In a subclass of NSPersistentStore, you can override this to provide a custom migration manager subclass (for example, to take advantage of store-specific functionality to improve migration performance).

    Availability

    Available in OS X v10.6 and later.