This repository has been archived by the owner on Feb 28, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 100
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
15 changed files
with
382 additions
and
143 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
// | ||
// EntityObserver.swift | ||
// AlecrimCoreData | ||
// | ||
// Created by Vanderlei Martinelli on 07/06/18. | ||
// Copyright © 2018 Alecrim. All rights reserved. | ||
// | ||
|
||
import Foundation | ||
|
||
// MARK: - | ||
|
||
/// A fetch request controller wrapper for one entity only. Can be used as observer for an entity detail presentation, for example. | ||
public final class EntityObserver<EntityType: ManagedObject> { | ||
|
||
private let frc: FetchRequestController<EntityType> | ||
|
||
fileprivate init(entity: EntityType, propertyName: String, updateHandler didChangeContentClosure: @escaping () -> Void, context: ManagedObjectContext) { | ||
self.frc = Query<EntityType>(in: context) | ||
.batchSize(0) | ||
.filtered(using: Predicate(format: "SELF == %@", argumentArray: [entity])) | ||
.sorted(by: SortDescriptor(key: propertyName, ascending: true)) | ||
.toFetchRequestController() | ||
|
||
self.frc.didChangeContent(closure: didChangeContentClosure) | ||
} | ||
|
||
deinit { | ||
self.frc.removeAllBindings() | ||
} | ||
|
||
} | ||
|
||
// MARK: - | ||
|
||
extension PersistentContainerType { | ||
public func observer<EntityType: ManagedObject>(for entity: EntityType, updateHandler: @escaping () -> Void) -> EntityObserver<EntityType> { | ||
// using any property here is fine, but there must be at least one property | ||
guard let propertyName = entity.entity.properties.first(where: { $0.isTransient == false })?.name else { | ||
fatalError("No property found.") | ||
} | ||
|
||
return EntityObserver(entity: entity, propertyName: propertyName, updateHandler: updateHandler, context: self.viewContext) | ||
} | ||
} |
133 changes: 133 additions & 0 deletions
133
Source/AlecrimCoreData/Convenience/ManagedObjectContextType.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,133 @@ | ||
// | ||
// ManagedObjectContextType.swift | ||
// AlecrimCoreData | ||
// | ||
// Created by Vanderlei Martinelli on 07/06/18. | ||
// Copyright © 2018 Alecrim. All rights reserved. | ||
// | ||
|
||
import Foundation | ||
|
||
// MARK: - | ||
|
||
extension ManagedObjectContext: ManagedObjectContextType {} | ||
|
||
// MARK: - | ||
|
||
public protocol ManagedObjectContextType { | ||
func perform(_ block: @escaping () -> Void) | ||
func performAndWait(_ block: () -> Void) | ||
} | ||
|
||
// MARK: - | ||
|
||
extension ManagedObjectContextType { | ||
|
||
public func async<Value>(execute closure: @escaping (Self) throws -> Value, completion: @escaping ((Value?, Error?) -> Void)) { | ||
let context = self | ||
|
||
context.perform { | ||
do { | ||
let value = try closure(context) | ||
completion(value, nil) | ||
} | ||
catch { | ||
completion(nil, error) | ||
} | ||
} | ||
} | ||
|
||
public func async<Value>(execute closure: @escaping (Self) -> Value, completion: ((Value) -> Void)? = nil) { | ||
let context = self | ||
|
||
context.perform { | ||
let value = closure(context) | ||
completion?(value) | ||
} | ||
} | ||
|
||
@discardableResult | ||
public func sync<Value>(execute closure: (Self) throws -> Value) throws -> Value { | ||
var value: Value? | ||
var outError: Error? | ||
|
||
let context = self | ||
|
||
context.performAndWait { | ||
do { | ||
value = try closure(context) | ||
} | ||
catch { | ||
outError = error | ||
} | ||
} | ||
|
||
if let outError = outError { | ||
throw outError | ||
} | ||
|
||
return value! | ||
} | ||
|
||
@discardableResult | ||
public func sync<Value>(execute closure: (Self) -> Value) -> Value { | ||
var value: Value? | ||
|
||
let context = self | ||
|
||
context.performAndWait { | ||
value = closure(context) | ||
} | ||
|
||
return value! | ||
} | ||
|
||
} | ||
|
||
// MARK: - | ||
|
||
extension PersistentContainer { | ||
|
||
public func async<Value>(execute closure: @escaping (ManagedObjectContext) throws -> Value, completion: @escaping ((Value?, Error?) -> Void)) { | ||
return self.backgroundContext.async(execute: closure, completion: completion) | ||
} | ||
|
||
public func async<Value>(execute closure: @escaping (ManagedObjectContext) -> Value, completion: ((Value) -> Void)? = nil) { | ||
return self.backgroundContext.async(execute: closure, completion: completion) | ||
} | ||
|
||
@discardableResult | ||
public func sync<Value>(execute closure: (ManagedObjectContext) throws -> Value) throws -> Value { | ||
return try self.backgroundContext.sync(execute: closure) | ||
} | ||
|
||
@discardableResult | ||
public func sync<Value>(execute closure: (ManagedObjectContext) -> Value) -> Value { | ||
return self.backgroundContext.sync(execute: closure) | ||
} | ||
|
||
} | ||
|
||
// MARK: - | ||
|
||
extension CustomPersistentContainer { | ||
|
||
public func async<Value>(execute closure: @escaping (Context) throws -> Value, completion: @escaping ((Value?, Error?) -> Void)) { | ||
return self.backgroundContext.async(execute: closure, completion: completion) | ||
} | ||
|
||
public func async<Value>(execute closure: @escaping (Context) -> Value, completion: ((Value) -> Void)? = nil) { | ||
return self.backgroundContext.async(execute: closure, completion: completion) | ||
} | ||
|
||
@discardableResult | ||
public func sync<Value>(execute closure: (Context) throws -> Value) throws -> Value { | ||
return try self.backgroundContext.sync(execute: closure) | ||
} | ||
|
||
@discardableResult | ||
public func sync<Value>(execute closure: (Context) -> Value) -> Value { | ||
return self.backgroundContext.sync(execute: closure) | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.