commit
5f23fc3ab2
Binary file not shown.
After Width: | Height: | Size: 132 KiB |
43
README.md
43
README.md
|
@ -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! :)
|
||||
|
|
|
@ -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!
|
||||
|
|
|
@ -2,13 +2,21 @@ import Foundation
|
|||
import CoreData
|
||||
|
||||
extension NSManagedObjectContext {
|
||||
/// Ifthe context has uncommitted changes, attempts to commit unsaved changes to registered objects to the context’s parent store.
|
||||
/// If the context has uncommitted changes, attempts to commit unsaved changes to registered objects to the context’s parent store.
|
||||
func commit() {
|
||||
if hasChanges {
|
||||
do {
|
||||
try save()
|
||||
} catch {
|
||||
print(error)
|
||||
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)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -15,13 +15,15 @@ extension EntityMapping {
|
|||
func map() throws -> NSManagedObject? {
|
||||
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)
|
||||
} else {
|
||||
model = try findObject(context) ?? Self.entityType.init(context: context)
|
||||
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.commit()
|
||||
}
|
||||
fillObject(with: model)
|
||||
context.performAndWait { context.commit() }
|
||||
return model
|
||||
}
|
||||
private func findObject(_ context: NSManagedObjectContext?) throws -> NSManagedObject? {
|
||||
|
|
Loading…
Reference in New Issue