Swiftpack.co - ABridoux/SafeFetching as Swift Package

Swiftpack.co is a collection of thousands of indexed Swift packages. Search packages.
See all packages published by ABridoux.
ABridoux/SafeFetching 0.7.6
DSL to build predicates and requests for CoreData fetching
โญ๏ธ 20
๐Ÿ•“ 1 year ago
iOS macOS tvOS
.package(url: "https://github.com/ABridoux/SafeFetching.git", from: "0.7.6")

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 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.

final class RandomEntity: NSManagedObject {

    @NSManaged var score = 0.0
    @NSManaged var name: String? = ""
}
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<RandomEntity> or execute the request in the provided context.

RandomEntity.request()
    .all(after: 10)
    .where(\.score >= 15 || \.name != "Joe")
    .sorted(by: .ascending(\.score), .descending(\.name))
    .setting(\.returnsDistinctResults, to: true)
    .nsValue
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 *:

RandomEntity.request()
    .all()
    .where(\.name * .hasPrefix("Do"))
    .nsValue

More about that in the documentation.

GitHub

link
Stars: 20
Last commit: 1 year ago
Advertisement: IndiePitcher.com - Cold Email Software for Startups

Release Notes

Fixed backslash escaping
1 year ago

Swiftpack is being maintained by Petr Pavlik | @ptrpavlik | @swiftpackco | API | Analytics