Merge pull request #2 from shial4/Release-0.1.1

Release 0.1.1
This commit is contained in:
Shial 2018-01-07 12:51:33 +11:00 committed by GitHub
commit 5f23fc3ab2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 60 additions and 19 deletions

BIN
Content/SLazeKit.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 132 KiB

View File

@ -22,6 +22,43 @@ SLazeKit is an easy to use Swift restful collection of extensions and classes. D
- serialize `CoreData` models from API response
- fast and simple extend your models with `API` & `CoreData`
<p align="center">
<img src="Content/SLazeKit.png">
</p>
- The type `ResponseModel` is a result object type for result callback argument. If you want to decode array you simple execute method on `[ResponseModel]` type.
- The `error` indicates why the request failed, or nil if the request was successful. it include `EntityMaping` errors, url preparation error and so on. If a response from the server is received, regardless of whether the request completes successfully or fails, the response parameter contains that information.
- The `response` provides tuple with HTTPURLResponse object and the data returned by the server.
- The request handler is executed on the `URLSession` delegate queue.
```swift
class func getRequest(for modelId: String, success: @escaping ((Model?) ->()), failure: (() ->())? = nil) throws {
let _ = ResponseModel.get(path: PathPattern.model.patternToPath(with: ["modelId":modelId])) { (response, result, error) in
guard response?.urlResponse?.statusCode == 200 && error == nil else {
failure?()
return
}
success(try result?.serialized(SLazeKit.newBackgroundContext()) as? Model)
}
}
```
If `ResponseModel` conforms to `EntityMapping` protocol it will be synchronize with CoreData. That means if mode object already exist it will update it or creat new instance and fill `NSManagedObject` with response data. You can serialize your response to recive `CoreData` object on given context by:
```swift
try result?.serialized(NSManagedObjectContext) as? Model)
```
Calling this method simple query `NSManagedObject` which was synchronize before by given object id.
**Models example**
Simple
[Object.swift](Tests/SLazeKitTests/Models/Object.swift)
Advance
[Model.swift](Tests/SLazeKitTests/Models/Model.swift)
## 🔧 Installation
**CocoaPods:**
@ -89,12 +126,6 @@ extension SLazeKit {
}
```
**Model example**
Simple
[Object.swift](Tests/SLazeKitTests/Models/Object.swift)
Advance
[Model.swift](Tests/SLazeKitTests/Models/Model.swift)
## ⭐ Contributing
Be welcome to contribute to this project! :)

View File

@ -1,6 +1,6 @@
Pod::Spec.new do |s|
s.name = 'SLazeKit'
s.version = '0.1.0'
s.version = '0.1.1'
s.summary = 'Swift restful manager.'
s.description = <<-DESC
SLazeKit is an easy to use Swift restfull collection of extensions and classes. Don't spend hours writing your code to map your rest api request into models and serialization. stop wasting your time!

View File

@ -4,12 +4,20 @@ import CoreData
extension NSManagedObjectContext {
/// If the context has uncommitted changes, attempts to commit unsaved changes to registered objects to the contexts parent store.
func commit() {
if hasChanges {
self.performAndWait {
if self.hasChanges {
do {
try save()
self.parent?.perform({
if self.parent?.hasChanges == true {
self.parent?.commit()
self.parent?.refreshAllObjects()
}
})
} catch {
print(error)
}
}
}
}
}

View File

@ -15,13 +15,15 @@ extension EntityMapping {
func map() throws -> NSManagedObject? {
guard let context = SLazeKit.newBackgroundContext() else { return nil }
let model: NSManagedObject
context.performAndWait {
if let attribiutes = idAttributes {
model = try Self.entityType.find(context, by: attribiutes) ?? Self.entityType.init(context: context)
} else {
model = try findObject(context) ?? Self.entityType.init(context: context)
}
fillObject(with: model)
context.performAndWait { context.commit() }
context.commit()
}
return model
}
private func findObject(_ context: NSManagedObjectContext?) throws -> NSManagedObject? {