changed keyPath string utility to use String initializer

This commit is contained in:
John Rommel Estropia
2017-12-29 00:05:11 +09:00
parent f447bcfb95
commit 15edabdbb5
11 changed files with 407 additions and 318 deletions

View File

@@ -532,10 +532,10 @@
B5CA2B091F7E5ACA004B1936 /* WhereClauseType.swift in Sources */ = {isa = PBXBuildFile; fileRef = B5CA2B071F7E5ACA004B1936 /* WhereClauseType.swift */; };
B5CA2B0A1F7E5ACA004B1936 /* WhereClauseType.swift in Sources */ = {isa = PBXBuildFile; fileRef = B5CA2B071F7E5ACA004B1936 /* WhereClauseType.swift */; };
B5CA2B0B1F7E5ACA004B1936 /* WhereClauseType.swift in Sources */ = {isa = PBXBuildFile; fileRef = B5CA2B071F7E5ACA004B1936 /* WhereClauseType.swift */; };
B5CA2B121F81DBFE004B1936 /* AnyCoreStoreKeyPath.swift in Sources */ = {isa = PBXBuildFile; fileRef = B5CA2B111F81DBFE004B1936 /* AnyCoreStoreKeyPath.swift */; };
B5CA2B131F81DBFE004B1936 /* AnyCoreStoreKeyPath.swift in Sources */ = {isa = PBXBuildFile; fileRef = B5CA2B111F81DBFE004B1936 /* AnyCoreStoreKeyPath.swift */; };
B5CA2B141F81DBFE004B1936 /* AnyCoreStoreKeyPath.swift in Sources */ = {isa = PBXBuildFile; fileRef = B5CA2B111F81DBFE004B1936 /* AnyCoreStoreKeyPath.swift */; };
B5CA2B151F81DBFF004B1936 /* AnyCoreStoreKeyPath.swift in Sources */ = {isa = PBXBuildFile; fileRef = B5CA2B111F81DBFE004B1936 /* AnyCoreStoreKeyPath.swift */; };
B5CA2B121F81DBFE004B1936 /* DynamicKeyPath.swift in Sources */ = {isa = PBXBuildFile; fileRef = B5CA2B111F81DBFE004B1936 /* DynamicKeyPath.swift */; };
B5CA2B131F81DBFE004B1936 /* DynamicKeyPath.swift in Sources */ = {isa = PBXBuildFile; fileRef = B5CA2B111F81DBFE004B1936 /* DynamicKeyPath.swift */; };
B5CA2B141F81DBFE004B1936 /* DynamicKeyPath.swift in Sources */ = {isa = PBXBuildFile; fileRef = B5CA2B111F81DBFE004B1936 /* DynamicKeyPath.swift */; };
B5CA2B151F81DBFF004B1936 /* DynamicKeyPath.swift in Sources */ = {isa = PBXBuildFile; fileRef = B5CA2B111F81DBFE004B1936 /* DynamicKeyPath.swift */; };
B5D1E22C19FA9FBC003B2874 /* CoreStoreError.swift in Sources */ = {isa = PBXBuildFile; fileRef = B5D1E22B19FA9FBC003B2874 /* CoreStoreError.swift */; };
B5D339B41E925C2B00C880DE /* DynamicModelTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = B5D339B31E925C2B00C880DE /* DynamicModelTests.swift */; };
B5D339B51E925C2B00C880DE /* DynamicModelTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = B5D339B31E925C2B00C880DE /* DynamicModelTests.swift */; };
@@ -881,7 +881,7 @@
B5C976E21C6C9F6A00B1AF90 /* UnsafeDataTransaction+Observing.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "UnsafeDataTransaction+Observing.swift"; sourceTree = "<group>"; };
B5C976E61C6E3A5900B1AF90 /* CoreStoreFetchedResultsController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = CoreStoreFetchedResultsController.swift; sourceTree = "<group>"; };
B5CA2B071F7E5ACA004B1936 /* WhereClauseType.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = WhereClauseType.swift; sourceTree = "<group>"; };
B5CA2B111F81DBFE004B1936 /* AnyCoreStoreKeyPath.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AnyCoreStoreKeyPath.swift; sourceTree = "<group>"; };
B5CA2B111F81DBFE004B1936 /* DynamicKeyPath.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DynamicKeyPath.swift; sourceTree = "<group>"; };
B5D1E22B19FA9FBC003B2874 /* CoreStoreError.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = CoreStoreError.swift; sourceTree = "<group>"; };
B5D2D5A91F7558CB00A4DE67 /* .cocoapods.yml */ = {isa = PBXFileReference; lastKnownFileType = text; path = .cocoapods.yml; sourceTree = SOURCE_ROOT; };
B5D339B31E925C2B00C880DE /* DynamicModelTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = DynamicModelTests.swift; sourceTree = "<group>"; };
@@ -1323,7 +1323,7 @@
children = (
B5A1DAC71F111BFA003CF369 /* KeyPath+Querying.swift */,
B5D339EB1E9495E500C880DE /* CoreStoreObject+Querying.swift */,
B5CA2B111F81DBFE004B1936 /* AnyCoreStoreKeyPath.swift */,
B5CA2B111F81DBFE004B1936 /* DynamicKeyPath.swift */,
);
name = "KeyPath Utilities";
sourceTree = "<group>";
@@ -2004,7 +2004,7 @@
B549F65E1E569C7400FBAB2D /* QueryableAttributeType.swift in Sources */,
B5E84F211AFF84860064E85B /* CoreStore+Observing.swift in Sources */,
B559CD431CAA8B6300E4D58B /* CSSetupResult.swift in Sources */,
B5CA2B121F81DBFE004B1936 /* AnyCoreStoreKeyPath.swift in Sources */,
B5CA2B121F81DBFE004B1936 /* DynamicKeyPath.swift in Sources */,
B5A991EC1E9DC2CE0091A2E3 /* VersionLock.swift in Sources */,
B5FE4DA71C84FB4400FA6A91 /* InMemoryStore.swift in Sources */,
B52F743D1E9B8724005F3DAC /* DynamicSchema.swift in Sources */,
@@ -2200,7 +2200,7 @@
B549F65F1E569C7400FBAB2D /* QueryableAttributeType.swift in Sources */,
B559CD451CAA8B6300E4D58B /* CSSetupResult.swift in Sources */,
82BA18B81C4BBD4200A0916E /* TypeErasedClauses.swift in Sources */,
B5CA2B131F81DBFE004B1936 /* AnyCoreStoreKeyPath.swift in Sources */,
B5CA2B131F81DBFE004B1936 /* DynamicKeyPath.swift in Sources */,
B5A991ED1E9DC2CE0091A2E3 /* VersionLock.swift in Sources */,
B5ECDBEE1CA6BF2000C7F112 /* CSFrom.swift in Sources */,
B52F743E1E9B8724005F3DAC /* DynamicSchema.swift in Sources */,
@@ -2396,7 +2396,7 @@
B549F6611E569C7400FBAB2D /* QueryableAttributeType.swift in Sources */,
B52DD19B1BE1F92800949AFE /* CoreStoreLogger.swift in Sources */,
B52DD1991BE1F92800949AFE /* DefaultLogger.swift in Sources */,
B5CA2B151F81DBFF004B1936 /* AnyCoreStoreKeyPath.swift in Sources */,
B5CA2B151F81DBFF004B1936 /* DynamicKeyPath.swift in Sources */,
B5A991EF1E9DC2CE0091A2E3 /* VersionLock.swift in Sources */,
B5220E201D130813009BC71E /* CSObjectMonitor.swift in Sources */,
B52F74401E9B8724005F3DAC /* DynamicSchema.swift in Sources */,
@@ -2592,7 +2592,7 @@
B549F6601E569C7400FBAB2D /* QueryableAttributeType.swift in Sources */,
B559CD461CAA8B6300E4D58B /* CSSetupResult.swift in Sources */,
B56321A61BD65216006C9394 /* MigrationType.swift in Sources */,
B5CA2B141F81DBFE004B1936 /* AnyCoreStoreKeyPath.swift in Sources */,
B5CA2B141F81DBFE004B1936 /* DynamicKeyPath.swift in Sources */,
B5A991EE1E9DC2CE0091A2E3 /* VersionLock.swift in Sources */,
B5ECDBEF1CA6BF2000C7F112 /* CSFrom.swift in Sources */,
B52F743F1E9B8724005F3DAC /* DynamicSchema.swift in Sources */,

View File

@@ -101,8 +101,8 @@ class Person: CoreStoreObject {
static func keyPathsAffectingDisplayName() -> Set<String> {
return [
self.keyPath({ $0.title }),
self.keyPath({ $0.name })
String(keyPath: \Person.title),
String(keyPath: \Person.name)
]
}
}
@@ -112,7 +112,8 @@ class Person: CoreStoreObject {
class DynamicModelTests: BaseTestDataTestCase {
func testDynamicModels_CanBeDeclaredCorrectly() {
@objc
dynamic func test_ThatDynamicModels_CanBeDeclaredCorrectly() {
let dataStack = DataStack(
CoreStoreSchema(
@@ -131,13 +132,13 @@ class DynamicModelTests: BaseTestDataTestCase {
)
self.prepareStack(dataStack, configurations: [nil]) { (stack) in
let k1 = Animal.keyPath({ $0.species })
let k1 = String(keyPath: \Animal.species)
XCTAssertEqual(k1, "species")
let k2 = Dog.keyPath({ $0.species })
let k2 = String(keyPath: \Dog.species)
XCTAssertEqual(k2, "species")
let k3 = Dog.keyPath({ $0.nickname })
let k3 = String(keyPath: \Dog.nickname)
XCTAssertEqual(k3, "nickname")
let updateDone = self.expectation(description: "update-done")
@@ -273,6 +274,13 @@ class DynamicModelTests: BaseTestDataTestCase {
}
}
@objc
dynamic func test_ThatDynamicModelKeyPaths_CanBeCreated() {
XCTAssertEqual(String(keyPath: \Animal.species), "species")
XCTAssertEqual(String(keyPath: \Dog.species), "species")
}
@nonobjc
func prepareStack(_ dataStack: DataStack, configurations: [ModelConfiguration] = [nil], _ closure: (_ dataStack: DataStack) -> Void) {

View File

@@ -52,6 +52,12 @@ private func XCTAssertAllEqual<D>(_ whereClauses: [Where<D>]) {
final class WhereTests: XCTestCase {
@objc
dynamic func test_ThatDynamicModelKeyPaths_CanBeCreated() {
XCTAssertEqual(String(keyPath: \TestEntity1.testEntityID), "testEntityID")
}
@objc
dynamic func test_ThatWhereClauses_ConfigureCorrectly() {

216
README.md
View File

@@ -27,27 +27,26 @@ CoreStore is now part of the [Swift Source Compatibility projects](https://swift
## Why use CoreStore?
CoreStore is the answer to the [challenges](http://inessential.com/2010/02/26/on_switching_away_from_core_data) [of](http://bsktapp.com/blog/why-is-realm-great-and-why-are-we-not-using-it/) [using](https://www.quora.com/Why-would-you-use-Realm-over-Core-Data) [Core](http://sebastiandobrincu.com/blog/5-reasons-why-you-should-choose-realm-over-coredata) [Data](https://medium.com/the-way-north/ditching-core-data-865c1bb5564c#.a5h8ou6ri).
CoreStore was (and is) heavily shaped by real-world needs of developing data-dependent apps. It enforces safe and convenient Core Data usage while letting you take advantage of the industry's encouraged best practices.
### Features
- **Tight design around Swifts code elegance and type safety.** CoreStore fully utilizes Swift's community-driven language features.
- **Safer concurrency architecture.** CoreStore makes it hard to fall into common concurrency mistakes. The main `NSManagedObjectContext` is strictly read-only, while all updates are done through serial *transactions*. *(See [Saving and processing transactions](#saving-and-processing-transactions))*
- **Clean fetching and querying API.** Fetching objects is easy, but querying for raw aggregates (`min`, `max`, etc.) and raw property values is now just as convenient. *(See [Fetching and querying](#fetching-and-querying))*
- **Type-safe, easy to configure observers.** You don't have to deal with the burden of setting up `NSFetchedResultsController`s and KVO. As an added bonus, `ListMonitor`s and `ObjectMonitor`s can have multiple observers. This means you can have multiple view controllers efficiently share a single resource! *(See [Observing changes and notifications](#observing-changes-and-notifications))*
- **Efficient importing utilities.** Map your entities once with their corresponding import source (JSON for example), and importing from *transactions* becomes elegant. Uniquing is also done with an efficient find-and-replace algorithm. *(See [Importing data](#importing-data))*
- **Say goodbye to *.xcdatamodeld* files!** The new `CoreStoreObject` is *the* replacement to `NSManagedObject`. `CoreStoreObject` subclasses can declare type-safe properties all in Swift code, no need to maintain separate resource files for the models. As bonus, these special properties support custom types, and can be used to create type-safe keypaths and queries. *(See [Type-safe `CoreStoreObject`s](#type-safe-corestoreobjects))*
- **Progressive migrations.** No need to think how to migrate from all previous model versions to your latest model. Just tell the `DataStack` the sequence of version strings (`MigrationChain`s) and CoreStore will automatically use progressive migrations when needed. *(See [Migrations](#migrations))*
- **💎Tight design around Swifts code elegance and type safety.** CoreStore fully utilizes Swift's community-driven language features.
- **🚦Safer concurrency architecture.** CoreStore makes it hard to fall into common concurrency mistakes. The main `NSManagedObjectContext` is strictly read-only, while all updates are done through serial *transactions*. *(See [Saving and processing transactions](#saving-and-processing-transactions))*
- **🔍Clean fetching and querying API.** Fetching objects is easy, but querying for raw aggregates (`min`, `max`, etc.) and raw property values is now just as convenient. *(See [Fetching and querying](#fetching-and-querying))*
- **🔭Type-safe, easy to configure observers.** You don't have to deal with the burden of setting up `NSFetchedResultsController`s and KVO. As an added bonus, `ListMonitor`s and `ObjectMonitor`s can have multiple observers. This means you can have multiple view controllers efficiently share a single resource! *(See [Observing changes and notifications](#observing-changes-and-notifications))*
- **📥Efficient importing utilities.** Map your entities once with their corresponding import source (JSON for example), and importing from *transactions* becomes elegant. Uniquing is also done with an efficient find-and-replace algorithm. *(See [Importing data](#importing-data))*
- **🗑Say goodbye to *.xcdatamodeld* files!** The new `CoreStoreObject` is *the* replacement to `NSManagedObject`. `CoreStoreObject` subclasses can declare type-safe properties all in Swift code, no need to maintain separate resource files for the models. As bonus, these special properties support custom types, and can be used to create type-safe keypaths and queries. *(See [Type-safe `CoreStoreObject`s](#type-safe-corestoreobjects))*
- **🔗Progressive migrations.** No need to think how to migrate from all previous model versions to your latest model. Just tell the `DataStack` the sequence of version strings (`MigrationChain`s) and CoreStore will automatically use progressive migrations when needed. *(See [Migrations](#migrations))*
- **Easier custom migrations.** Say goodbye to *.xcmappingmodel* files; CoreStore can now infer entity mappings when possible, while still allowing an easy way to write custom mappings. *(See [Migrations](#migrations))*
- **Plug-in your own logging framework.** Although a default logger is built-in, all logging, asserting, and error reporting can be funneled to `CoreStoreLogger` protocol implementations. *(See [Logging and error reporting](#logging-and-error-reporting))*
- **Heavy support for multiple persistent stores per data stack.** CoreStore lets you manage separate stores in a single `DataStack`, just the way *.xcdatamodeld* configurations are designed to. CoreStore will also manage one stack by default, but you can create and manage as many as you need. *(See [Setting up](#setting-up))*
- **Free to name entities and their class names independently.** CoreStore gets around a restriction with other Core Data wrappers where the entity name should be the same as the `NSManagedObject` subclass name. CoreStore loads entity-to-class mappings from the managed object model file, so you can assign different names for the entities and their class names.
- **Full Documentation.** No magic here; all public classes, functions, properties, etc. have detailed *Apple Docs*. This *README* also introduces a lot of concepts and explains a lot of CoreStore's behavior.
- **Informative (and pretty) logs.** All CoreStore and Core Data-related types now have very informative and pretty print outputs! *(See [Logging and error reporting](#logging-and-error-reporting))*
- **Objective-C support!** Is your project transitioning from Objective-C to Swift but still can't quite fully convert some huge classes to Swift yet? CoreStore adjusts to the ever-increasing Swift adoption. While still written in pure Swift, all CoreStore types have their corresponding Objective-C-visible "bridging classes". *(See [Objective-C support](#objective-c-support))*
- **More extensive Unit Tests.** Extending CoreStore is safe without having to worry about breaking old behavior.
- **📝Plug-in your own logging framework.** Although a default logger is built-in, all logging, asserting, and error reporting can be funneled to `CoreStoreLogger` protocol implementations. *(See [Logging and error reporting](#logging-and-error-reporting))*
- **Heavy support for multiple persistent stores per data stack.** CoreStore lets you manage separate stores in a single `DataStack`, just the way *.xcdatamodeld* configurations are designed to. CoreStore will also manage one stack by default, but you can create and manage as many as you need. *(See [Setting up](#setting-up))*
- **🎯Free to name entities and their class names independently.** CoreStore gets around a restriction with other Core Data wrappers where the entity name should be the same as the `NSManagedObject` subclass name. CoreStore loads entity-to-class mappings from the managed object model file, so you can assign different names for the entities and their class names.
- **📙Full Documentation.** No magic here; all public classes, functions, properties, etc. have detailed *Apple Docs*. This *README* also introduces a lot of concepts and explains a lot of CoreStore's behavior.
- **Informative (and pretty) logs.** All CoreStore and Core Data-related types now have very informative and pretty print outputs! *(See [Logging and error reporting](#logging-and-error-reporting))*
- **🎗Objective-C support!** Is your project transitioning from Objective-C to Swift but still can't quite fully convert some huge classes to Swift yet? CoreStore adjusts to the ever-increasing Swift adoption. While still written in pure Swift, all CoreStore types have their corresponding Objective-C-visible "bridging classes". *(See [Objective-C support](#objective-c-support))*
- **🛡More extensive Unit Tests.** Extending CoreStore is safe without having to worry about breaking old behavior.
*Have ideas that may benefit other Core Data users? [Feature Request](https://github.com/JohnEstropia/CoreStore/issues)s are welcome!*
@@ -86,7 +85,7 @@ CoreStore was (and is) heavily shaped by real-world needs of developing data-dep
- [`Select<T>` clause](#selectt-clause)
- [`GroupBy` clause](#groupby-clause)
- [Logging and error reporting](#logging-and-error-reporting)
- [Observing changes and notifications](#observing-changes-and-notifications) (unavailable on macOS)
- [Observing changes and notifications](#observing-changes-and-notifications)
- [Observe a single object](#observe-a-single-object)
- [Observe a list of objects](#observe-a-list-of-objects)
- [Objective-C support](#objective-c-support)
@@ -226,8 +225,8 @@ let migrationProgress = dataStack.addStorage(
CoreStore.defaultStack = dataStack // pass the dataStack to CoreStore for easier access later on
```
(If you have never heard of "Configurations", you'll find them in your *.xcdatamodeld* file)
<img src="https://cloud.githubusercontent.com/assets/3029684/8333192/e52cfaac-1acc-11e5-9902-08724f9f1324.png" alt="xcode configurations screenshot" height=212 />
> 💡If you have never heard of "Configurations", you'll find them in your *.xcdatamodeld* file
> <img src="https://cloud.githubusercontent.com/assets/3029684/8333192/e52cfaac-1acc-11e5-9902-08724f9f1324.png" alt="xcode configurations screenshot" height=212 />
In our sample code above, note that you don't need to do the `CoreStore.defaultStack = dataStack` line. You can just as well hold a reference to the `DataStack` like below and call all its instance methods directly:
```swift
@@ -260,12 +259,14 @@ class MyViewController: UIViewController {
}
}
func methodToBeCalledLaterOn() {
let objects = CoreStore.fetchAll(From(MyEntity))
let objects = CoreStore.fetchAll(From<MyEntity>())
print(objects)
}
}
```
> 💡By default, CoreStore will initialize `NSManagedObject`s from *.xcdatamodeld* files, but you can create models completely from source code using `CoreStoreObject`s and `CoreStoreSchema`. To use this feature, refer to [Type-safe `CoreStoreObject`s](#type-safe-corestoreobjects).
Notice that in our previous examples, `addStorageAndWait(_:)` and `addStorage(_:completion:)` both accept either `InMemoryStore`, `SQLiteStore`, or `ICloudStore`. These implement the `StorageInterface` protocol.
### In-memory store
@@ -311,7 +312,7 @@ public protocol LocalStorage: StorageInterface {
If you have custom `NSIncrementalStore` or `NSAtomicStore` subclasses, you can implement this protocol and use it similarly to `SQLiteStore`.
### iCloud Store
> **Important:** The iCloud Store is currently in beta. Please use with caution. If you have any concerns please do send me a message on [Twitter](https://twitter.com/JohnEstropia) or on the [CoreStore Slack Team](http://swift-corestore-slack.herokuapp.com/)
> ⚠️**Important:** The iCloud Store is being deprecated. Please use with caution. If you have any concerns please do send me a message on [Twitter](https://twitter.com/JohnEstropia) or on the [CoreStore Slack Team](http://swift-corestore-slack.herokuapp.com/)
As a counterpart to `LocalStorage`, the `CloudStorage` protocol abstracts stores managed in the cloud. CoreStore currently provides the concrete class `ICloudStore`. Unlike `InMemoryStore` and `SQLiteStore` though, the `ICloudStore`'s initializer may return `nil` if the iCloud container could not be located or if iCloud is not available on the device:
```swift
@@ -550,14 +551,14 @@ This closure is executed on the main thread so UIKit and AppKit calls can be don
### Progressive migrations
By default, CoreStore uses Core Data's default automatic migration mechanism. In other words, CoreStore will try to migrate the existing persistent store to the *.xcdatamodeld* file's current model version. If no mapping model is found from the store's version to the data model's version, CoreStore gives up and reports an error.
By default, CoreStore uses Core Data's default automatic migration mechanism. In other words, CoreStore will try to migrate the existing persistent store until it matches the `SchemaHistory`'s `currentModelVersion`. If no mapping model path is found from the store's version to the data model's version, CoreStore gives up and reports an error.
The `DataStack` lets you specify hints on how to break a migration into several sub-migrations using a `MigrationChain`. This is typically passed to the `DataStack` initializer and will be applied to all stores added to the `DataStack` with `addSQLiteStore(...)` and its variants:
```swift
let dataStack = DataStack(migrationChain:
["MyAppModel", "MyAppModelV2", "MyAppModelV3", "MyAppModelV4"])
```
The most common usage is to pass in the *.xcdatamodeld* version names in increasing order as above.
The most common usage is to pass in the model version (*.xcdatamodeld* version names for `NSManagedObject`s, or the `modelName` for `CoreStoreSchema`s) in increasing order as above.
For more complex, non-linear migration paths, you can also pass in a version tree that maps the key-values to the source-destination versions:
```swift
@@ -582,7 +583,7 @@ The `MigrationChain` is validated when passed to the `DataStack` and unless it i
- a version appears twice as a key in a dictionary literal
- a loop is found in any of the paths
One important thing to remember is that **if a `MigrationChain` is specified, the *.xcdatamodeld*'s "Current Version" will be bypassed** and the `MigrationChain`'s leafmost version will be the `DataStack`'s base model version.
> ⚠️**Important: If a `MigrationChain` is specified, the *.xcdatamodeld*'s "Current Version" will be bypassed** and the `MigrationChain`'s leafmost version will be the `DataStack`'s base model version.
### Forecasting migrations
@@ -717,7 +718,7 @@ dataStack.perform(
}
)
```
Never use `try?` or `try!` on a `transaction.cancel()` call. Always use `try`. Using `try?` will swallow the cancellation and the transaction will proceed to save as normal. Using `try!` will crash the app as `transaction.cancel()` will *always* throw an error.
> ⚠️**Important:** Never use `try?` or `try!` on a `transaction.cancel()` call. Always use `try`. Using `try?` will swallow the cancellation and the transaction will proceed to save as normal. Using `try!` will crash the app as `transaction.cancel()` will *always* throw an error.
The examples above use `perform(asynchronous:...)`, but there are actually 3 types of transactions at your disposal: *asynchronous*, *synchronous*, and *unsafe*.
@@ -754,6 +755,9 @@ CoreStore.perform(
}
)
```
> Be careful when returning `NSManagedObject`s or `CoreStoreObject`s from the transaction closure. Those instances are for the transaction's use only. See [Passing objects safely](#passing-objects-safely).
Transactions created from `perform(asynchronous:...)` are instances of `AsynchronousDataTransaction`.
#### Synchronous transactions
@@ -770,6 +774,21 @@ let hasChanges = CoreStore.perform(
Since `perform(synchronous:...)` technically blocks two queues (the caller's queue and the transaction's background queue), it is considered less safe as it's more prone to deadlock. Take special care that the closure does not block on any other external queues.
By default, `perform(synchronous:...)` will wait for observers such as `ListMonitor`s to be notified before the method returns. This may cause deadlocks, especially if you are calling this from the main thread. To reduce this risk, you may try to set the `waitForAllObservers:` parameter to `false`. Doing so tells the `SynchronousDataTransaction` to block only until it completes saving. It will not wait for other context's to receive those changes. This reduces deadlock risk but may have surprising side-effects:
```swift
CoreStore.perform(
synchronous: { (transaction) in
let person = transaction.create(Into<Person>())
person.name = "John"
},
waitForAllObservers: false
)
let newPerson = CoreStore.fetchOne(From<Person>.where(\.name == "John"))
// newPerson may be nil!
// The DataStack may have not yet received the update notification.
```
Due to this complicated nature of synchronous transactions, if your app has very heavy transaction throughput it is highly recommended to use [asynchronous transactions](#asynchronous-transactions) instead.
#### Unsafe transactions
are special in that they do not enclose updates within a closure:
```swift
@@ -802,8 +821,8 @@ let person = transaction.create(Into<MyPersonEntity>())
While the syntax is straightforward, CoreStore does not just naively insert a new object. This single line does the following:
- Checks that the entity type exists in any of the transaction's parent persistent store
- If the entity belongs to only one persistent store, a new object is inserted into that store and returned from `create(...)`
- If the entity does not belong to any store, an assert will be triggered. **This is a programmer error and should never occur in production code.**
- If the entity belongs to multiple stores, an assert will be triggered. **This is also a programmer error and should never occur in production code.** Normally, with Core Data you can insert an object in this state but saving the `NSManagedObjectContext` will always fail. CoreStore checks this for you at creation time when it makes sense (not during save).
- If the entity does not belong to any store, an assertion failure will be raised. **This is a programmer error and should never occur in production code.**
- If the entity belongs to multiple stores, an assertion failure will be raised. **This is also a programmer error and should never occur in production code.** Normally, with Core Data you can insert an object in this state but saving the `NSManagedObjectContext` will always fail. CoreStore checks this for you at creation time when it makes sense (not during save).
If the entity exists in multiple configurations, you need to provide the configuration name for the destination persistent store:
@@ -833,8 +852,8 @@ To update an existing object, fetch the object's instance from the transaction:
CoreStore.perform(
asynchronous: { (transaction) -> Void in
let person = transaction.fetchOne(
From<MyPersonEntity>(),
Where("name", isEqualTo: "Jane Smith")
From<MyPersonEntity>()
.where(\.name == "Jane Smith")
)
person.age = person.age + 1
},
@@ -905,8 +924,8 @@ If you do not have references yet to the objects to be deleted, transactions hav
CoreStore.perform(
asynchronous: { (transaction) -> Void in
transaction.deleteAll(
From<MyPersonEntity>(),
Where("age > 30")
From<MyPersonEntity>()
.where(\.age > 30)
)
},
completion: { _ in }
@@ -953,8 +972,8 @@ var peopleIDs: [NSManagedObjectID] = // ...
CoreStore.perform(
asynchronous: { (transaction) -> Void in
let jane = transaction.fetchOne(
From<MyPersonEntity>(),
Where("name", isEqualTo: "Jane Smith")
From<MyPersonEntity>()
.where(\.name == "Jane Smith")
)
jane.friends = NSSet(array: transaction.fetchExisting(peopleIDs)!)
// ...
@@ -999,7 +1018,7 @@ typealias ImportSource = [String: Any]
```swift
typealias ImportSource = NSData
```
You can even use external types from popular 3rd-party JSON libraries ([SwiftyJSON](https://github.com/SwiftyJSON/SwiftyJSON)'s `JSON` type is a personal favorite), or just simple tuples or primitives.
You can even use external types from popular 3rd-party JSON libraries, or just simple tuples or primitives.
#### `ImportableObject`
`ImportableObject` is a very simple protocol:
@@ -1197,11 +1216,11 @@ The `Where` clause is CoreStore's `NSPredicate` wrapper. It specifies the search
```swift
var people = CoreStore.fetchAll(
From<MyPersonEntity>(),
Where("%K > %d", "age", 30) // string format initializer
Where<MyPersonEntity>("%K > %d", "age", 30) // string format initializer
)
people = CoreStore.fetchAll(
From<MyPersonEntity>(),
Where(true) // boolean initializer
Where<MyPersonEntity>(true) // boolean initializer
)
```
If you do have an existing `NSPredicate` instance already, you can pass that to `Where` as well:
@@ -1209,14 +1228,21 @@ If you do have an existing `NSPredicate` instance already, you can pass that to
let predicate = NSPredicate(...)
var people = CoreStore.fetchAll(
From<MyPersonEntity>(),
Where(predicate) // predicate initializer
Where<MyPersonEntity>(predicate) // predicate initializer
)
```
Starting CoreStore 5.0, `Where` clauses became more type-safe and are now generic types. To avoid verbose repetition of the generic object type, fetch methods now support **Fetch Chain builders**. We can also use Swift's Smart KeyPaths as the `Where` clause expression:
```swift
var people = CoreStore.fetchAll(
From<MyPersonEntity>()
.where(\.age > 30) // Type-safe!
)
```
`Where` clauses also implement the `&&`, `||`, and `!` logic operators, so you can provide logical conditions without writing too much `AND`, `OR`, and `NOT` strings:
```swift
var people = CoreStore.fetchAll(
From<MyPersonEntity>(),
Where("age > %d", 30) && Where("gender == %@", "M")
From<MyPersonEntity>()
.where(\.age > 30 && \.gender == "M")
)
```
If you do not provide a `Where` clause, all objects that belong to the specified `From` will be returned.
@@ -1227,16 +1253,23 @@ The `OrderBy` clause is CoreStore's `NSSortDescriptor` wrapper. Use it to specif
```swift
var mostValuablePeople = CoreStore.fetchAll(
From<MyPersonEntity>(),
OrderBy(.descending("rating"), .ascending("surname"))
OrderBy<MyPersonEntity>(.descending("rating"), .ascending("surname"))
)
```
As seen above, `OrderBy` accepts a list of `SortKey` enumeration values, which can be either `.ascending` or `.descending`.
As with `Where` clauses, CoreStore 5.0 turned `OrderBy` clauses into generic types. To avoid verbose repetition of the generic object type, fetch methods now support **Fetch Chain builders**. We can also use Swift's Smart KeyPaths as the `OrderBy` clause expression:
```swift
var people = CoreStore.fetchAll(
From<MyPersonEntity>()
.orderBy(.descending(\.rating), .ascending(\.surname)) // Type-safe!
)
```
You can use the `+` and `+=` operator to append `OrderBy`s together. This is useful when sorting conditionally:
```swift
var orderBy = OrderBy(.descending("rating"))
var orderBy = OrderBy<MyPersonEntity>(.descending(\.rating))
if sortFromYoungest {
orderBy += OrderBy(.ascending("age"))
orderBy += OrderBy(.ascending(\.age))
}
var mostValuablePeople = CoreStore.fetchAll(
From<MyPersonEntity>(),
@@ -1250,8 +1283,8 @@ The `Tweak` clause lets you, uh, *tweak* the fetch (or query). `Tweak` exposes t
```swift
var people = CoreStore.fetchAll(
From<MyPersonEntity>(),
Where("age > %d", 30),
OrderBy(.ascending("surname")),
Where<MyPersonEntity>("age > %d", 30),
OrderBy<MyPersonEntity>(.ascending("surname")),
Tweak { (fetchRequest) -> Void in
fetchRequest.includesPendingChanges = false
fetchRequest.returnsObjectsAsFaults = false
@@ -1259,6 +1292,19 @@ var people = CoreStore.fetchAll(
}
)
```
`Tweak` also supports **Fetch Chain builders**:
```swift
var people = CoreStore.fetchAll(
From<MyPersonEntity>(),
.where(\.age > 30)
.orderBy(.ascending(\.surname))
.tweak {
$0.includesPendingChanges = false
$0.returnsObjectsAsFaults = false
$0.includesSubentities = false
}
)
```
The clauses are evaluated the order they appear in the fetch/query, so you typically need to set `Tweak` as the last clause.
`Tweak`'s closure is executed only just before the fetch occurs, so make sure that any values captured by the closure is not prone to race conditions.
@@ -1294,6 +1340,14 @@ let allAges = CoreStore.queryAttributes(
Select("age")
)
```
Starting CoreStore 5.0, query methods now support **Query Chain builders**. We can also use Swift's Smart KeyPaths to use in the expressions:
```swift
let johnsAge = CoreStore.queryValue(
From<MyPersonEntity>()
.select(\.age) // binds the result to Int
.where(\.name == "John Smith")
)
```
If you only need a value for a particular attribute, you can just specify the key name (like we did with `Select<Int>("age")`), but several aggregate functions can also be used as parameter to `Select`:
- `.average(...)`
@@ -1380,6 +1434,14 @@ let personJSON = CoreStore.queryAttributes(
GroupBy("age")
)
```
Starting CoreStore 5.0, `GroupBy` clauses are now also generic types and now support **Query Chain builders**. We can also use Swift's Smart KeyPaths to use in the expressions:
```swift
let personJSON = CoreStore.queryAttributes(
From<MyPersonEntity>()
.select(.attribute(\.age), .count(\.age, as: "count"))
.groupBy(\.age)
)
```
this returns dictionaries that shows the count for each `"age"`:
```swift
[
@@ -1428,7 +1490,9 @@ A couple of examples, `ListMonitor`:
These are all implemented with `CustomDebugStringConvertible.debugDescription`, so they work with lldb's `po` command as well.
## Observing changes and notifications (unavailable on macOS)
## Observing changes and notifications
> (unavailable on macOS versions below 10.12)
CoreStore provides type-safe wrappers for observing managed objects:
- `ObjectMonitor`: use to monitor changes to a single `NSManagedObject` or `CoreStoreObject` instance (instead of Key-Value Observing)
@@ -1503,12 +1567,10 @@ Including `ListObserver`, there are 3 observer protocols you can implement depen
We then need to create a `ListMonitor` instance and register our `ListObserver` as an observer:
```swift
self.monitor = CoreStore.monitorList(
From<MyPersonEntity>(),
Where("age > 30"),
OrderBy(.ascending("name")),
Tweak { (fetchRequest) -> Void in
fetchRequest.fetchBatchSize = 20
}
From<MyPersonEntity>()
.where(\.age > 30)
.orderBy(.ascending(\.name))
.tweak { $0.fetchBatchSize = 20 }
)
self.monitor.addObserver(self)
```
@@ -1524,13 +1586,11 @@ let firstPerson = self.monitor[0]
If the list needs to be grouped into sections, create the `ListMonitor` instance with the `monitorSectionedList(...)` method and a `SectionBy` clause:
```swift
self.monitor = CoreStore.monitorSectionedList(
From<MyPersonEntity>(),
SectionBy("age"),
Where("gender", isEqualTo: "M"),
OrderBy(.ascending("age"), .ascending("name")),
Tweak { (fetchRequest) -> Void in
fetchRequest.fetchBatchSize = 20
}
From<MyPersonEntity>()
.sectionBy(\.age)
.where(\.gender == "M")
.orderBy(.ascending(\.age), .ascending(\.name))
.tweak { $0.fetchBatchSize = 20 }
)
```
A list controller created this way will group the objects by the attribute key indicated by the `SectionBy` clause. One more thing to remember is that the `OrderBy` clause should sort the list in such a way that the `SectionBy` attribute would be sorted together (a requirement shared by `NSFetchedResultsController`.)
@@ -1538,11 +1598,11 @@ A list controller created this way will group the objects by the attribute key i
The `SectionBy` clause can also be passed a closure to transform the section name into a displayable string:
```swift
self.monitor = CoreStore.monitorSectionedList(
From<MyPersonEntity>(),
SectionBy("age") { (sectionName) -> String? in
"\(sectionName) years old"
},
OrderBy(.ascending("age"), .ascending("name"))
From<MyPersonEntity>()
.sectionBy(\.age) { (sectionName) -> String? in
"\(sectionName) years old"
}
.orderBy(.ascending(\.age), .ascending(\.name))
)
```
This is useful when implementing a `UITableViewDelegate`'s section header:
@@ -1562,9 +1622,9 @@ let person2 = self.monitor[1, 2]
```
## Objective-C support
CoreStore 2.0 was a big move to address the large number of apps starting to convert from Objective-C to Swift. The basic problem is this: The cost of converting all code base to Swift is very big, so most apps are forced to do undergo a *transitional* ObjC-Swift hybrid phase. This used to mean that these apps could not use the Swifty-est libraries out there yet, or that they may have to write their own bridging methods just to make new code usable in their old Objective-C code.
> Objective-C support is planned to be deprecated in a future CoreStore version.
With 2.0, all CoreStore types are still written in pure Swift, but they now have Objective-C "bridging classes" that are visible to Objective-C code. To show a couple of usage examples:
All CoreStore types are still written in pure Swift, but most core types have Objective-C "bridging classes" that are visible to Objective-C code. To show a couple of usage examples:
<table>
<tr><th>Swift</th><th>Objective-C</th></tr>
@@ -1612,7 +1672,7 @@ All of these `CS`-prefixed bridging classes have very similar usage to the exist
For example, you may have a new, modern Swift class that holds a `ListMonitor`:
```swift
class MyViewController: UIViewController {
let monitor = CoreStore.monitorList(From(MyEntity), ...)
let monitor = CoreStore.monitorList(From<MyEntity>(), ...)
// ...
}
```
@@ -1626,7 +1686,7 @@ Now let's say you have a legacy Objective-C class that previously uses `NSFetche
When you need to instantiate this class from Swift, you just call `bridgeToObjectiveC`:
```swift
class MyViewController: UIViewController {
let monitor = CoreStore.monitorList(From(MyEntity), ...)
let monitor = CoreStore.monitorList(From<MyEntity>(), ...)
func showOldController() {
let controller = MYOldViewController(monitor: self.monitor.bridgeToObjectiveC)
self.presentViewController(controller, animated: true, completion: nil)
@@ -1688,14 +1748,14 @@ The property names to be saved to Core Data is specified as the `keyPath` argume
```swift
class Person: CoreStoreObject {
private let _name = Value.Required<String>("name", initial: "")
// ...
// note property name is independent of the storage key name
}
```
Here we added an underscore to the property name and made it `private`, but the underlying key-path `"name"` was unchanged so our model will not trigger a data migration.
> **Important:** As a rule, CoreStore can only process *stored properties*. Computed, `static`, `weak`, or `lazy` properties will not be added to the store. It is also strictly advised use `let` instead of `var` to declare these properties, as any changes to the schema after declaration is not allowed.
> ⚠️**Important:** As a rule, CoreStore can only process *stored properties*. Computed, `static`, `weak`, or `lazy` properties will be ignored and will not be added to the store. It is also strictly advised use `let` instead of `var` to declare these properties, as any changes to the property value will break the schema.
Also note how `Relationship`s are linked statically with the `inverse:` argument. All relationships are required to have an "inverse" relationship. Unfortunately, due to Swift compiler limitation we can only declare the `inverse:` on one end of the relationship-pair.
Also note how `Relationship`s are linked statically with the `inverse:` argument. **All relationships are required to have an "inverse" relationship**. Unfortunately, due to Swift compiler limitation we can only declare the `inverse:` on one end of the relationship-pair.
To tell the `DataStack` about these types, add all `CoreStoreObject`s' entities to a `CoreStoreSchema`:
```swift
@@ -1737,9 +1797,9 @@ let keyPath: String = Dog.keyPath { $0.nickname }
as well as `Where` and `OrderBy` clauses
```swift
let puppies = CoreStore.fetchAll(
From<Dog>(),
Dog.where { $0.age < 1 },
Dog.ascending { $0.age }
From<Dog>()
.where(\.age < 1)
.orderBy(.ascending(\.age))
)
```
@@ -1749,7 +1809,7 @@ All CoreStore APIs that are usable with `NSManagedObject`s are also available fo
While it is convenient to be able to declare entities only in code, it is worrying that we might accidentally change the `CoreStoreObject`'s properties and break our users' model version history. For this, the `CoreStoreSchema` allows us to "lock" our properties to a particular configuration. Any changes to that `VersionLock` will raise an assertion failure during the `CoreStoreSchema` initialization, so you can then look for the commit which changed the `VersionLock` hash.
To use `VersionLock`s, create the `CoreStoreSchema`, run the app, and look for this particular log message that is automatically printed to the console:
To use `VersionLock`s, create the `CoreStoreSchema`, run the app, and look for a similar log message that is automatically printed to the console:
<img width="700" alt="VersionLock" src="https://cloud.githubusercontent.com/assets/3029684/26525632/757f1bd0-4398-11e7-9795-4132a2df0538.png" />
@@ -1782,7 +1842,7 @@ Once the version lock is set, any changes in the properties or to the model will
# Installation
- Requires:
- iOS 8 SDK and above
- Swift 3.1 (Xcode 8.3.2+)
- Swift 4 (Xcode 9+)
- Dependencies:
- *None*
- Other notes:
@@ -1791,7 +1851,7 @@ Once the version lock is set, any changes in the properties or to the model will
### Install with CocoaPods
In your `Podfile`, add
```
pod 'CoreStore', '~> 4.0'
pod 'CoreStore', '~> 5.0'
```
and run
```
@@ -1802,7 +1862,7 @@ This installs CoreStore as a framework. Declare `import CoreStore` in your swift
### Install with Carthage
In your `Cartfile`, add
```
github "JohnEstropia/CoreStore" >= 4.0.0
github "JohnEstropia/CoreStore" >= 5.0.0
```
and run
```
@@ -1833,9 +1893,7 @@ For the full Changelog, refer to the [Releases](https://github.com/JohnEstropia/
# Contact
Questions? Suggestions?
Reach me on Twitter [@JohnEstropia](https://twitter.com/JohnEstropia)
You can reach me on Twitter [@JohnEstropia](https://twitter.com/JohnEstropia)
or join our Slack team at [swift-corestore.slack.com](http://swift-corestore-slack.herokuapp.com/)

View File

@@ -1,100 +0,0 @@
//
// AnyCoreStoreKeyPath.swift
// CoreStore
//
// Copyright © 2017 John Rommel Estropia
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
//
import Foundation
// MARK: - AnyCoreStoreKeyPath
public protocol AnyCoreStoreKeyPath {
var cs_keyPathString: String { get }
}
// SE-0143 is not implemented: https://github.com/apple/swift-evolution/blob/master/proposals/0143-conditional-conformances.md
//extension KeyPath: AnyCoreStoreKeyPath where Root: NSManagedObject, Value: ImportableAttributeType {
//
// public var cs_keyPathString: String {
//
// return self._kvcKeyPathString!
// }
//}
extension ValueContainer.Required: AnyCoreStoreKeyPath {
public var cs_keyPathString: String {
return self.keyPath
}
}
extension ValueContainer.Optional: AnyCoreStoreKeyPath {
public var cs_keyPathString: String {
return self.keyPath
}
}
extension TransformableContainer.Required: AnyCoreStoreKeyPath {
public var cs_keyPathString: String {
return self.keyPath
}
}
extension TransformableContainer.Optional: AnyCoreStoreKeyPath {
public var cs_keyPathString: String {
return self.keyPath
}
}
extension RelationshipContainer.ToOne: AnyCoreStoreKeyPath {
public var cs_keyPathString: String {
return self.keyPath
}
}
extension RelationshipContainer.ToManyOrdered: AnyCoreStoreKeyPath {
public var cs_keyPathString: String {
return self.keyPath
}
}
extension RelationshipContainer.ToManyUnordered: AnyCoreStoreKeyPath {
public var cs_keyPathString: String {
return self.keyPath
}
}

View File

@@ -27,67 +27,6 @@ import CoreData
import Foundation
// MARK: - DynamicObject
public extension DynamicObject where Self: CoreStoreObject {
/**
Extracts the keyPath string from a `CoreStoreObject.Value` property.
```
let keyPath: String = Person.keyPath { $0.nickname }
```
*/
public static func keyPath<O, V>(_ attribute: (Self) -> ValueContainer<O>.Required<V>) -> String {
return attribute(self.meta).keyPath
}
/**
Extracts the keyPath string from a `CoreStoreObject.Value` property.
```
let keyPath: String = Person.keyPath { $0.nickname }
```
*/
public static func keyPath<O, V>(_ attribute: (Self) -> ValueContainer<O>.Optional<V>) -> String {
return attribute(self.meta).keyPath
}
/**
Extracts the keyPath string from a `CoreStoreObject.Relationship` property.
```
let keyPath: String = Person.keyPath { $0.pets }
```
*/
public static func keyPath<O, D>(_ relationship: (Self) -> RelationshipContainer<O>.ToOne<D>) -> String {
return relationship(self.meta).keyPath
}
/**
Extracts the keyPath string from a `CoreStoreObject.Relationship` property.
```
let keyPath: String = Person.keyPath { $0.pets }
```
*/
public static func keyPath<O, D>(_ relationship: (Self) -> RelationshipContainer<O>.ToManyOrdered<D>) -> String {
return relationship(self.meta).keyPath
}
/**
Extracts the keyPath string from a `CoreStoreObject.Relationship` property.
```
let keyPath: String = Person.keyPath { $0.pets }
```
*/
public static func keyPath<O, D>(_ relationship: (Self) -> RelationshipContainer<O>.ToManyUnordered<D>) -> String {
return relationship(self.meta).keyPath
}
}
// MARK: - ValueContainer.Required
public extension ValueContainer.Required {
@@ -98,7 +37,6 @@ public extension ValueContainer.Required {
let person = CoreStore.fetchOne(From<Person>().where({ $0.nickname == "John" }))
```
*/
@inline(__always)
public static func == (_ attribute: ValueContainer<O>.Required<V>, _ value: V) -> Where<O> {
return Where(attribute.keyPath, isEqualTo: value)
@@ -110,7 +48,6 @@ public extension ValueContainer.Required {
let person = CoreStore.fetchOne(From<Person>().where({ $0.nickname != "John" }))
```
*/
@inline(__always)
public static func != (_ attribute: ValueContainer<O>.Required<V>, _ value: V) -> Where<O> {
return !Where(attribute.keyPath, isEqualTo: value)
@@ -122,7 +59,6 @@ public extension ValueContainer.Required {
let person = CoreStore.fetchOne(From<Person>().where({ $0.age < 20 }))
```
*/
@inline(__always)
public static func < (_ attribute: ValueContainer<O>.Required<V>, _ value: V) -> Where<O> {
return Where("%K < %@", attribute.keyPath, value)
@@ -134,7 +70,6 @@ public extension ValueContainer.Required {
let person = CoreStore.fetchOne(From<Person>().where({ $0.age > 20 }))
```
*/
@inline(__always)
public static func > (_ attribute: ValueContainer<O>.Required<V>, _ value: V) -> Where<O> {
return Where("%K > %@", attribute.keyPath, value)
@@ -146,7 +81,6 @@ public extension ValueContainer.Required {
let person = CoreStore.fetchOne(From<Person>().where({ $0.age <= 20 }))
```
*/
@inline(__always)
public static func <= (_ attribute: ValueContainer<O>.Required<V>, _ value: V) -> Where<O> {
return Where("%K <= %@", attribute.keyPath, value)
@@ -158,7 +92,6 @@ public extension ValueContainer.Required {
let person = CoreStore.fetchOne(From<Person>().where({ $0.age >= 20 }))
```
*/
@inline(__always)
public static func >= (_ attribute: ValueContainer<O>.Required<V>, _ value: V) -> Where<O> {
return Where("%K >= %@", attribute.keyPath, value)
@@ -170,7 +103,6 @@ public extension ValueContainer.Required {
let dog = CoreStore.fetchOne(From<Dog>().where({ ["Pluto", "Snoopy", "Scooby"] ~= $0.nickname }))
```
*/
@inline(__always)
public static func ~= <S: Sequence>(_ sequence: S, _ attribute: ValueContainer<O>.Required<V>) -> Where<O> where S.Iterator.Element == V {
return Where(attribute.keyPath, isMemberOf: sequence)
@@ -188,7 +120,6 @@ public extension ValueContainer.Optional {
let person = CoreStore.fetchOne(From<Person>().where({ $0.nickname == "John" }))
```
*/
@inline(__always)
public static func == (_ attribute: ValueContainer<O>.Optional<V>, _ value: V?) -> Where<O> {
return Where(attribute.keyPath, isEqualTo: value)
@@ -200,7 +131,6 @@ public extension ValueContainer.Optional {
let person = CoreStore.fetchOne(From<Person>().where({ $0.nickname != "John" }))
```
*/
@inline(__always)
public static func != (_ attribute: ValueContainer<O>.Optional<V>, _ value: V?) -> Where<O> {
return !Where(attribute.keyPath, isEqualTo: value)
@@ -212,7 +142,6 @@ public extension ValueContainer.Optional {
let person = CoreStore.fetchOne(From<Person>().where({ $0.age < 20 }))
```
*/
@inline(__always)
public static func < (_ attribute: ValueContainer<O>.Optional<V>, _ value: V?) -> Where<O> {
if let value = value {
@@ -231,7 +160,6 @@ public extension ValueContainer.Optional {
let person = CoreStore.fetchOne(From<Person>().where({ $0.age > 20 }))
```
*/
@inline(__always)
public static func > (_ attribute: ValueContainer<O>.Optional<V>, _ value: V?) -> Where<O> {
if let value = value {
@@ -250,7 +178,6 @@ public extension ValueContainer.Optional {
let person = CoreStore.fetchOne(From<Person>().where({ $0.age <= 20 }))
```
*/
@inline(__always)
public static func <= (_ attribute: ValueContainer<O>.Optional<V>, _ value: V?) -> Where<O> {
if let value = value {
@@ -269,7 +196,6 @@ public extension ValueContainer.Optional {
let person = CoreStore.fetchOne(From<Person>().where({ $0.age >= 20 }))
```
*/
@inline(__always)
public static func >= (_ attribute: ValueContainer<O>.Optional<V>, _ value: V?) -> Where<O> {
if let value = value {
@@ -288,7 +214,6 @@ public extension ValueContainer.Optional {
let dog = CoreStore.fetchOne(From<Dog>().where({ ["Pluto", "Snoopy", "Scooby"] ~= $0.nickname }))
```
*/
@inline(__always)
public static func ~= <S: Sequence>(_ sequence: S, _ attribute: ValueContainer<O>.Optional<V>) -> Where<O> where S.Iterator.Element == V {
return Where(attribute.keyPath, isMemberOf: sequence)
@@ -306,7 +231,6 @@ public extension RelationshipContainer.ToOne {
let dog = CoreStore.fetchOne(From<Dog>().where({ $0.master == me }))
```
*/
@inline(__always)
public static func == (_ relationship: RelationshipContainer<O>.ToOne<D>, _ object: D?) -> Where<O> {
return Where(relationship.keyPath, isEqualTo: object)
@@ -318,7 +242,6 @@ public extension RelationshipContainer.ToOne {
let dog = CoreStore.fetchOne(From<Dog>().where({ $0.master != me }))
```
*/
@inline(__always)
public static func != (_ relationship: RelationshipContainer<O>.ToOne<D>, _ object: D?) -> Where<O> {
return !Where(relationship.keyPath, isEqualTo: object)
@@ -330,7 +253,6 @@ public extension RelationshipContainer.ToOne {
let dog = CoreStore.fetchOne(From<Dog>().where({ [john, joe, bob] ~= $0.master }))
```
*/
@inline(__always)
public static func ~= <S: Sequence>(_ sequence: S, _ relationship: RelationshipContainer<O>.ToOne<D>) -> Where<O> where S.Iterator.Element == D {
return Where(relationship.keyPath, isMemberOf: sequence)
@@ -342,6 +264,36 @@ public extension RelationshipContainer.ToOne {
extension DynamicObject where Self: CoreStoreObject {
@available(*, deprecated, message: "Use the String(keyPath:) initializer and pass the KeyPath: String(keyPath: \\Person.name)")
public static func keyPath<O, V>(_ attribute: (Self) -> ValueContainer<O>.Required<V>) -> String {
return attribute(self.meta).keyPath
}
@available(*, deprecated, message: "Use the String(keyPath:) initializer and pass the KeyPath: String(keyPath: \\Person.name)")
public static func keyPath<O, V>(_ attribute: (Self) -> ValueContainer<O>.Optional<V>) -> String {
return attribute(self.meta).keyPath
}
@available(*, deprecated, message: "Use the String(keyPath:) initializer and pass the KeyPath: String(keyPath: \\Person.friend)")
public static func keyPath<O, D>(_ relationship: (Self) -> RelationshipContainer<O>.ToOne<D>) -> String {
return relationship(self.meta).keyPath
}
@available(*, deprecated, message: "Use the String(keyPath:) initializer and pass the KeyPath: String(keyPath: \\Person.friends)")
public static func keyPath<O, D>(_ relationship: (Self) -> RelationshipContainer<O>.ToManyOrdered<D>) -> String {
return relationship(self.meta).keyPath
}
@available(*, deprecated, message: "Use the String(keyPath:) initializer and pass the KeyPath: String(keyPath: \\Person.friends)")
public static func keyPath<O, D>(_ relationship: (Self) -> RelationshipContainer<O>.ToManyUnordered<D>) -> String {
return relationship(self.meta).keyPath
}
@available(*, deprecated, message: "Use the Where<DynamicObject>(_:) initializer that accepts the same closure argument")
public static func `where`(_ condition: (Self) -> Where<Self>) -> Where<Self> {

View File

@@ -0,0 +1,193 @@
//
// DynamicKeyPath.swift
// CoreStore
//
// Copyright © 2017 John Rommel Estropia
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
//
import Foundation
// MARK: - DynamicKeyPath
/**
Used only for utility methods.
*/
public protocol DynamicKeyPath {
/**
The DynamicObject type
*/
associatedtype ObjectType
/**
The Value type
*/
associatedtype ValueType
/**
The keyPath string
*/
var cs_keyPathString: String { get }
}
// MARK: - KeyPathString
public extension KeyPathString {
/**
Extracts the keyPath string from the property.
```
let keyPath = String(keyPath: \Person.nickname)
```
*/
public init<O: NSManagedObject, V>(keyPath: KeyPath<O, V>) {
self = keyPath.cs_keyPathString
}
/**
Extracts the keyPath string from the property.
```
let keyPath = String(keyPath: \Person.nickname)
```
*/
public init<O: CoreStoreObject, K: DynamicKeyPath>(keyPath: KeyPath<O, K>) {
self = O.meta[keyPath: keyPath].cs_keyPathString
}
}
// MARK: - KeyPath: DynamicKeyPath
// TODO: SE-0143 is not implemented: https://github.com/apple/swift-evolution/blob/master/proposals/0143-conditional-conformances.md
//extension KeyPath: DynamicKeyPath where Root: NSManagedObject, Value: ImportableAttributeType {
extension KeyPath: DynamicKeyPath {
public typealias ObjectType = Root
public typealias ValueType = Value
public var cs_keyPathString: String {
return self._kvcKeyPathString!
}
}
// MARK: - ValueContainer.Required: DynamicKeyPath
extension ValueContainer.Required: DynamicKeyPath {
public typealias ObjectType = O
public typealias ValueType = V
public var cs_keyPathString: String {
return self.keyPath
}
}
// MARK: - ValueContainer.Optional: DynamicKeyPath
extension ValueContainer.Optional: DynamicKeyPath {
public typealias ObjectType = O
public typealias ValueType = V
public var cs_keyPathString: String {
return self.keyPath
}
}
// MARK: - TransformableContainer.Required: DynamicKeyPath
extension TransformableContainer.Required: DynamicKeyPath {
public typealias ObjectType = O
public typealias ValueType = V
public var cs_keyPathString: String {
return self.keyPath
}
}
// MARK: - TransformableContainer.Optional: DynamicKeyPath
extension TransformableContainer.Optional: DynamicKeyPath {
public typealias ObjectType = O
public typealias ValueType = V
public var cs_keyPathString: String {
return self.keyPath
}
}
// MARK: - RelationshipContainer.ToOne: DynamicKeyPath
extension RelationshipContainer.ToOne: DynamicKeyPath {
public typealias ObjectType = O
public typealias ValueType = D
public var cs_keyPathString: String {
return self.keyPath
}
}
// MARK: - RelationshipContainer.ToManyOrdered: DynamicKeyPath
extension RelationshipContainer.ToManyOrdered: DynamicKeyPath {
public typealias ObjectType = O
public typealias ValueType = D
public var cs_keyPathString: String {
return self.keyPath
}
}
// MARK: - RelationshipContainer.ToManyUnordered: DynamicKeyPath
extension RelationshipContainer.ToManyUnordered: DynamicKeyPath {
public typealias ObjectType = O
public typealias ValueType = D
public var cs_keyPathString: String {
return self.keyPath
}
}

View File

@@ -189,7 +189,7 @@ public struct OrderBy<D: DynamicObject>: OrderByClause, FetchClause, QueryClause
/**
Indicates that the `KeyPathString` should be sorted in ascending order
*/
public static func ascending<A, T>(_ attribute: KeyPath<D, A>) -> SortKey where A: ValueContainer<D>.Required<T> {
public static func ascending<T>(_ attribute: KeyPath<D, ValueContainer<D>.Required<T>>) -> SortKey {
return .ascending(D.meta[keyPath: attribute].keyPath)
}
@@ -197,7 +197,7 @@ public struct OrderBy<D: DynamicObject>: OrderByClause, FetchClause, QueryClause
/**
Indicates that the `KeyPathString` should be sorted in ascending order
*/
public static func ascending<A, T>(_ attribute: KeyPath<D, A>) -> SortKey where A: ValueContainer<D>.Optional<T> {
public static func ascending<T>(_ attribute: KeyPath<D, ValueContainer<D>.Optional<T>>) -> SortKey {
return .ascending(D.meta[keyPath: attribute].keyPath)
}
@@ -205,7 +205,7 @@ public struct OrderBy<D: DynamicObject>: OrderByClause, FetchClause, QueryClause
/**
Indicates that the `KeyPathString` should be sorted in ascending order
*/
public static func ascending<A, T>(_ attribute: KeyPath<D, A>) -> SortKey where A: TransformableContainer<D>.Required<T> {
public static func ascending<T>(_ attribute: KeyPath<D, TransformableContainer<D>.Required<T>>) -> SortKey {
return .ascending(D.meta[keyPath: attribute].keyPath)
}
@@ -213,7 +213,7 @@ public struct OrderBy<D: DynamicObject>: OrderByClause, FetchClause, QueryClause
/**
Indicates that the `KeyPathString` should be sorted in ascending order
*/
public static func ascending<A, T>(_ attribute: KeyPath<D, A>) -> SortKey where A: TransformableContainer<D>.Optional<T> {
public static func ascending<T>(_ attribute: KeyPath<D, TransformableContainer<D>.Optional<T>>) -> SortKey {
return .ascending(D.meta[keyPath: attribute].keyPath)
}
@@ -221,7 +221,7 @@ public struct OrderBy<D: DynamicObject>: OrderByClause, FetchClause, QueryClause
/**
Indicates that the `KeyPathString` should be sorted in descending order
*/
public static func descending<A, T>(_ attribute: KeyPath<D, A>) -> SortKey where A: ValueContainer<D>.Required<T> {
public static func descending<T>(_ attribute: KeyPath<D, ValueContainer<D>.Required<T>>) -> SortKey {
return .descending(D.meta[keyPath: attribute].keyPath)
}
@@ -229,7 +229,7 @@ public struct OrderBy<D: DynamicObject>: OrderByClause, FetchClause, QueryClause
/**
Indicates that the `KeyPathString` should be sorted in descending order
*/
public static func descending<A, T>(_ attribute: KeyPath<D, A>) -> SortKey where A: ValueContainer<D>.Optional<T> {
public static func descending<T>(_ attribute: KeyPath<D, ValueContainer<D>.Optional<T>>) -> SortKey {
return .descending(D.meta[keyPath: attribute].keyPath)
}
@@ -237,7 +237,7 @@ public struct OrderBy<D: DynamicObject>: OrderByClause, FetchClause, QueryClause
/**
Indicates that the `KeyPathString` should be sorted in descending order
*/
public static func descending<A, T>(_ attribute: KeyPath<D, A>) -> SortKey where A: TransformableContainer<D>.Required<T> {
public static func descending<T>(_ attribute: KeyPath<D, TransformableContainer<D>.Required<T>>) -> SortKey {
return .descending(D.meta[keyPath: attribute].keyPath)
}
@@ -245,7 +245,7 @@ public struct OrderBy<D: DynamicObject>: OrderByClause, FetchClause, QueryClause
/**
Indicates that the `KeyPathString` should be sorted in descending order
*/
public static func descending<A, T>(_ attribute: KeyPath<D, A>) -> SortKey where A: TransformableContainer<D>.Optional<T> {
public static func descending<T>(_ attribute: KeyPath<D, TransformableContainer<D>.Optional<T>>) -> SortKey {
return .descending(D.meta[keyPath: attribute].keyPath)
}
@@ -262,7 +262,7 @@ public extension OrderBy.SortKey where D: CoreStoreObject {
/**
Indicates that the `KeyPathString` should be sorted in ascending order
*/
public static func ascending<T: AnyCoreStoreKeyPath>(_ attribute: (D) -> T) -> OrderBy<D>.SortKey {
public static func ascending<K: DynamicKeyPath>(_ attribute: (D) -> K) -> OrderBy<D>.SortKey {
return .ascending(attribute(D.meta).cs_keyPathString)
}
@@ -270,7 +270,7 @@ public extension OrderBy.SortKey where D: CoreStoreObject {
/**
Indicates that the `KeyPathString` should be sorted in descending order
*/
public static func descending<T: AnyCoreStoreKeyPath>(_ attribute: (D) -> T) -> OrderBy<D>.SortKey {
public static func descending<K: DynamicKeyPath>(_ attribute: (D) -> K) -> OrderBy<D>.SortKey {
return .descending(attribute(D.meta).cs_keyPathString)
}

View File

@@ -234,11 +234,7 @@ public enum RelationshipContainer<O: CoreStoreObject> {
// MARK: RelationshipProtocol
/**
The keyPath string represented by this property. Generally, there are more type-safe utilities for querying and other common tasks.
*/
public let keyPath: KeyPathString
internal let keyPath: KeyPathString
internal let isToMany = false
internal let isOrdered = false
internal let deleteRule: NSDeleteRule
@@ -505,11 +501,7 @@ public enum RelationshipContainer<O: CoreStoreObject> {
// MARK: RelationshipProtocol
/**
The keyPath string represented by this property. Generally, there are more type-safe utilities for querying and other common tasks.
*/
public let keyPath: KeyPathString
internal let keyPath: KeyPathString
internal let isToMany = true
internal let isOptional = true
internal let isOrdered = true
@@ -782,11 +774,7 @@ public enum RelationshipContainer<O: CoreStoreObject> {
// MARK: RelationshipProtocol
/**
The keyPath string represented by this property. Generally, there are more type-safe utilities for querying and other common tasks.
*/
public let keyPath: KeyPathString
internal let keyPath: KeyPathString
internal let isToMany = true
internal let isOptional = true
internal let isOrdered = false

View File

@@ -192,16 +192,12 @@ public enum TransformableContainer<O: CoreStoreObject> {
// MARK: AttributeProtocol
/**
The keyPath string represented by this property. Generally, there are more type-safe utilities for querying and other common tasks.
*/
public let keyPath: KeyPathString
internal static var attributeType: NSAttributeType {
return .transformableAttributeType
}
internal let keyPath: KeyPathString
internal let isOptional = false
internal let isIndexed: Bool
internal let isTransient: Bool
@@ -423,11 +419,7 @@ public enum TransformableContainer<O: CoreStoreObject> {
return .transformableAttributeType
}
/**
The keyPath string represented by this property. Generally, there are more type-safe utilities for querying and other common tasks.
*/
public let keyPath: KeyPathString
internal let keyPath: KeyPathString
internal let isOptional = true
internal let isIndexed: Bool
internal let isTransient: Bool

View File

@@ -195,11 +195,7 @@ public enum ValueContainer<O: CoreStoreObject> {
return V.cs_rawAttributeType
}
/**
The keyPath string represented by this property. Generally, there are more type-safe utilities for querying and other common tasks.
*/
public let keyPath: KeyPathString
internal let keyPath: KeyPathString
internal let isOptional = false
internal let isIndexed: Bool
internal let isTransient: Bool
@@ -424,11 +420,7 @@ public enum ValueContainer<O: CoreStoreObject> {
return V.cs_rawAttributeType
}
/**
The keyPath string represented by this property. Generally, there are more type-safe utilities for querying and other common tasks.
*/
public let keyPath: KeyPathString
internal let keyPath: KeyPathString
internal let isOptional = true
internal let isIndexed: Bool
internal let isTransient: Bool