comments cleanup

This commit is contained in:
John Rommel Estropia
2016-02-17 01:38:26 +09:00
parent def105e73a
commit 66892f22a3
47 changed files with 1749 additions and 1750 deletions
@@ -31,9 +31,9 @@ import CoreData
public extension NSFetchedResultsController {
// MARK: Public
/**
Utility for creating an `NSFetchedResultsController` from a `DataStack`. This is useful to partially support Objective-C classes by passing an `NSFetchedResultsController` instance instead of a `ListMonitor`.
*/
public func createForStack<T: NSManagedObject>(dataStack: DataStack, fetchRequest: NSFetchRequest, from: From<T>? = nil, sectionBy: SectionBy? = nil, fetchClauses: [FetchClause]) -> NSFetchedResultsController {
return CoreStoreFetchedResultsController<T>(
@@ -33,8 +33,6 @@ import Foundation
public extension NSProgress {
// MARK: Public
/**
Sets a closure that the `NSProgress` calls whenever its `fractionCompleted` changes. You can use this instead of setting up KVO.
- parameter closure: the closure to execute on progress change
-1
View File
@@ -28,4 +28,3 @@
FOUNDATION_EXPORT double CoreStoreVersionNumber;
FOUNDATION_EXPORT const unsigned char CoreStoreVersionString[];
+2 -4
View File
@@ -32,12 +32,10 @@ import CoreData
// MARK: - CoreStore
/**
`CoreStore` is the main entry point for all other APIs.
*/
`CoreStore` is the main entry point for all other APIs.
*/
public enum CoreStore {
// MARK: Public
/**
The default `DataStack` instance to be used. If `defaultStack` is not set before the first time accessed, a default-configured `DataStack` will be created.
@@ -31,8 +31,6 @@ import CoreData
public extension BaseDataTransaction {
// MARK: Public
/**
Fetches the `NSManagedObject` instance in the transaction's context from a reference created from a transaction or from a different managed object context.
@@ -30,38 +30,77 @@ import CoreData
// MARK: - From
/**
A `Form` clause binds the `NSManagedObject` entity type to the generics type system.
*/
A `From` clause specifies the source entity and source persistent store for fetch and query methods. A common usage is to just indicate the entity:
```
let person = transaction.fetchOne(From(MyPersonEntity))
```
For cases where multiple `NSPersistentStore`s contain the same entity, the source configuration's name needs to be specified as well:
```
let person = transaction.fetchOne(From<MyPersonEntity>("Configuration1"))
```
*/
public struct From<T: NSManagedObject> {
// MARK: Public
/**
Initializes a `From` clause with the specified entity type and configuration.
Initializes a `From` clause.
Sample Usage:
let person = transaction.fetchOne(From<MyPersonEntity>())
```
let people = transaction.fetchAll(From<MyPersonEntity>())
```
*/
public init(){
self.init(entityClass: T.self)
}
/**
Initializes a `From` clause with the specified entity type.
Sample Usage:
```
let people = transaction.fetchAll(From<MyPersonEntity>())
```
- parameter entity: the `NSManagedObject` type to be created
*/
public init(_ entity: T.Type) {
self.init(entityClass: entity)
}
/**
Initializes a `From` clause with the specified entity class.
Sample Usage:
```
let people = transaction.fetchAll(From<MyPersonEntity>())
```
- parameter entityClass: the `NSManagedObject` class type to be created
*/
public init(_ entityClass: AnyClass) {
self.init(entityClass: entityClass)
}
/**
Initializes a `From` clause with the specified configurations.
Sample Usage:
```
let people = transaction.fetchAll(From<MyPersonEntity>(nil, "Configuration1"))
```
- parameter configuration: the `NSPersistentStore` configuration name to associate objects from. This parameter is required if multiple configurations contain the created `NSManagedObject`'s entity type. Set to `nil` to use the default configuration.
- parameter otherConfigurations: an optional list of other configuration names to associate objects from (see `configuration` parameter)
*/
public init(_ configuration: String?, otherConfigurations: String?...) {
self.init(entityClass: T.self, configurations: [configuration] + otherConfigurations)
}
/**
Initializes a `From` clause with the specified configurations.
Sample Usage:
```
let people = transaction.fetchAll(From<MyPersonEntity>(["Configuration1", "Configuration2"]))
```
- parameter configurations: a list of `NSPersistentStore` configuration names to associate objects from. This parameter is required if multiple configurations contain the created `NSManagedObject`'s entity type. Set to `nil` to use the default configuration.
*/
public init(_ configurations: [String?]) {
self.init(entityClass: T.self, configurations: configurations)
@@ -30,12 +30,10 @@ import CoreData
// MARK: - GroupBy
/**
The `GroupBy` clause specifies that the result of a query be grouped accoording to the specified key path.
*/
The `GroupBy` clause specifies that the result of a query be grouped accoording to the specified key path.
*/
public struct GroupBy: QueryClause {
// MARK: Public
/**
Initializes a `GroupBy` clause with a list of key path strings
@@ -72,7 +70,7 @@ public struct GroupBy: QueryClause {
public func applyToFetchRequest(fetchRequest: NSFetchRequest) {
if fetchRequest.propertiesToGroupBy != nil {
if let keyPaths = fetchRequest.propertiesToGroupBy as? [String] where keyPaths != self.keyPaths {
CoreStore.log(
.Warning,
@@ -46,8 +46,8 @@ public typealias KeyPath = String
// MARK: - SortKey
/**
The `SortKey` is passed to the `OrderBy` clause to indicate the sort keys and their sort direction.
*/
The `SortKey` is passed to the `OrderBy` clause to indicate the sort keys and their sort direction.
*/
public enum SortKey {
/**
@@ -65,12 +65,10 @@ public enum SortKey {
// MARK: - OrderBy
/**
The `OrderBy` clause specifies the sort order for results for a fetch or a query.
*/
The `OrderBy` clause specifies the sort order for results for a fetch or a query.
*/
public struct OrderBy: FetchClause, QueryClause, DeleteClause {
// MARK: Public
/**
Initializes a `OrderBy` clause with a list of sort descriptors
@@ -139,7 +137,7 @@ public struct OrderBy: FetchClause, QueryClause, DeleteClause {
public func applyToFetchRequest(fetchRequest: NSFetchRequest) {
if fetchRequest.sortDescriptors != nil {
if let sortDescriptors = fetchRequest.sortDescriptors where sortDescriptors != self.sortDescriptors {
CoreStore.log(
.Warning,
@@ -30,16 +30,16 @@ import CoreData
// MARK: - SelectResultType
/**
The `SelectResultType` protocol is implemented by return types supported by the `Select` clause.
*/
The `SelectResultType` protocol is implemented by return types supported by the `Select` clause.
*/
public protocol SelectResultType { }
// MARK: - SelectValueResultType
/**
The `SelectValueResultType` protocol is implemented by return types supported by the `queryValue(...)` methods.
*/
The `SelectValueResultType` protocol is implemented by return types supported by the `queryValue(...)` methods.
*/
public protocol SelectValueResultType: SelectResultType {
static func fromResultObject(result: AnyObject) -> Self?
@@ -49,8 +49,8 @@ public protocol SelectValueResultType: SelectResultType {
// MARK: - SelectAttributesResultType
/**
The `SelectValueResultType` protocol is implemented by return types supported by the `queryAttributes(...)` methods.
*/
The `SelectValueResultType` protocol is implemented by return types supported by the `queryAttributes(...)` methods.
*/
public protocol SelectAttributesResultType: SelectResultType {
static func fromResultObjects(result: [AnyObject]) -> [[NSString: AnyObject]]
@@ -60,29 +60,27 @@ public protocol SelectAttributesResultType: SelectResultType {
// MARK: - SelectTerm
/**
The `SelectTerm` is passed to the `Select` clause to indicate the attributes/aggregate keys to be queried.
*/
The `SelectTerm` is passed to the `Select` clause to indicate the attributes/aggregate keys to be queried.
*/
public enum SelectTerm: StringLiteralConvertible {
// MARK: Public
/**
Provides a `SelectTerm` to a `Select` clause for querying an entity attribute. A shorter way to do the same is to assign from the string keypath directly:
```
let fullName = CoreStore.queryValue(
From(MyPersonEntity),
Select<String>(.Attribute("fullName")),
Where("employeeID", isEqualTo: 1111)
)
```
is equivalent to:
```
let fullName = CoreStore.queryValue(
From(MyPersonEntity),
Select<String>("fullName"),
Where("employeeID", isEqualTo: 1111)
)
```
- parameter keyPath: the attribute name
- returns: a `SelectTerm` to a `Select` clause for querying an entity attribute
*/
@@ -93,12 +91,12 @@ public enum SelectTerm: StringLiteralConvertible {
/**
Provides a `SelectTerm` to a `Select` clause for querying the average value of an attribute.
```
let averageAge = CoreStore.queryValue(
From(MyPersonEntity),
Select<Int>(.Average("age"))
)
```
- parameter keyPath: the attribute name
- parameter alias: the dictionary key to use to access the result. Ignored when the query return value is not an `NSDictionary`. If `nil`, the default key "average(<attributeName>)" is used
- returns: a `SelectTerm` to a `Select` clause for querying the average value of an attribute
@@ -115,12 +113,12 @@ public enum SelectTerm: StringLiteralConvertible {
/**
Provides a `SelectTerm` to a `Select` clause for a count query.
```
let numberOfEmployees = CoreStore.queryValue(
From(MyPersonEntity),
Select<Int>(.Count("employeeID"))
)
```
- parameter keyPath: the attribute name
- parameter alias: the dictionary key to use to access the result. Ignored when the query return value is not an `NSDictionary`. If `nil`, the default key "count(<attributeName>)" is used
- returns: a `SelectTerm` to a `Select` clause for a count query
@@ -137,12 +135,12 @@ public enum SelectTerm: StringLiteralConvertible {
/**
Provides a `SelectTerm` to a `Select` clause for querying the maximum value for an attribute.
```
let maximumAge = CoreStore.queryValue(
From(MyPersonEntity),
Select<Int>(.Maximum("age"))
)
```
- parameter keyPath: the attribute name
- parameter alias: the dictionary key to use to access the result. Ignored when the query return value is not an `NSDictionary`. If `nil`, the default key "max(<attributeName>)" is used
- returns: a `SelectTerm` to a `Select` clause for querying the maximum value for an attribute
@@ -159,12 +157,12 @@ public enum SelectTerm: StringLiteralConvertible {
/**
Provides a `SelectTerm` to a `Select` clause for querying the minimum value for an attribute.
```
let minimumAge = CoreStore.queryValue(
From(MyPersonEntity),
Select<Int>(.Minimum("age"))
)
```
- parameter keyPath: the attribute name
- parameter alias: the dictionary key to use to access the result. Ignored when the query return value is not an `NSDictionary`. If `nil`, the default key "min(<attributeName>)" is used
- returns: a `SelectTerm` to a `Select` clause for querying the minimum value for an attribute
@@ -181,12 +179,12 @@ public enum SelectTerm: StringLiteralConvertible {
/**
Provides a `SelectTerm` to a `Select` clause for querying the sum value for an attribute.
```
let totalAge = CoreStore.queryValue(
From(MyPersonEntity),
Select<Int>(.Sum("age"))
)
```
- parameter keyPath: the attribute name
- parameter alias: the dictionary key to use to access the result. Ignored when the query return value is not an `NSDictionary`. If `nil`, the default key "sum(<attributeName>)" is used
- returns: a `SelectTerm` to a `Select` clause for querying the sum value for an attribute
@@ -230,25 +228,25 @@ public enum SelectTerm: StringLiteralConvertible {
// MARK: - Select
/**
The `Select` clause indicates the attribute / aggregate value to be queried. The generic type is a `SelectResultType`, and will be used as the return type for the query.
You can bind the return type by specializing the initializer:
The `Select` clause indicates the attribute / aggregate value to be queried. The generic type is a `SelectResultType`, and will be used as the return type for the query.
You can bind the return type by specializing the initializer:
```
let maximumAge = CoreStore.queryValue(
From(MyPersonEntity),
Select<Int>(.Maximum("age"))
)
or by casting the type of the return value:
```
or by casting the type of the return value:
```
let maximumAge: Int = CoreStore.queryValue(
From(MyPersonEntity),
Select(.Maximum("age"))
)
```
Valid return types depend on the query:
Valid return types depend on the query:
- for `queryValue(...)` methods:
- for `queryValue(...)` methods:
- `Bool`
- `Int8`
- `Int16`
@@ -264,15 +262,13 @@ Valid return types depend on the query:
- `NSData`
- `NSManagedObjectID`
- `NSString`
- for `queryAttributes(...)` methods:
- for `queryAttributes(...)` methods:
- `NSDictionary`
- parameter sortDescriptors: a series of `NSSortDescriptor`s
*/
- parameter sortDescriptors: a series of `NSSortDescriptor`s
*/
public struct Select<T: SelectResultType> {
// MARK: Public
/**
The `SelectResultType` type for the query's return value
*/
@@ -30,10 +30,9 @@ import CoreData
// MARK: - Tweak
/**
The `Tweak` clause allows fine-tuning the `NSFetchRequest` for a fetch or query.
Sample usage:
The `Tweak` clause allows fine-tuning the `NSFetchRequest` for a fetch or query.
Sample usage:
```
let employees = transaction.fetchAll(
From(MyPersonEntity),
Tweak { (fetchRequest) -> Void in
@@ -41,11 +40,10 @@ Sample usage:
fetchRequest.fetchLimit = 5
}
)
*/
```
*/
public struct Tweak: FetchClause, QueryClause, DeleteClause {
// MARK: Public
/**
Initializes a `Tweak` clause with a closure where the `NSFetchRequest` may be configured.
@@ -65,5 +63,7 @@ public struct Tweak: FetchClause, QueryClause, DeleteClause {
}
// MARK: Private
private let customization: (fetchRequest: NSFetchRequest) -> Void
}
@@ -46,12 +46,10 @@ public prefix func !(clause: Where) -> Where {
// MARK: - Where
/**
The `Where` clause specifies the conditions for a fetch or a query.
*/
The `Where` clause specifies the conditions for a fetch or a query.
*/
public struct Where: FetchClause, QueryClause, DeleteClause {
// MARK: Public
/**
Initializes a `Where` clause with an `NSPredicate`
@@ -144,7 +142,7 @@ public struct Where: FetchClause, QueryClause, DeleteClause {
public func applyToFetchRequest(fetchRequest: NSFetchRequest) {
if fetchRequest.predicate != nil {
if let predicate = fetchRequest.predicate where predicate != self.predicate {
CoreStore.log(
.Warning,
@@ -31,8 +31,6 @@ import CoreData
public extension CoreStore {
// MARK: Public
/**
Using the `defaultStack`, fetches the `NSManagedObject` instance in the `DataStack`'s context from a reference created from a transaction or from a different managed object context.
@@ -34,8 +34,6 @@ import CoreData
public extension DataStack {
// MARK: Public
/**
Fetches the `NSManagedObject` instance in the `DataStack`'s context from a reference created from a transaction or from a different managed object context.
@@ -29,6 +29,9 @@ import CoreData
// MARK: - FetchClause
/**
The `FetchClause` implement clauses used to configure `NSFetchRequest`s.
*/
public protocol FetchClause {
func applyToFetchRequest(fetchRequest: NSFetchRequest)
@@ -37,6 +40,9 @@ public protocol FetchClause {
// MARK: - QueryClause
/**
The `QueryClause` implement clauses used to configure `NSFetchRequest`s.
*/
public protocol QueryClause {
func applyToFetchRequest(fetchRequest: NSFetchRequest)
@@ -45,6 +51,9 @@ public protocol QueryClause {
// MARK: - DeleteClause
/**
The `DeleteClause` implement clauses used to configure `NSFetchRequest`s.
*/
public protocol DeleteClause {
func applyToFetchRequest(fetchRequest: NSFetchRequest)
@@ -31,8 +31,6 @@ import CoreData
public extension BaseDataTransaction {
// MARK: Public
/**
Creates an `ImportableObject` by importing from the specified import source.
@@ -30,8 +30,8 @@ import CoreData
// MARK: - ImportableObject
/**
`NSManagedObject` subclasses that conform to the `ImportableObject` protocol can be imported from a specified `ImportSource`. This allows transactions to create and insert instances this way:
`NSManagedObject` subclasses that conform to the `ImportableObject` protocol can be imported from a specified `ImportSource`. This allows transactions to create and insert instances this way:
```
class MyPersonEntity: NSManagedObject, ImportableObject {
typealias ImportSource = NSDictionary
// ...
@@ -46,7 +46,8 @@ import CoreData
// ...
transaction.commit()
}
*/
```
*/
public protocol ImportableObject: class {
/**
@@ -30,8 +30,8 @@ import CoreData
// MARK: - ImportableUniqueObject
/**
`NSManagedObject` subclasses that conform to the `ImportableUniqueObject` protocol can be imported from a specified `ImportSource`. This allows transactions to either update existing objects or create new instances this way:
`NSManagedObject` subclasses that conform to the `ImportableUniqueObject` protocol can be imported from a specified `ImportSource`. This allows transactions to either update existing objects or create new instances this way:
```
class MyPersonEntity: NSManagedObject, ImportableUniqueObject {
typealias ImportSource = NSDictionary
typealias UniqueIDType = NSString
@@ -47,7 +47,8 @@ import CoreData
// ...
transaction.commit()
}
*/
```
*/
public protocol ImportableUniqueObject: ImportableObject {
/**
@@ -30,8 +30,6 @@ import Foundation
public extension CoreStore {
// MARK: Public
/**
The `CoreStoreLogger` instance to be used. The default logger is an instance of a `DefaultLogger`.
*/
+5 -6
View File
@@ -29,8 +29,8 @@ import Foundation
// MARK: - LogLevel
/**
The `LogLevel` indicates the severity of a log message.
*/
The `LogLevel` indicates the severity of a log message.
*/
public enum LogLevel {
case Trace
@@ -43,8 +43,8 @@ public enum LogLevel {
// MARK: - CoreStoreLogger
/**
Custom loggers should implement the `CoreStoreLogger` protocol and pass its instance to `CoreStore.logger`. Calls to `log(...)`, `handleError(...)`, and `assert(...)` are not tied to a specific queue/thread, so it is the implementer's job to handle thread-safety.
*/
Custom loggers should implement the `CoreStoreLogger` protocol and pass its instance to `CoreStore.logger`. Calls to `log(...)`, `handleError(...)`, and `assert(...)` are not tied to a specific queue/thread, so it is the implementer's job to handle thread-safety.
*/
public protocol CoreStoreLogger {
/**
@@ -101,6 +101,5 @@ internal func typeName(value: AnyClass) -> String {
internal func typeName(name: String?) -> String {
let typeName = name ?? "unknown"
return "<\(typeName)>"
return "<\(name ?? "unknown")>"
}
+5 -5
View File
@@ -29,12 +29,12 @@ import Foundation
// MARK: - DefaultLogger
/**
The `DefaultLogger` is a basic implementation of the `CoreStoreLogger` protocol.
The `DefaultLogger` is a basic implementation of the `CoreStoreLogger` protocol.
- The `log(...)` method calls `print(...)` to print the level, source file name, line number, function name, and the log message.
- The `handleError(...)` method calls `print(...)` to print the source file name, line number, function name, and the error message.
- The `assert(...)` method calls `assert(...)` on the arguments.
*/
- The `log(...)` method calls `print(...)` to print the level, source file name, line number, function name, and the log message.
- The `handleError(...)` method calls `print(...)` to print the source file name, line number, function name, and the error message.
- The `assert(...)` method calls `assert(...)` on the arguments.
*/
public final class DefaultLogger: CoreStoreLogger {
public init() { }
+20 -20
View File
@@ -30,36 +30,36 @@ import CoreData
// MARK: - MigrationChain
/**
A `MigrationChain` indicates the sequence of model versions to be used as the order for incremental migration. This is typically passed to the `DataStack` initializer and will be applied to all stores added to the `DataStack` with `addSQLiteStore(...)` and its variants.
Initializing with empty values (either `nil`, `[]`, or `[:]`) instructs the `DataStack` to use the .xcdatamodel's current version as the final version, and to disable incremental migrations:
A `MigrationChain` indicates the sequence of model versions to be used as the order for incremental migration. This is typically passed to the `DataStack` initializer and will be applied to all stores added to the `DataStack` with `addSQLiteStore(...)` and its variants.
Initializing with empty values (either `nil`, `[]`, or `[:]`) instructs the `DataStack` to use the .xcdatamodel's current version as the final version, and to disable incremental migrations:
```
let dataStack = DataStack(migrationChain: nil)
This means that the mapping model will be computed from the store's version straight to the `DataStack`'s model version.
To support incremental migrations, specify the linear order of versions:
```
This means that the mapping model will be computed from the store's version straight to the `DataStack`'s model version.
To support incremental migrations, specify the linear order of versions:
```
let dataStack = DataStack(migrationChain:
["MyAppModel", "MyAppModelV2", "MyAppModelV3", "MyAppModelV4"])
or for more complex migration paths, a version tree that maps the key-values to the source-destination versions:
```
or for more complex migration paths, a version tree that maps the key-values to the source-destination versions:
```
let dataStack = DataStack(migrationChain: [
"MyAppModel": "MyAppModelV3",
"MyAppModelV2": "MyAppModelV4",
"MyAppModelV3": "MyAppModelV4"
])
```
This allows for different migration paths depending on the starting version. The example above resolves to the following paths:
- MyAppModel-MyAppModelV3-MyAppModelV4
- MyAppModelV2-MyAppModelV4
- MyAppModelV3-MyAppModelV4
This allows for different migration paths depending on the starting version. The example above resolves to the following paths:
- MyAppModel-MyAppModelV3-MyAppModelV4
- MyAppModelV2-MyAppModelV4
- MyAppModelV3-MyAppModelV4
The `MigrationChain` is validated when passed to the `DataStack` and unless it is empty, will raise an assertion if any of the following conditions are met:
- a version appears twice in an array
- a version appears twice as a key in a dictionary literal
- a loop is found in any of the paths
*/
The `MigrationChain` is validated when passed to the `DataStack` and unless it is empty, will raise an assertion if any of the following conditions are met:
- a version appears twice in an array
- a version appears twice as a key in a dictionary literal
- a loop is found in any of the paths
*/
public struct MigrationChain: NilLiteralConvertible, StringLiteralConvertible, DictionaryLiteralConvertible, ArrayLiteralConvertible {
// MARK: NilLiteralConvertible
+9 -10
View File
@@ -29,9 +29,9 @@ import Foundation
// MARK: - MigrationResult
/**
The `MigrationResult` indicates the result of a migration.
The `MigrationResult` can be treated as a boolean:
The `MigrationResult` indicates the result of a migration.
The `MigrationResult` can be treated as a boolean:
```
CoreStore.upgradeSQLiteStoreIfNeeded { transaction in
// ...
let result = transaction.commit()
@@ -42,9 +42,9 @@ The `MigrationResult` can be treated as a boolean:
// failed
}
}
or as an `enum`, where the resulting associated object can also be inspected:
```
or as an `enum`, where the resulting associated object can also be inspected:
```
CoreStore.beginAsynchronous { transaction in
// ...
let result = transaction.commit()
@@ -55,12 +55,10 @@ or as an `enum`, where the resulting associated object can also be inspected:
// error is the NSError instance for the failure
}
}
```
*/
```
*/
public enum MigrationResult {
// MARK: Public
/**
`MigrationResult.Success` indicates either the migration succeeded, or there were no migrations needed. The associated value is an array of `MigrationType`s reflecting the migration steps completed.
*/
@@ -103,6 +101,7 @@ extension MigrationResult: BooleanType {
public var boolValue: Bool {
switch self {
case .Success: return true
case .Failure: return false
}
+2 -4
View File
@@ -29,12 +29,10 @@ import Foundation
// MARK: - MigrationType
/**
The `MigrationType` specifies the type of migration required for a store.
*/
The `MigrationType` specifies the type of migration required for a store.
*/
public enum MigrationType: BooleanType {
// MARK: Public
/**
Indicates that the persistent store matches the latest model version and no migration is needed
*/
+8 -5
View File
@@ -26,14 +26,17 @@
import Foundation
import CoreData
// MARK: - CoreStoreError
/**
The `NSError` error domain for `CoreStore`.
*/
The `NSError` error domain for `CoreStore`.
*/
public let CoreStoreErrorDomain = "com.corestore.error"
/**
The `NSError` error codes for `CoreStoreErrorDomain`.
*/
The `NSError` error codes for `CoreStoreErrorDomain`.
*/
public enum CoreStoreErrorCode: Int {
/**
@@ -58,7 +61,7 @@ public enum CoreStoreErrorCode: Int {
}
// MARK: - NSError+CoreStore
// MARK: - NSError
public extension NSError {
@@ -32,8 +32,6 @@ import CoreData
@available(OSX, unavailable)
public extension CoreStore {
// MARK: Public
/**
Using the `defaultStack`, creates a `ObjectMonitor` for the specified `NSManagedObject`. Multiple `ObjectObserver`s may then register themselves to be notified when changes are made to the `NSManagedObject`.
@@ -35,8 +35,6 @@ import CoreData
@available(OSX, unavailable)
public extension DataStack {
// MARK: Public
/**
Creates a `ObjectMonitor` for the specified `NSManagedObject`. Multiple `ObjectObserver`s may then register themselves to be notified when changes are made to the `NSManagedObject`.
+17 -17
View File
@@ -33,26 +33,26 @@ import CoreData
// MARK: - ListMonitor
/**
The `ListMonitor` monitors changes to a list of `NSManagedObject` instances. Observers that implement the `ListObserver` protocol may then register themselves to the `ListMonitor`'s `addObserver(_:)` method:
The `ListMonitor` monitors changes to a list of `NSManagedObject` instances. Observers that implement the `ListObserver` protocol may then register themselves to the `ListMonitor`'s `addObserver(_:)` method:
```
let monitor = CoreStore.monitorList(
From(MyPersonEntity),
Where("title", isEqualTo: "Engineer"),
OrderBy(.Ascending("lastName"))
)
monitor.addObserver(self)
```
The `ListMonitor` instance needs to be held on (retained) for as long as the list needs to be observed.
Observers registered via `addObserver(_:)` are not retained. `ListMonitor` only keeps a `weak` reference to all observers, thus keeping itself free from retain-cycles.
The `ListMonitor` instance needs to be held on (retained) for as long as the list needs to be observed.
Observers registered via `addObserver(_:)` are not retained. `ListMonitor` only keeps a `weak` reference to all observers, thus keeping itself free from retain-cycles.
Lists created with `monitorList(...)` keep a single-section list of objects, where each object can be accessed by index:
Lists created with `monitorList(...)` keep a single-section list of objects, where each object can be accessed by index:
```
let firstPerson: MyPersonEntity = monitor[0]
```
Accessing the list with an index above the valid range will throw an exception.
Accessing the list with an index above the valid range will throw an exception.
Creating a sectioned-list is also possible with the `monitorSectionedList(...)` method:
Creating a sectioned-list is also possible with the `monitorSectionedList(...)` method:
```
let monitor = CoreStore.monitorSectionedList(
From(MyPersonEntity),
SectionBy("age") { "Age \($0)" },
@@ -60,15 +60,15 @@ Creating a sectioned-list is also possible with the `monitorSectionedList(...)`
OrderBy(.Ascending("lastName"))
)
monitor.addObserver(self)
Objects from `ListMonitor`s created this way can be accessed either by an `NSIndexPath` or a tuple:
```
Objects from `ListMonitor`s created this way can be accessed either by an `NSIndexPath` or a tuple:
```
let indexPath = NSIndexPath(forItem: 3, inSection: 2)
let person1 = monitor[indexPath]
let person2 = monitor[2, 3]
In the example above, both `person1` and `person2` will contain the object at section=2, index=3.
*/
```
In the example above, both `person1` and `person2` will contain the object at section=2, index=3.
*/
@available(OSX, unavailable)
public final class ListMonitor<T: NSManagedObject> {
+12 -9
View File
@@ -30,14 +30,15 @@ import CoreData
// MARK: - ListObserver
/**
Implement the `ListObserver` protocol to observe changes to a list of `NSManagedObject`s. `ListObserver`s may register themselves to a `ListMonitor`'s `addObserver(_:)` method:
Implement the `ListObserver` protocol to observe changes to a list of `NSManagedObject`s. `ListObserver`s may register themselves to a `ListMonitor`'s `addObserver(_:)` method:
```
let monitor = CoreStore.monitorList(
From(MyPersonEntity),
OrderBy(.Ascending("lastName"))
)
monitor.addObserver(self)
*/
```
*/
@available(OSX, unavailable)
public protocol ListObserver: class {
@@ -106,14 +107,15 @@ public extension ListObserver {
// MARK: - ListObjectObserver
/**
Implement the `ListObjectObserver` protocol to observe detailed changes to a list's object. `ListObjectObserver`s may register themselves to a `ListMonitor`'s `addObserver(_:)` method:
Implement the `ListObjectObserver` protocol to observe detailed changes to a list's object. `ListObjectObserver`s may register themselves to a `ListMonitor`'s `addObserver(_:)` method:
```
let monitor = CoreStore.monitorList(
From(MyPersonEntity),
OrderBy(.Ascending("lastName"))
)
monitor.addObserver(self)
*/
```
*/
@available(OSX, unavailable)
public protocol ListObjectObserver: ListObserver {
@@ -186,15 +188,16 @@ public extension ListObjectObserver {
// MARK: - ListSectionObserver
/**
Implement the `ListSectionObserver` protocol to observe changes to a list's section info. `ListSectionObserver`s may register themselves to a `ListMonitor`'s `addObserver(_:)` method:
Implement the `ListSectionObserver` protocol to observe changes to a list's section info. `ListSectionObserver`s may register themselves to a `ListMonitor`'s `addObserver(_:)` method:
```
let monitor = CoreStore.monitorSectionedList(
From(MyPersonEntity),
SectionBy("age") { "Age \($0)" },
OrderBy(.Ascending("lastName"))
)
monitor.addObserver(self)
*/
```
*/
@available(OSX, unavailable)
public protocol ListSectionObserver: ListObjectObserver {
+6 -8
View File
@@ -33,20 +33,18 @@ import CoreData
// MARK: - ObjectMonitor
/**
The `ObjectMonitor` monitors changes to a single `NSManagedObject` instance. Observers that implement the `ObjectObserver` protocol may then register themselves to the `ObjectMonitor`'s `addObserver(_:)` method:
The `ObjectMonitor` monitors changes to a single `NSManagedObject` instance. Observers that implement the `ObjectObserver` protocol may then register themselves to the `ObjectMonitor`'s `addObserver(_:)` method:
```
let monitor = CoreStore.monitorObject(object)
monitor.addObserver(self)
```
The created `ObjectMonitor` instance needs to be held on (retained) for as long as the object needs to be observed.
The created `ObjectMonitor` instance needs to be held on (retained) for as long as the object needs to be observed.
Observers registered via `addObserver(_:)` are not retained. `ObjectMonitor` only keeps a `weak` reference to all observers, thus keeping itself free from retain-cycles.
*/
Observers registered via `addObserver(_:)` are not retained. `ObjectMonitor` only keeps a `weak` reference to all observers, thus keeping itself free from retain-cycles.
*/
@available(OSX, unavailable)
public final class ObjectMonitor<T: NSManagedObject> {
// MARK: Public
/**
Returns the `NSManagedObject` instance being observed, or `nil` if the object was already deleted.
*/
+4 -3
View File
@@ -30,11 +30,12 @@ import CoreData
// MARK: - ObjectObserver
/**
Implement the `ObjectObserver` protocol to observe changes to a single `NSManagedObject` instance. `ObjectObserver`s may register themselves to a `ObjectMonitor`'s `addObserver(_:)` method:
Implement the `ObjectObserver` protocol to observe changes to a single `NSManagedObject` instance. `ObjectObserver`s may register themselves to a `ObjectMonitor`'s `addObserver(_:)` method:
```
let monitor = CoreStore.monitorObject(object)
monitor.addObserver(self)
*/
```
*/
@available(OSX, unavailable)
public protocol ObjectObserver: class {
+4 -5
View File
@@ -30,19 +30,18 @@ import CoreData
// MARK: - SectionBy
/**
The `SectionBy` clause indicates the key path to use to group the `ListMonitor` objects into sections. An optional closure can also be provided to transform the value into an appropriate section name:
The `SectionBy` clause indicates the key path to use to group the `ListMonitor` objects into sections. An optional closure can also be provided to transform the value into an appropriate section name:
```
let monitor = CoreStore.monitorSectionedList(
From(MyPersonEntity),
SectionBy("age") { "Age \($0)" },
OrderBy(.Ascending("lastName"))
)
*/
```
*/
@available(OSX, unavailable)
public struct SectionBy {
// MARK: Public
/**
Initializes a `SectionBy` clause with the key path to use to group `ListMonitor` objects into sections
@@ -35,9 +35,6 @@ import CoreData
@available(OSX, unavailable)
public extension UnsafeDataTransaction {
// MARK: Public
/**
Creates a `ObjectMonitor` for the specified `NSManagedObject`. Multiple `ObjectObserver`s may then register themselves to be notified when changes are made to the `NSManagedObject`.
@@ -33,12 +33,10 @@ import CoreData
// MARK: - AsynchronousDataTransaction
/**
The `AsynchronousDataTransaction` provides an interface for `NSManagedObject` creates, updates, and deletes. A transaction object should typically be only used from within a transaction block initiated from `DataStack.beginAsynchronous(_:)`, or from `CoreStore.beginAsynchronous(_:)`.
*/
The `AsynchronousDataTransaction` provides an interface for `NSManagedObject` creates, updates, and deletes. A transaction object should typically be only used from within a transaction block initiated from `DataStack.beginAsynchronous(_:)`, or from `CoreStore.beginAsynchronous(_:)`.
*/
public final class AsynchronousDataTransaction: BaseDataTransaction {
// MARK: Public
/**
Saves the transaction changes. This method should not be used after the `commit()` method was already called once.
@@ -33,8 +33,8 @@ import CoreData
// MARK: - BaseDataTransaction
/**
The `BaseDataTransaction` is an abstract interface for `NSManagedObject` creates, updates, and deletes. All `BaseDataTransaction` subclasses manage a private `NSManagedObjectContext` which are direct children of the `NSPersistentStoreCoordinator`'s root `NSManagedObjectContext`. This means that all updates are saved first to the persistent store, and then propagated up to the read-only `NSManagedObjectContext`.
*/
The `BaseDataTransaction` is an abstract interface for `NSManagedObject` creates, updates, and deletes. All `BaseDataTransaction` subclasses manage a private `NSManagedObjectContext` which are direct children of the `NSPersistentStoreCoordinator`'s root `NSManagedObjectContext`. This means that all updates are saved first to the persistent store, and then propagated up to the read-only `NSManagedObjectContext`.
*/
public /*abstract*/ class BaseDataTransaction {
// MARK: Object management
@@ -30,8 +30,6 @@ import Foundation
public extension CoreStore {
// MARK: Public
/**
Using the `defaultStack`, begins a transaction asynchronously where `NSManagedObject` creates, updates, and deletes can be made.
@@ -34,8 +34,6 @@ import CoreData
public extension DataStack {
// MARK: Public
/**
Begins a transaction asynchronously where `NSManagedObject` creates, updates, and deletes can be made.
+22 -21
View File
@@ -30,25 +30,23 @@ import CoreData
// MARK: - Into
/**
A `Into` clause contains the destination entity and destination persistent store for a `create(...)` method. A common usage is to just indicate the entity:
let person = transaction.create(Into(MyPersonEntity))
For cases where multiple `NSPersistentStore`s contain the same entity, the destination configuration's name needs to be specified as well:
An `Into` clause contains the destination entity and destination persistent store for a `create(...)` method. A common usage is to just indicate the entity:
```
let person = transaction.create(Into(MyPersonEntity))
```
For cases where multiple `NSPersistentStore`s contain the same entity, the destination configuration's name needs to be specified as well:
```
let person = transaction.create(Into<MyPersonEntity>("Configuration1"))
This helps the `NSManagedObjectContext` to determine which
*/
```
*/
public struct Into<T: NSManagedObject> {
// MARK: Public
/**
Initializes an `Into` clause.
Sample Usage:
```
let person = transaction.create(Into<MyPersonEntity>())
```
*/
public init(){
@@ -60,9 +58,9 @@ public struct Into<T: NSManagedObject> {
/**
Initializes an `Into` clause with the specified entity type.
Sample Usage:
```
let person = transaction.create(Into(MyPersonEntity))
```
- parameter entity: the `NSManagedObject` type to be created
*/
public init(_ entity: T.Type) {
@@ -74,7 +72,10 @@ public struct Into<T: NSManagedObject> {
/**
Initializes an `Into` clause with the specified entity class.
Sample Usage:
```
let person = transaction.create(Into(MyPersonEntity))
```
- parameter entityClass: the `NSManagedObject` class type to be created
*/
public init(_ entityClass: AnyClass) {
@@ -87,9 +88,9 @@ public struct Into<T: NSManagedObject> {
/**
Initializes an `Into` clause with the specified configuration.
Sample Usage:
```
let person = transaction.create(Into<MyPersonEntity>("Configuration1"))
```
- parameter configuration: the `NSPersistentStore` configuration name to associate the object to. This parameter is required if multiple configurations contain the created `NSManagedObject`'s entity type. Set to `nil` to use the default configuration.
*/
public init(_ configuration: String?) {
@@ -102,9 +103,9 @@ public struct Into<T: NSManagedObject> {
/**
Initializes an `Into` clause with the specified entity type and configuration.
Sample Usage:
```
let person = transaction.create(Into(MyPersonEntity.self, "Configuration1"))
```
- parameter entity: the `NSManagedObject` type to be created
- parameter configuration: the `NSPersistentStore` configuration name to associate the object to. This parameter is required if multiple configurations contain the created `NSManagedObject`'s entity type. Set to `nil` to use the default configuration.
*/
@@ -118,9 +119,9 @@ public struct Into<T: NSManagedObject> {
/**
Initializes an `Into` clause with the specified entity class and configuration.
Sample Usage:
```
let person = transaction.create(Into(MyPersonEntity.self, "Configuration1"))
```
- parameter entityClass: the `NSManagedObject` class type to be created
- parameter configuration: the `NSPersistentStore` configuration name to associate the object to. This parameter is required if multiple configurations contain the created `NSManagedObject`'s entity type. Set to `nil` to use the default configuration.
*/
@@ -26,12 +26,11 @@
import Foundation
import CoreData
// MARK: - NSManagedObject
public extension NSManagedObject {
// MARK: Public
/**
Returns this object's parent `UnsafeDataTransaction` instance if it was created from one. Returns `nil` if the parent transaction is either an `AsynchronousDataTransaction` or a `SynchronousDataTransaction`, or if the object is not managed by CoreStore.
@@ -29,9 +29,9 @@ import Foundation
// MARK: - SaveResult
/**
The `SaveResult` indicates the result of a `commit(...)` for a transaction.
The `SaveResult` can be treated as a boolean:
The `SaveResult` indicates the result of a `commit(...)` for a transaction.
The `SaveResult` can be treated as a boolean:
```
CoreStore.beginAsynchronous { transaction in
// ...
let result = transaction.commit()
@@ -42,9 +42,9 @@ The `SaveResult` can be treated as a boolean:
// failed
}
}
or as an `enum`, where the resulting associated object can also be inspected:
```
or as an `enum`, where the resulting associated object can also be inspected:
```
CoreStore.beginAsynchronous { transaction in
// ...
let result = transaction.commit()
@@ -55,12 +55,10 @@ or as an `enum`, where the resulting associated object can also be inspected:
// error is the NSError instance for the failure
}
}
```
*/
```
*/
public enum SaveResult {
// MARK: Public
/**
`SaveResult.Success` indicates that the `commit()` for the transaction succeeded, either because the save succeeded or because there were no changes to save. The associated value `hasChanges` indicates if there were saved changes or not.
*/
@@ -103,6 +101,7 @@ extension SaveResult: BooleanType {
public var boolValue: Bool {
switch self {
case .Success: return true
case .Failure: return false
}
@@ -33,12 +33,10 @@ import CoreData
// MARK: - SynchronousDataTransaction
/**
The `SynchronousDataTransaction` provides an interface for `NSManagedObject` creates, updates, and deletes. A transaction object should typically be only used from within a transaction block initiated from `DataStack.beginSynchronous(_:)`, or from `CoreStore.beginSynchronous(_:)`.
*/
The `SynchronousDataTransaction` provides an interface for `NSManagedObject` creates, updates, and deletes. A transaction object should typically be only used from within a transaction block initiated from `DataStack.beginSynchronous(_:)`, or from `CoreStore.beginSynchronous(_:)`.
*/
public final class SynchronousDataTransaction: BaseDataTransaction {
// MARK: Public
/**
Saves the transaction changes and waits for completion synchronously. This method should not be used after the `commit()` method was already called once.
*/
@@ -37,12 +37,10 @@ public typealias DetachedDataTransaction = UnsafeDataTransaction
// MARK: - UnsafeDataTransaction
/**
The `UnsafeDataTransaction` provides an interface for non-contiguous `NSManagedObject` creates, updates, and deletes. This is useful for making temporary changes, such as partially filled forms. An unsafe transaction object should typically be only used from the main queue.
*/
The `UnsafeDataTransaction` provides an interface for non-contiguous `NSManagedObject` creates, updates, and deletes. This is useful for making temporary changes, such as partially filled forms. An unsafe transaction object should typically be only used from the main queue.
*/
public final class UnsafeDataTransaction: BaseDataTransaction {
// MARK: Public
/**
Saves the transaction changes asynchronously. For a `UnsafeDataTransaction`, multiple commits are allowed, although it is the developer's responsibility to ensure a reasonable leeway to prevent blocking the main thread.
+2 -4
View File
@@ -46,12 +46,10 @@ internal let defaultSQLiteStoreURL = defaultDirectory.URLByAppendingPathComponen
// MARK: - DataStack
/**
The `DataStack` encapsulates the data model for the Core Data stack. Each `DataStack` can have multiple data stores, usually specified as a "Configuration" in the model editor. Behind the scenes, the DataStack manages its own `NSPersistentStoreCoordinator`, a root `NSManagedObjectContext` for disk saves, and a shared `NSManagedObjectContext` designed as a read-only model interface for `NSManagedObjects`.
*/
The `DataStack` encapsulates the data model for the Core Data stack. Each `DataStack` can have multiple data stores, usually specified as a "Configuration" in the model editor. Behind the scenes, the DataStack manages its own `NSPersistentStoreCoordinator`, a root `NSManagedObjectContext` for disk saves, and a shared `NSManagedObjectContext` designed as a read-only model interface for `NSManagedObjects`.
*/
public final class DataStack {
// MARK: Public
/**
Initializes a `DataStack` from an `NSManagedObjectModel`.
@@ -30,9 +30,9 @@ import CoreData
// MARK: - PersistentStoreResult
/**
The `PersistentStoreResult` indicates the result of an asynchronous initialization of a persistent store.
The `PersistentStoreResult` can be treated as a boolean:
The `PersistentStoreResult` indicates the result of an asynchronous initialization of a persistent store.
The `PersistentStoreResult` can be treated as a boolean:
```
try! CoreStore.addSQLiteStore(completion: { (result: PersistentStoreResult) -> Void in
if result {
// succeeded
@@ -41,9 +41,9 @@ The `PersistentStoreResult` can be treated as a boolean:
// failed
}
})
or as an `enum`, where the resulting associated object can also be inspected:
```
or as an `enum`, where the resulting associated object can also be inspected:
```
try! CoreStore.addSQLiteStore(completion: { (result: PersistentStoreResult) -> Void in
switch result {
case .Success(let persistentStore):
@@ -52,12 +52,10 @@ or as an `enum`, where the resulting associated object can also be inspected:
// error is the NSError instance for the failure
}
})
```
*/
```
*/
public enum PersistentStoreResult {
// MARK: Public
/**
`PersistentStoreResult.Success` indicates that the persistent store process succeeded. The associated object for this `enum` value is the related `NSPersistentStore` instance.
*/
@@ -88,9 +86,7 @@ public enum PersistentStoreResult {
internal init(_ errorCode: CoreStoreErrorCode, userInfo: [NSObject: AnyObject]?) {
self.init(NSError(
coreStoreErrorCode: errorCode,
userInfo: userInfo))
self.init(NSError(coreStoreErrorCode: errorCode, userInfo: userInfo))
}
}
@@ -99,8 +95,6 @@ public enum PersistentStoreResult {
extension PersistentStoreResult: BooleanType {
// MARK: Public
public var boolValue: Bool {
switch self {