mirror of
https://github.com/JohnEstropia/CoreStore.git
synced 2026-04-01 23:23:07 +02:00
WIP
This commit is contained in:
@@ -34,6 +34,11 @@ import CoreData
|
||||
*/
|
||||
public final class AsynchronousDataTransaction: BaseDataTransaction {
|
||||
|
||||
public func cancel() throws -> Never {
|
||||
|
||||
throw CoreStoreError.userCancelled
|
||||
}
|
||||
|
||||
/**
|
||||
Saves the transaction changes. This method should not be used after the `commit()` method was already called once.
|
||||
|
||||
|
||||
@@ -36,12 +36,6 @@ public /*abstract*/ class BaseDataTransaction {
|
||||
|
||||
// MARK: Object management
|
||||
|
||||
|
||||
public func cancel() throws -> Never {
|
||||
|
||||
throw CoreStoreError.userCancelled
|
||||
}
|
||||
|
||||
/**
|
||||
Indicates if the transaction has pending changes
|
||||
*/
|
||||
|
||||
@@ -31,6 +31,16 @@ import CoreData
|
||||
|
||||
public extension DataStack {
|
||||
|
||||
|
||||
public func perform<T>(asynchronous task: @escaping (_ transaction: AsynchronousDataTransaction) throws -> T, completion: @escaping (TransactionResult<T>) -> Void) {
|
||||
|
||||
self.perform(
|
||||
asynchronous: task,
|
||||
success: { completion(TransactionResult(userInfo: $0)) },
|
||||
failure: { completion(TransactionResult(error: $0)) }
|
||||
)
|
||||
}
|
||||
|
||||
public func perform<T>(asynchronous task: @escaping (_ transaction: AsynchronousDataTransaction) throws -> T, success: @escaping (T) -> Void, failure: @escaping (CoreStoreError) -> Void) {
|
||||
|
||||
let transaction = AsynchronousDataTransaction(
|
||||
@@ -40,28 +50,28 @@ public extension DataStack {
|
||||
)
|
||||
transaction.transactionQueue.cs_async {
|
||||
|
||||
let userInfo: T
|
||||
do {
|
||||
|
||||
let extraInfo = try task(transaction)
|
||||
transaction.commit { (result) in
|
||||
|
||||
switch result {
|
||||
|
||||
case .success:
|
||||
success(extraInfo)
|
||||
|
||||
case .failure(let error):
|
||||
failure(error)
|
||||
}
|
||||
}
|
||||
userInfo = try task(transaction)
|
||||
}
|
||||
catch let error as CoreStoreError {
|
||||
|
||||
DispatchQueue.main.async { failure(error) }
|
||||
return
|
||||
}
|
||||
catch let error {
|
||||
|
||||
DispatchQueue.main.async { failure(.userError(error: error)) }
|
||||
return
|
||||
}
|
||||
transaction.commit { (result) in
|
||||
|
||||
switch result {
|
||||
|
||||
case .success: success(userInfo)
|
||||
case .failure(let error): failure(error)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -75,10 +85,10 @@ public extension DataStack {
|
||||
)
|
||||
return try transaction.transactionQueue.cs_sync {
|
||||
|
||||
let extraInfo: T
|
||||
let userInfo: T
|
||||
do {
|
||||
|
||||
extraInfo = try task(transaction)
|
||||
userInfo = try task(transaction)
|
||||
}
|
||||
catch let error as CoreStoreError {
|
||||
|
||||
@@ -88,17 +98,13 @@ public extension DataStack {
|
||||
|
||||
throw CoreStoreError.userError(error: error)
|
||||
}
|
||||
|
||||
let result = waitForObserverNotifications
|
||||
? transaction.commitAndWait()
|
||||
: transaction.commit()
|
||||
switch result {
|
||||
|
||||
case .success:
|
||||
return extraInfo
|
||||
|
||||
case .failure(let error):
|
||||
throw error
|
||||
case .success: return userInfo
|
||||
case .failure(let error): throw error
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -34,6 +34,11 @@ import CoreData
|
||||
*/
|
||||
public final class SynchronousDataTransaction: BaseDataTransaction {
|
||||
|
||||
public func cancel() throws -> Never {
|
||||
|
||||
throw CoreStoreError.userCancelled
|
||||
}
|
||||
|
||||
/**
|
||||
Saves the transaction changes and waits for completion synchronously. This method should not be used after the `commit()` or `commitAndWait()` method was already called once.
|
||||
- Important: Unlike `SynchronousDataTransaction.commit()`, this method waits for all observers to be notified of the changes before returning. This results in more predictable data update order, but may risk triggering deadlocks.
|
||||
|
||||
58
Sources/Transactions/TransactionResult.swift
Normal file
58
Sources/Transactions/TransactionResult.swift
Normal file
@@ -0,0 +1,58 @@
|
||||
//
|
||||
// TransactionResult.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: - TransactionResult
|
||||
|
||||
public enum TransactionResult<T> {
|
||||
|
||||
case success(T)
|
||||
|
||||
case failure(CoreStoreError)
|
||||
|
||||
public var boolValue: Bool {
|
||||
|
||||
switch self {
|
||||
|
||||
case .success: return true
|
||||
case .failure: return false
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// MARK: Internal
|
||||
|
||||
internal init(userInfo: T) {
|
||||
|
||||
self = .success(userInfo)
|
||||
}
|
||||
|
||||
internal init(error: CoreStoreError) {
|
||||
|
||||
self = .failure(error)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user