TestsTested | ✗ |
LangLanguage | SwiftSwift |
License | MIT |
ReleasedLast Release | Feb 2018 |
SwiftSwift Version | 4.0 |
SPMSupports SPM | ✗ |
Maintained by Mandea Daniel.
To run the example project, clone the repo, and run pod install
from the Example directory first.
import Foundation
import CoreData
import CoreDataStackHelper
final class User: NSManagedObject, AddManagedObject, SaveManagedObject, Fetch {
}
// MARK: - UpdateManangedObject
extension User: UpdateManangedObject {
public func update(with data: Any) {
// Checkout data
guard let data = data as? Dictionary<String, Any> else {
return
}
// Update managed object
name = data["name"] as? String
uniqueID = data["uniqueID"] as? String
age = data["age"] as? NSNumber
}
}
To setup core data stack only call this method:
/**
Use this method in order to load the persistent store
- parameter name: The name of the container
- parameter descriptions: The descriptions related to NSPersistentStoreDescription
- parameter block: The block that is called after completing the process
*/
@available(iOS 10.0, *)
public func setup(with name: String = kDefatultContainerName, descriptions:Array<NSPersistentStoreDescription>?, comletion block:@escaping LoadPersistentStoreCompletion)
Example:
// Call setup in your app delegate
Persistence.store.setup(descriptions: nil) { (persistentStoreDescription, error) in
if let currentError = error {
print(currentError)
}
}
Helper Methods:
/**
This method is called by all managedObjects in order to save single
- parameter data: The data received for saving
- parameter context: The context that should handle the fetch
- parameter completion: The block that is called after the process completed
*/
static func saveSingle(data:Any, context:NSManagedObjectContext, completion:@escaping CompletionBlock)
/**
This method is called by all managedObjects in order to save multiple
- parameter data: The data received for saving
- parameter context: The context that should handle the fetch
- parameter completion: The block that is called after the process completed
*/
static func saveMultiple(data:Array<Any>, context:NSManagedObjectContext, completion:@escaping CompletionBlock)
Example:
// Save some users
User.saveMultiple(data: users, context: savingContext) { (success, error) in
// Do smth in the completion
}
Helper methods:
/**
Use this method i order to fetch some context based on some predicate
- parameter predicate: The predicte that is used for fetch
- parameter context: The context that will handle the fetch
- return: An instance of NSArray containing Self or nil
*/
static func fetch(with predicate: NSPredicate?, in context: NSManagedObjectContext) throws -> Array<Self>?
/**
Use this method i order to count an entity in context based on some predicate
- parameter predicate: The predicte that is used for fetch
- parameter context: The context that will handle the fetch
- return: An instance of Int or nil
*/
static func count(with predicate: NSPredicate?, in context: NSManagedObjectContext) throws -> Int?
Example:
do {
if let data = try User.fetch(with: nil, in: self.viewContext) {
self.allUsers = data
}
} catch {
print(error)
}
iOS 10
CoreDataStackHelper is available through CocoaPods. To install
it, simply add the following line to your Podfile:
pod "CoreDataStackHelper"
DanielMandea, [email protected]
CoreDataStackHelper is available under the MIT license. See the LICENSE file for more info.