[0.1.2] Context save

This commit is contained in:
Shial 2018-01-08 08:59:59 +11:00
parent 7960a923fa
commit 006e7606ba
3 changed files with 35 additions and 10 deletions

View File

@ -12,8 +12,7 @@ public protocol EntityMapping {
}
extension EntityMapping {
func map() throws -> NSManagedObject? {
guard let context = SLazeKit.newBackgroundContext() else { return nil }
func map(_ context: NSManagedObjectContext) throws -> NSManagedObject? {
var model: NSManagedObject? = nil
var mapError: Error?
context.performAndWait {
@ -26,7 +25,6 @@ extension EntityMapping {
if let object = model {
fillObject(with: object)
}
context.commit()
} catch {
mapError = error
}

View File

@ -186,12 +186,14 @@ public class SLazeKit {
}
private class func synchronize(_ obj: Any) throws {
guard let context = newBackgroundContext() else { return }
if let array = obj as? [EntityMapping] {
array.forEach({_ = try? $0.map()})
array.forEach({_ = try? $0.map(context)})
} else {
guard let mapper = obj as? EntityMapping else { return }
_ = try mapper.map()
_ = try mapper.map(context)
}
context.commit()
}
}

View File

@ -1,11 +1,36 @@
import XCTest
@testable import SLazeKit
class SLazeKitTests: XCTestCase {
static var allTests = [
("testExample", testExample),
]
extension SLazeKit {
open class var basePath: String? { return "www.yourdomain.com" }
open class var basePort: Int? { return 8765 }
open class var decoder: JSONDecoder { return JSONDecoder() }
open class var urlSession: URLSession { return URLSession.shared }
func testExample() {
open class func setup(_ request: URLRequest) -> URLRequest {
var request: URLRequest = request
request.setValue("Your token", forHTTPHeaderField: "X-Access-Token")
request.setValue("application/json", forHTTPHeaderField: "Content-Type")
return request
}
open class func handle(_ response: HTTPURLResponse?) {
if response?.statusCode == 401 {
print("unauthorised")
}
}
}
class SLazeKitTests: XCTestCase {
static var allTests = [
("testBasePort", testBasePort),
]
func testBasePath() {
XCTAssertTrue(SLazeKit.basePath == "www.yourdomain.com")
}
func testBasePort() {
XCTAssertTrue(SLazeKit.basePort == 8765)
}
}