gpt4 book ai didi

ios - Swift - 核心数据播种类

转载 作者:搜寻专家 更新时间:2023-11-01 06:44:47 26 4
gpt4 key购买 nike

我正在学习有关创建大型类以处理用数据为大型数据库播种的当前教程。

http://www.andrewcbancroft.com/2015/02/25/using-swift-to-seed-a-core-data-database/

我的数据库是使用 JSON 填充的,尽管我想复制作者在上述文章中使用的模式。

在文章中,他提到这种方法违反了一次性使用责任。我知道类应该承担单一责任,但考虑到像我这样的情况,例如在用户登录时我需要播种相当大的数据集,是否有另一种方法可以采用?

如果这引起了讨论,我深表歉意,这不是我的本意,我的问题是这种播种方式在生产中是否司空见惯,如果不是,实现这种数据播种的最佳模式是什么。

最佳答案

我认为不可能真正回答每个人如何在生产中导入数据,因为每个人都可以做不同的事情。

相反,我只想提一下,根据 Apple 的“核心数据编程指南”,导入数据的最有效方法是通过批量导入过程。此处详细介绍了此过程。

https://developer.apple.com/library/ios/documentation/Cocoa/Conceptual/CoreData/Articles/cdImporting.html

话虽如此,我会将您的数据存储在一个 JSON 文件中,该文件存储在 Web 服务上,或者作为资源存储在应用程序包中,然后使用 NSJsonSerialization 类将其转换为您的代码可以推理的基础对象和。然后我会使用上面指南中概述的原则来创建批量导入过程来为您的数据库播种。

差不多就这些了,Apple 的示例非常简单明了。我还要声明最好在后台线程上运行此进程,因为如果导入需要很长时间才能完成,操作系统可能会终止您的应用程序。

希望这对您有所帮助!

* 编辑 *

这是一个示例,说明如何使用协议(protocol)和泛型对任何类型的对象执行任务。您可以使用此模式执行任何类型的操作,因此只需了解概念并输入您的核心数据逻辑即可。

这只是一个可以遵循的模式示例,不应被视为即插即用的实现。它需要进行调整以支持核心数据批量导入和保存。然而,它确实清楚地展示了一种获取字典或字典数组并将它们解码为对象的方法。然后,您如何处理您的对象完全取决于您。

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

关于ios - Swift - 核心数据播种类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31611235/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com