[0.1.0] Add NSManagedObjectContext helper method
This commit is contained in:
parent
a27cdecc8d
commit
7e71f8720e
|
@ -9,10 +9,9 @@ public protocol EntityMapping {
|
|||
func fillObject(with model: NSManagedObject)
|
||||
}
|
||||
|
||||
@available(iOS 10.0, *)
|
||||
extension EntityMapping {
|
||||
func map() throws -> NSManagedObject? {
|
||||
guard let context = Self.persistentContainer?.newBackgroundContext() else { return nil }
|
||||
guard let context = SLazeKit.newBackgroundContext() else { return nil }
|
||||
let model: NSManagedObject
|
||||
if let attribiutes = idAttributes {
|
||||
model = try Self.entityType.find(context, by: attribiutes) ?? Self.entityType.init(context: context)
|
||||
|
@ -23,16 +22,6 @@ extension EntityMapping {
|
|||
context.performAndWait { context.commit() }
|
||||
return model
|
||||
}
|
||||
|
||||
private func findObject(_ context: NSManagedObjectContext?) throws -> NSManagedObject? {
|
||||
return try Self.entityType.find(context, by: idAttributes ?? [])
|
||||
}
|
||||
}
|
||||
|
||||
extension EntityMapping {
|
||||
public func serialized<T: NSManagedObject>(_ context: NSManagedObjectContext?) throws -> T? {
|
||||
return (try Self.entityType.find(context, by: idAttributes ?? [])) as? T
|
||||
}
|
||||
}
|
||||
|
||||
extension Array where Element: EntityMapping {
|
||||
|
@ -40,3 +29,15 @@ extension Array where Element: EntityMapping {
|
|||
return try flatMap({ try $0.serialized(context) })
|
||||
}
|
||||
}
|
||||
|
||||
extension EntityMapping {
|
||||
private func findObject(_ context: NSManagedObjectContext?) throws -> NSManagedObject? {
|
||||
return try Self.entityType.find(context, by: idAttributes ?? [])
|
||||
}
|
||||
|
||||
public func serialized<T: NSManagedObject>(_ context: NSManagedObjectContext?) throws -> T? {
|
||||
return (try Self.entityType.find(context, by: idAttributes ?? [])) as? T
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
|
|
@ -8,11 +8,6 @@ public enum HTTPMethod {
|
|||
case GET, POST, PUT, PATCH, DELETE, COPY, HEAD, OPTIONS, LINK, UNLINK, PURGE, LOCK, UNLOCK, PROPFIND, VIEW
|
||||
}
|
||||
|
||||
@available(iOS 10.0, *)
|
||||
extension EntityMapping {
|
||||
public static var persistentContainer: NSPersistentContainer? { return nil }
|
||||
}
|
||||
|
||||
public class SLazeKit {
|
||||
open class var basePath: String? { return nil }
|
||||
open class var basePort: Int? { return nil }
|
||||
|
@ -21,6 +16,8 @@ public class SLazeKit {
|
|||
|
||||
open class func setup(_ request: URLRequest) -> URLRequest { return request }
|
||||
open class func handle(_ response: HTTPURLResponse?) {}
|
||||
|
||||
open class func newBackgroundContext() -> NSManagedObjectContext? { return nil }
|
||||
}
|
||||
|
||||
//MARK: network tasks implementation
|
||||
|
@ -40,9 +37,7 @@ extension SLazeKit {
|
|||
if let data = data, error == nil {
|
||||
do {
|
||||
let object = try decoder.decode(T.self, from: data)
|
||||
if #available(iOS 10.0, *) {
|
||||
try synchronize(object)
|
||||
}
|
||||
try synchronize(object)
|
||||
handler(response as? HTTPURLResponse, object, nil)
|
||||
} catch {
|
||||
handler(response as? HTTPURLResponse, nil, error)
|
||||
|
@ -176,12 +171,8 @@ extension SLazeKit {
|
|||
urlComponents?.queryItems = queryItems
|
||||
return urlComponents
|
||||
}
|
||||
}
|
||||
|
||||
//MARK: CoreData mapping support
|
||||
extension SLazeKit {
|
||||
@available(iOS 10.0, *)
|
||||
fileprivate class func synchronize(_ obj: Any) throws {
|
||||
|
||||
private class func synchronize(_ obj: Any) throws {
|
||||
if let array = obj as? [EntityMapping] {
|
||||
array.forEach({_ = try? $0.map()})
|
||||
} else {
|
||||
|
|
|
@ -109,7 +109,7 @@ extension Model {
|
|||
do {
|
||||
//Result is type of `[ModelResponse]` to return array of our CoreData models we need to serialize it.
|
||||
//result?.serialized will return `[Model]`
|
||||
models = try result?.serialized(ModelResponse.persistentContainer?.newBackgroundContext())
|
||||
models = try result?.serialized(SLazeKit.newBackgroundContext())
|
||||
} catch {
|
||||
print(error)
|
||||
}
|
||||
|
@ -128,7 +128,7 @@ extension Model {
|
|||
do {
|
||||
//Result of type `ModelResponse` to return CoreData model we need to serialize it.
|
||||
//result?.serialized will return `Model`
|
||||
models = try result?.serialized(ModelResponse.persistentContainer?.newBackgroundContext())
|
||||
models = try result?.serialized(SLazeKit.newBackgroundContext())
|
||||
} catch {
|
||||
print(error)
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue