mirror of
https://github.com/JohnEstropia/CoreStore.git
synced 2026-01-17 06:27:02 +01:00
WIP: documentation
This commit is contained in:
@@ -29,16 +29,60 @@ import Foundation
|
||||
|
||||
// MARK: - XcodeDataModelSchema
|
||||
|
||||
/**
|
||||
The `XcodeDataModelSchema` describes a model version declared in a single *.xcdatamodeld file.
|
||||
```
|
||||
CoreStore.defaultStack = DataStack(
|
||||
XcodeDataModelSchema(modelName: "MyAppV1", bundle: .main)
|
||||
)
|
||||
```
|
||||
*/
|
||||
public final class XcodeDataModelSchema: DynamicSchema {
|
||||
|
||||
public required init(modelVersion: ModelVersion, modelVersionFileURL: URL) {
|
||||
/**
|
||||
Initializes an `XcodeDataModelSchema` from an *.xcdatamodeld version name and its containing `Bundle`.
|
||||
```
|
||||
CoreStore.defaultStack = DataStack(
|
||||
XcodeDataModelSchema(modelName: "MyAppV1", bundle: .main)
|
||||
)
|
||||
```
|
||||
- parameter modelName: the model version, typically the file name of an *.xcdatamodeld file (without the file extension)
|
||||
- parameter bundle: the `Bundle` that contains the .xcdatamodeld's "momd" file. If not specified, the `Bundle.main` will be searched.
|
||||
*/
|
||||
public convenience init(modelName: ModelVersion, bundle: Bundle = Bundle.main) {
|
||||
|
||||
guard let modelFilePath = bundle.path(forResource: modelName, ofType: "momd") else {
|
||||
|
||||
// For users migrating from very old Xcode versions: Old xcdatamodel files are not contained inside xcdatamodeld (with a "d"), and will thus fail this check. If that was the case, create a new xcdatamodeld file and copy all contents into the new model.
|
||||
let foundModels = bundle
|
||||
.paths(forResourcesOfType: "momd", inDirectory: nil)
|
||||
.map({ ($0 as NSString).lastPathComponent })
|
||||
CoreStore.abort("Could not find \"\(modelName).momd\" from the bundle \"\(bundle.bundleIdentifier ?? "<nil>")\". Other model files in bundle: \(foundModels.coreStoreDumpString)")
|
||||
}
|
||||
|
||||
let modelFileURL = URL(fileURLWithPath: modelFilePath)
|
||||
let fileURL = modelFileURL.appendingPathComponent("\(modelName).mom", isDirectory: false)
|
||||
self.init(modelName: modelName, modelVersionFileURL: fileURL)
|
||||
}
|
||||
|
||||
/**
|
||||
Initializes an `XcodeDataModelSchema` from an *.xcdatamodeld file URL.
|
||||
```
|
||||
CoreStore.defaultStack = DataStack(
|
||||
XcodeDataModelSchema(modelName: "MyAppV1", modelVersionFileURL: fileURL)
|
||||
)
|
||||
```
|
||||
- parameter modelName: the model version, typically the file name of an *.xcdatamodeld file (without the file extension)
|
||||
- parameter modelVersionFileURL: the file URL that points to the .xcdatamodeld's "momd" file.
|
||||
*/
|
||||
public required init(modelName: ModelVersion, modelVersionFileURL: URL) {
|
||||
|
||||
CoreStore.assert(
|
||||
NSManagedObjectModel(contentsOf: modelVersionFileURL) != nil,
|
||||
"Could not find the \"\(modelVersion).mom\" version file for the model at URL \"\(modelVersionFileURL)\"."
|
||||
"Could not find the \"\(modelName).mom\" version file for the model at URL \"\(modelVersionFileURL)\"."
|
||||
)
|
||||
|
||||
self.modelVersion = modelVersion
|
||||
self.modelVersion = modelName
|
||||
self.modelVersionFileURL = modelVersionFileURL
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user