iOS Developer Library

Developer

UIKit Framework Reference UIManagedDocument Class Reference

Options
Deployment Target:

On This Page
Language:

UIManagedDocument

UIManagedDocument is a concrete subclass of UIDocument that integrates with Core Data. When you initialize a managed document, you specify the URL for the document location. The document object then creates a Core Data stack to use to access the document’s persistent store using a managed object model from the application’s main bundle. See Supporting Document-Based Apps in iCloud in iCloud Programming Guide for Core Data for implementation strategies and troubleshooting steps. More...

Inheritance


Conforms To


Import Statement


import UIKit @import UIKit;

Availability


Available in iOS 5.0 and later.
  • Creates or loads the document’s persistent store.

    Declaration

    Swift

    func configurePersistentStoreCoordinatorForURL(_ storeURL: NSURL!, ofType fileType: String!, modelConfiguration configuration: String?, storeOptions storeOptions: [NSObject : AnyObject]?, error error: NSErrorPointer) -> Bool

    Objective-C

    - (BOOL)configurePersistentStoreCoordinatorForURL:(NSURL *)storeURL ofType:(NSString *)fileType modelConfiguration:(NSString *)configuration storeOptions:(NSDictionary *)storeOptions error:(NSError **)error

    Parameters

    storeURL

    The URL for the persistent store.

    fileType

    The document’s file type.

    configuration

    The managed object model configuration to use.

    storeOptions

    The options used to configure the persistent store coordinator.

    error

    Upon return, if a problem occurs, contains an error object that describes the problem.

    Return Value

    YEStrue if configuration is successful, otherwise NOfalse.

    Discussion

    You can override this method if you want customize the creation or loading of the document’s persistent store. For example, you can perform post-migration clean-up—if your application needs to migrate store data to use a new version of the managed object model, you can override this method to make additional modifications to the store after migration.

    Import Statement

    import UIKit

    Availability

    Available in iOS 5.0 and later.

  • The document’s managed object context. (read-only)

    Declaration

    Swift

    var managedObjectContext: NSManagedObjectContext! { get }

    Objective-C

    @property(nonatomic, retain, readonly) NSManagedObjectContext *managedObjectContext

    Discussion

    The document automatically creates a managed object context using its persistent store coordinator.

    Special Considerations

    You must not use the document’s managed object context in writeAdditionalContent:toURL:originalContentsURL:error:, or any of the asynchronous UIDocument methods.

    Import Statement

    import UIKit

    Availability

    Available in iOS 5.0 and later.

  • The document’s managed object model. (read-only)

    Declaration

    Swift

    var managedObjectModel: NSManagedObjectModel! { get }

    Objective-C

    @property(nonatomic, retain, readonly) NSManagedObjectModel *managedObjectModel

    Discussion

    Persistent documents always have a managed object model. The default model is the union of all models in the main bundle. You can specify a configuration to use with modelConfiguration. You can subclass UIManagedDocument to override this method if you need custom behavior.

    Import Statement

    import UIKit

    Availability

    Available in iOS 5.0 and later.

  • Options used when creating the document’s persistent store.

    Declaration

    Swift

    var persistentStoreOptions: [NSObject : AnyObject]?

    Objective-C

    @property(nonatomic, copy) NSDictionary *persistentStoreOptions

    Discussion

    By default, this value is nil.

    To support automatic migration, you might pass a dictionary like that shown in the following example.

    • NSDictionary *options = @{
    • NSMigratePersistentStoresAutomaticallyOption: @YES,
    • NSInferMappingModelAutomaticallyOption: @YES
    • };
    • <#Managed document instance#>.persistentStoreOptions = options;

    Import Statement

    import UIKit

    Availability

    Available in iOS 5.0 and later.

  • A model configuration name to be passed when configuring the persistent store.

    Declaration

    Swift

    var modelConfiguration: String?

    Objective-C

    @property(nonatomic, copy) NSString *modelConfiguration

    Discussion

    By default, this value is nil.

    Import Statement

    import UIKit

    Availability

    Available in iOS 5.0 and later.

  • Returns the Core Data store type for a given document file type.

    Declaration

    Swift

    func persistentStoreTypeForFileType(_ fileType: String!) -> String!

    Objective-C

    - (NSString *)persistentStoreTypeForFileType:(NSString *)fileType

    Parameters

    fileType

    The document file type.

    Return Value

    The persistent store type for fileType.

    Discussion

    Override this method to specify a persistent store type for a given document type.

    The default returns NSSQLiteStoreType.

    Import Statement

    import UIKit

    Availability

    Available in iOS 5.0 and later.

  • Handles reading non-Core Data content in the additional content directory in the document’s file package.

    Declaration

    Swift

    func readAdditionalContentFromURL(_ absoluteURL: NSURL, error error: NSErrorPointer) -> Bool

    Objective-C

    - (BOOL)readAdditionalContentFromURL:(NSURL *)absoluteURL error:(NSError **)error

    Parameters

    absoluteURL

    The URL for the additional content directory in the document’s file package.

    error

    Upon return, if a problem occurs, contains an error object that describes the problem.

    Return Value

    YEStrue if the read operation is successful, otherwise NOfalse.

    Discussion

    You override this method to read non-Core Data content from the additional content directory in the document’s file package.

    If you implement this method, it is invoked automatically by readFromURL:error:.

    There is no need to invoke super’s implementation.

    Special Considerations

    Additional content is not supported on iCloud.

    Import Statement

    import UIKit

    Availability

    Available in iOS 5.0 and later.

  • Handles writing non-Core Data content to the additional content directory in the document’s file package.

    Declaration

    Swift

    func additionalContentForURL(_ absoluteURL: NSURL, error error: NSErrorPointer) -> AnyObject?

    Objective-C

    - (id)additionalContentForURL:(NSURL *)absoluteURL error:(NSError **)error

    Parameters

    absoluteURL

    The URL for the additional content directory in the document’s file package.

    error

    Upon return, if a problem occurs, contains an error object that describes the problem.

    Return Value

    An object that contains the additional content for the document at absoluteURL, or nil if there is a problem.

    Discussion

    You override this method to perform to manage non-Core Data content to be stored in the additional content directory in the document’s file package.

    If you implement this method, it is invoked automatically by contentsForType:error:. The returned object is passed to writeAdditionalContent:toURL:originalContentsURL:error:.

    There is no need to invoke super’s implementation.

    Special Considerations

    A return value of nil indicates an error condition. To avoid generating an exception, you must return a value from this method. If it is not always the case that there will be additional content, you should return a sentinel value (for example, an NSNull instance) that you check for in writeAdditionalContent:toURL:originalContentsURL:error:.

    The object returned from this method is passed to writeAdditionalContent:toURL:originalContentsURL:error:. Because writeAdditionalContent:toURL:originalContentsURL:error: is executed on a different thread, you must ensure that the object you return is thread-safe. For example, you might return an NSData object containing an archive of the state you want to capture.

    Additional content is not supported on iCloud.

    Import Statement

    import UIKit

    Availability

    Available in iOS 5.0 and later.

  • Handles writing non-Core Data content to the document’s file package.

    Declaration

    Swift

    func writeAdditionalContent(_ content: AnyObject!, toURL absoluteURL: NSURL!, originalContentsURL absoluteOriginalContentsURL: NSURL!, error error: NSErrorPointer) -> Bool

    Objective-C

    - (BOOL)writeAdditionalContent:(id)content toURL:(NSURL *)absoluteURL originalContentsURL:(NSURL *)absoluteOriginalContentsURL error:(NSError **)error

    Parameters

    content

    An object that represents the additional content for the document.

    This is the object returned from additionalContentForURL:error:.

    absoluteURL

    The URL to which to write the additional content.

    absoluteOriginalContentsURL

    The current URL of the document that is being saved.

    error

    Upon return, if a problem occurs, contains an error object that describes the problem.

    Return Value

    YEStrue if the write operation is successful, otherwise NOfalse.

    Discussion

    You override this method to perform to write non-Core Data content in the additional content directory in the document’s file package. There are several issues to consider:

    Special Considerations

    Additional content is not supported on iCloud.

    Import Statement

    import UIKit

    Availability

    Available in iOS 5.0 and later.

  • Returns the name for the persistent store file inside the document’s file package.

    Declaration

    Swift

    class func persistentStoreName() -> String

    Objective-C

    + (NSString *)persistentStoreName

    Return Value

    The name for the persistent store file inside the document’s file package.

    Discussion

    This path component is appended to the document URL provided by UIDocument. The default name is persistentStore.

    Import Statement

    import UIKit

    Availability

    Available in iOS 5.0 and later.