Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/alexander-ignition/combinecoredata
Combine + CoreData
https://github.com/alexander-ignition/combinecoredata
combine coredata swift swiftui
Last synced: about 5 hours ago
JSON representation
Combine + CoreData
- Host: GitHub
- URL: https://github.com/alexander-ignition/combinecoredata
- Owner: Alexander-Ignition
- License: mit
- Created: 2020-05-03T06:52:04.000Z (over 4 years ago)
- Default Branch: master
- Last Pushed: 2020-11-07T13:30:03.000Z (about 4 years ago)
- Last Synced: 2023-08-03T17:35:48.101Z (over 1 year ago)
- Topics: combine, coredata, swift, swiftui
- Language: Swift
- Homepage:
- Size: 25.4 KB
- Stars: 12
- Watchers: 4
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# 🚜 CombineCoreData 🗄
[![SPM compatible](https://img.shields.io/badge/spm-compatible-brightgreen.svg?style=flat)](https://swift.org/package-manager)
[![GitHub license](https://img.shields.io/badge/license-MIT-lightgrey.svg)](https://github.com/Alexander-Ignition/CombineCoreData/blob/master/LICENSE)
[![GitHub Workflow Test](https://github.com/Alexander-Ignition/CombineCoreData/workflows/Test/badge.svg)](https://github.com/Alexander-Ignition/CombineCoreData/actions?query=workflow%3ATest)> Inspired by [ReactiveCocoa and Core Data Concurrency](https://thoughtbot.com/blog/reactive-core-data)
- You will no longer need to use method `perform(_:)` directly with `do catch`.
- You can forget about the callback based api when working with CoreData.## Features
- [x] NSManagedObjectContext produce Publisher
- [x] NSManagedObjectContext + Scheduler## Instalation
Add dependency to `Package.swift`...
```swift
.package(url: "https://github.com/Alexander-Ignition/CombineCoreData", from: "0.0.3"),
```... and your target
```swift
.target(name: "ExampleApp", dependencies: ["CombineCoreData"]),
```## Usage
Wrap any operation with managed objects in context with method `publisher(_:)`.
```swift
import CombineCoreDatamanagedObjectContext.publisher {
// do something
}
```Full examples you can see in [Sources/Books](Sources/Books). This module contains [Book](Sources/Books/Book.swift) and [BookStorage](Sources/Books/BookStorage.swift) that manages books.
### Save objects
Example of asynchronously saving books in а `backgroundContex` on its private queue.
```swift
func saveBooks(names: [String]) -> AnyPublisher {
backgroundContex.publisher {
for name in names {
let book = Book(context: self.backgroundContex)
book.name = name
}
try self.backgroundContex.save()
}
}
```### Fetch objects
Example of asynchronously fetching books in а `backgroundContex` on its private queue.
```swift
func fetchBooks() -> AnyPublisher<[Book], Error> {
backgroundContex.fetchPublisher(Book.all)
}
```## Scheduler
You can use `NSManagedObjectContext` instead of `OperationQeue`, `DispatchQueue` or `RunLoop` with operators `receive(on:)` and `subscribe(on:)`
```swift
let subscription = itemService.load()
.receive(on: viewContext)
.sink(receiveCompletion: { completion in
// Receive `completion` on main queue in `viewContext`
print(completion)
}, receiveValue: { (items: [Item]) in
// Receive `[Item]` on main queue in `viewContext`
print(book)
})
````CombineCoreData` extends `NSManagedObjectContext` to adapt the `Scheduler` protocol. Because `NSManagedObjectContext` has a private queue and and schedule task through method `perform(_:)`.