Ios Swift-核心数据种子类
我遵循当前的教程,创建一个大型类来处理在大型数据库中植入数据 我的数据库是使用JSON填充的,不过我想复制作者在上面文章中使用的模式 在文章中,他提到这种方法违反了一次性使用责任。我知道类应该承担单一的责任,但是在像我这样的情况下,例如当用户登录时,我需要为一个相当大的数据集播种种子,还有其他方法可以采取吗Ios Swift-核心数据种子类,ios,swift,core-data,Ios,Swift,Core Data,我遵循当前的教程,创建一个大型类来处理在大型数据库中植入数据 我的数据库是使用JSON填充的,不过我想复制作者在上面文章中使用的模式 在文章中,他提到这种方法违反了一次性使用责任。我知道类应该承担单一的责任,但是在像我这样的情况下,例如当用户登录时,我需要为一个相当大的数据集播种种子,还有其他方法可以采取吗 我很抱歉,如果这引发了一场讨论,那不是我的本意,我的问题是这种播种方式在制作中是否普遍,实现这种数据播种的最佳模式是什么。我认为不可能真正回答每个人是如何在生产中导入数据的,因为每个人都可
我很抱歉,如果这引发了一场讨论,那不是我的本意,我的问题是这种播种方式在制作中是否普遍,实现这种数据播种的最佳模式是什么。我认为不可能真正回答每个人是如何在生产中导入数据的,因为每个人都可以做不同的事情 相反,我只想指出,根据苹果的“核心数据编程指南”,导入数据的最有效方式是通过批量导入过程。这里详细介绍了这个过程
这样,我会把你的数据存储在一个JSON文件中,它存储在一个Web服务上,或者应用程序包中作为一个资源,然后使用NSJSONSerialTrand类将它转换成代码可以用来推理的基础对象。然后,我将使用上面指南中概述的原则创建一个批量导入过程来为数据库种子
差不多就是这样,苹果的例子也很直截了当。我还要声明,最好在后台线程上运行此过程,因为如果导入需要很长时间才能完成,操作系统可能会终止您的应用程序 希望这有帮助 *编辑* 下面是一个示例,说明如何使用协议和泛型对任何类型的对象执行任务。您可以使用此模式执行任何类型的操作,因此只需接受概念并输入核心数据逻辑即可 这只是一个可以遵循的模式的示例,不应该被视为即插即用实现。需要对其进行调整,以支持核心数据批量导入和保存。然而,它确实清楚地显示了一种获取字典或字典数组并将其解码为对象的方法。那么你如何处理你的物品完全取决于你自己protocol JSONDecodable {
// This is used so you can have a unified way to instantiate an instance without relying on sub-classing NSObject
init()
// This can be implemented so you can manually decode a single object of the type from a dictionary
static func decodeFromJSON(json: AnyObject?) -> AnyObject?
// This is used so that you can manually set the values to the object. This is required as Swift reflection doesn't support dynamic property setting
func setValueForKey(value: AnyObject?, forKey: String)
}
// This class is responsible for decoding a JSON dictionary into an object
class JSONDecoder<T:JSONDecodable>: NSObject {
//MARK: Initialization
required override init() {
// do any initialization here
}
//MARK: Public Methods
/**
Creates a single object from the JSON. This method should only be used if the JSON will only ever contain a single object
:json: A dictionary of data
:returns: An object of the given type
*/
func toSingle(json: AnyObject?) -> T? {
// process single object, and return an array with the one object
if let dict = json as? [NSObject: AnyObject] {
return self.makeObject(dict)
}
return nil
}
/**
Creates a list of objects from the JSON. This method should only be used if the JSON will contain multiple objects
:json: A dictionary of data
:returns: An list of objects of the given type
*/
func toArray(json: AnyObject?) -> [T]? {
// process array
if let arr = json as? [AnyObject] {
return self.makeObjects(arr)
} else if let dict = json as? [NSObject: AnyObject] {
// process single object, and return an array with the one object
var arr = [T]()
arr.append(self.makeObject(dict))
return arr
}
return nil
}
//MARK: The Magic
private func makeObjects(jsonArray: [AnyObject]?) -> [T]? {
var returnArray: [T] = [T]()
if let jArray = jsonArray {
for jObject in jArray {
if let dict = jObject as? [NSObject: AnyObject] {
returnArray.append(self.makeObject(dict))
}
}
}
if returnArray.count > 0 {
return returnArray
} else {
return nil
}
}
private func makeObject(jsonDict: [NSObject: AnyObject]) -> T {
var returnObject = T.self() // this is where the init() function in the protocol comes in handy. It allows us to use generics to create a dynamic instance of our object
for (key, value) in jsonDict {
if let k = key as? String {
returnObject.setValueForKey(value, forKey: k) // this is where the setValueForKey(value: AnyObject?, forKey: String) function in the protocol comes in handy. It allows us to let the object it's self set it's own values.
}
}
return returnObject
}
}
// This is an example class that implements the protocol which means it can be sent through the decoding process
class Employee: NSManagedObject, JSONDecodable {
//MARK: - Properties
var employeID: Int!
var name: Int!
var hireDate: NSDate?
var department: Department?
//MARK: - Initialization
override required init() {
// Necessary to satisfy the JSONDecodable protocol
}
static func decodeFromJSON(json: AnyObject?) -> AnyObject? {
var decoder = JSONDecoder<Employee>()
return decoder.toSingle(json)
}
func setValueForKey(value: AnyObject?, forKey: String) {
switch (forKey) {
case "employeID":
self.employeID = value as! Int
case "name":
self.name = value as! String
case "hireDate":
if let v = value as? String {
let dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "MM/dd/yyyy"
self.hireDate = dateFormatter.dateFromString(v)
}
case "department":
if let v = value as? [NSObject: AnyObject] {
if let dept = Department.decodeFromJSON(dict) as? Department {
self.department = dept
}
}
default:
NSLog("[setValueForKey] Unable to find property \(forKey)")
}
}
}
协议JSONDecodable{
//使用此选项,您可以使用统一的方式实例化实例,而无需依赖子类NSObject
init()
//这可以实现,以便您可以从字典中手动解码该类型的单个对象
静态func decodeFromJSON(json:AnyObject?->AnyObject?
//使用此选项可以手动设置对象的值。这是必需的,因为Swift reflection不支持动态属性设置
func setValueForKey(值:AnyObject?,forKey:String)
}
//此类负责将JSON字典解码为对象
类JSONDecoder:NSObject{
//标记:初始化
必需的重写初始化(){
//在这里进行任何初始化
}
//标记:公共方法
/**
从JSON创建单个对象。仅当JSON仅包含单个对象时,才应使用此方法
:json:数据字典
:返回:给定类型的对象
*/
func-toSingle(json:AnyObject?->T{
//处理单个对象,并返回一个包含一个对象的数组
如果让dict=json作为?[NSObject:AnyObject]{
返回self.makeObject(dict)
}
归零
}
/**
从JSON创建对象列表。仅当JSON将包含多个对象时,才应使用此方法
:json:数据字典
:返回:给定类型的对象列表
*/
func toArray(json:AnyObject?->[T]{
//进程阵列
如果让arr=json作为?[AnyObject]{
返回self.makeObjects(arr)
}如果让dict=json作为?[NSObject:AnyObject]{
//处理单个对象,并返回一个包含一个对象的数组
var arr=[T]()
arr.append(self.makeObject(dict))
返回arr
}
归零
}
//马克:魔术
私有func makeObjects(jsonArray:[AnyObject]?)->[T]{
var returnArray:[T]=[T]()
如果让jArray=jsonArray{
jArray中的jObject{
如果让dict=jObject作为?[NSObject:AnyObject]{
returnArray.append(self.makeObject(dict))
}
}
}
如果returnArray.count>0{
返回数组
}否则{
归零
}
}
private func makeObject(jsonDict:[NSObject:AnyObject])->T{
var returnObject=T.self()//协议中的init()函数就是在这里派上用场的。它允许我们使用泛型来创建对象的动态实例
用于jsonDict中的(键、值){
如果让k=键为?字符串{
returnObject.setValueForKey(value,forKey:k)//协议中的setValueForKey(value:AnyObject?,forKey:String)函数就在这里派上了用场。它允许我们让对象自己设置自己的值。
}
}
返回对象
}
}
//这是一个实现协议的示例类,这意味着它可以通过解码过程发送
类别雇员:NSManagedObject,JSONDecodable{
//标记:-属性
var employeID:Int!
变量名:Int!
var hireDate:NSDate?
var部门:部门?
//标记:-初始化
重写所需的init(){
//必须满足JSONDECODATABLE协议
}
静态func decodeFromJSON(json:AnyObject?->AnyObject{
var decoder=JSONDecoder()
返回解码器.toSingle(json)
}
func s