CoreDataStackHelper 0.4.3

CoreDataStackHelper 0.4.3

测试已测试
语言语言 SwiftSwift
许可 MIT
发布上次发布2018年2月
SwiftSwift版本4.0
SPM支持SPM

Mandea Daniel 维护。






示例

要运行示例项目,请克隆仓库,并首先从Example目录运行pod install

设置Core Data条目

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
    }
}

设置Core Data堆栈

仅调用此方法来设置Core Data堆栈

/**
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)

示例

// Call setup in your app delegate
Persistence.store.setup(descriptions: nil) { (persistentStoreDescription, error) in
    if let currentError = error {
        print(currentError)
    }
}

基于一些字典创建新的NSManagedObject

辅助方法

/**
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)

示例

// Save some users
User.saveMultiple(data: users, context: savingContext) { (success, error) in
    // Do smth in the completion
}

获取NSManagedObjects

辅助方法

/**
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?

示例

do {
    if let data = try User.fetch(with: nil, in: self.viewContext) {
        self.allUsers = data
    }
} catch {
    print(error)
}

要求

iOS 10

安装

CoreDataStackHelper可以通过CocoaPods获得。要安装它,只需将以下行添加到您的Podfile中

pod "CoreDataStackHelper"

作者

DanielMandea, [email protected]

许可

CoreDataStackHelper遵循MIT许可。有关更多信息,请参阅LICENSE文件。