Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/ABridoux/SafeFetching
DSL to build predicates and requests for CoreData fetching
https://github.com/ABridoux/SafeFetching
coredata fetching predicate swift
Last synced: about 1 month ago
JSON representation
DSL to build predicates and requests for CoreData fetching
- Host: GitHub
- URL: https://github.com/ABridoux/SafeFetching
- Owner: ABridoux
- License: mit
- Created: 2021-10-23T10:47:27.000Z (almost 3 years ago)
- Default Branch: main
- Last Pushed: 2022-10-04T15:47:19.000Z (almost 2 years ago)
- Last Synced: 2024-07-19T03:08:21.363Z (2 months ago)
- Topics: coredata, fetching, predicate, swift
- Language: Swift
- Homepage:
- Size: 412 KB
- Stars: 20
- Watchers: 3
- Forks: 3
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: License
Awesome Lists containing this project
README
# SafeFetching
This library offers a DSL (Domain Specific Language) to safely build predicates and requests to fetch a CoreData store. Also a wrapper around `NSFetchedResultsController` is offered to publish arrays of `NSManagedObject` to be used with a `NSDiffableDataSource`.
The documentation is built with docC. You can [read it online](https://abridoux.github.io/SafeFetching/documentation/safefetching/) or locally by running *Product* → *Build Documentation* or hitting **⇧⌃⌘D**.
## Convenient and safe fetching
For any CoreData entity generated by Xcode, the only required step is to make it implement `Fetchable`.
```swift
final class RandomEntity: NSManagedObject {@NSManaged var score = 0.0
@NSManaged var name: String? = ""
}
``````swift
extension RandomEntity: Fetchable {}
```Then it's possible to use the DSL to build a request. The last step can either get the built request as `NSFetchRequest` or execute the request in the provided context.
```swift
RandomEntity.request()
.all(after: 10)
.where(\.score >= 15 || \.name != "Joe")
.sorted(by: .ascending(\.score), .descending(\.name))
.setting(\.returnsDistinctResults, to: true)
.nsValue
``````swift
RandomEntity.request()
.all(after: 10)
.where(\.score >= 15 || \.name != "Joe")
.sorted(by: .ascending(\.score), .descending(\.name))
.setting(\.returnsDistinctResults, to: true)
.fetch(in: context) // returns [RandomEntity]
```Advanced `NSPredicate` operators are also available like `BEGINSWITH` (`hasPrefix`). To use one, specified a key path followed by `*`:
```swift
RandomEntity.request()
.all()
.where(\.name * .hasPrefix("Do"))
.nsValue
```More about that in the documentation.