- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我正在学习有关创建大型类以处理用数据为大型数据库播种的当前教程。
http://www.andrewcbancroft.com/2015/02/25/using-swift-to-seed-a-core-data-database/
我的数据库是使用 JSON 填充的,尽管我想复制作者在上述文章中使用的模式。
在文章中,他提到这种方法违反了一次性使用责任。我知道类应该承担单一责任,但考虑到像我这样的情况,例如在用户登录时我需要播种相当大的数据集,是否有另一种方法可以采用?
如果这引起了讨论,我深表歉意,这不是我的本意,我的问题是这种播种方式在生产中是否司空见惯,如果不是,实现这种数据播种的最佳模式是什么。
最佳答案
我认为不可能真正回答每个人如何在生产中导入数据,因为每个人都可以做不同的事情。
相反,我只想提一下,根据 Apple 的“核心数据编程指南”,导入数据的最有效方法是通过批量导入过程。此处详细介绍了此过程。
话虽如此,我会将您的数据存储在一个 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/
从 PHP7 开始,为 PRNG 引入了一个新函数:random_int ( http://php.net/manual/en/function.random-int.php ) PHP 手册中没有与
在 .net 核心项目中,我像这样在 Program.cs 文件中播种: var host = BuildWebHost(args); using (var scope = host.Services
我有一张谷歌地图,上面有大约 200 个标记。使用谷歌距离矩阵服务,我可以找到从一个地址到 map 上所有标记的行驶距离。由于 API 限制,我每次调用只能提交 25 个目的地,因此我必须将操作分解为
下面的脚本抛出错误(自定义字段未定义)。我需要以不同的方式传递元素 ID 吗? 我正在尝试使用我要计算的表单字段来为数组播种。它应该迭代数组中的每个表单字段,并用表单元素的值递增 sum 变量。 jQ
我正在学习“Laravel 5 Essentials”中的教程。当我尝试使用命令为我的数据库播种时 php artisan db:seed 我收到错误 [ReflectionException]
我正在关注 docs为 users 表设置种子,该表显示正在使用 User::create class UserTableSeeder extends Seeder { public func
让我首先说明我要完成的任务: 我需要在一定范围内随机生成一组数字 我希望这些数字稍微均匀分布 我需要能够为随机数生成播种,这样,给定一个种子,生成的随机数将始终相同。 在对 drand48()、ran
这个问题在这里已经有了答案: Recommended way to initialize srand? (15 个答案) 关闭 9 年前。 我学习的方法是最初使用 srand(time(NULL))
SQLite 是否支持播种 RANDOM() 的功能与 MySQL 对 RAND() 的处理方式相同? $query = "SELECT * FROM table ORDER BY RAND(" .
我正在使用不支持的 Visual Studio 2010 ,所以我必须播种 default_random_engine .因此,我决定用 rand 播种它如下 srand((unsigned int
在 google OR-tools 库中,“原始”CP-Solver(此处讨论: https://developers.google.com/optimization/cp/original_cp_s
我正在尝试为 AspNetRole 表设置初始系统角色。 播种扩展: public static void EnsureRolesAreCreated(this IApplicationBuilder
我似乎无法弄清楚如何使用 Sequelize 为 ARRAY(ENUM) 播种。当我通过我的应用程序注册用户时,我可以很好地创建一个新用户,但是当我在种子文件中使用 queryInterface.bu
以下代码应创建两个具有相同种子的 Random 对象: System.out.println("System time before: " + System.currentTimeMillis());
尝试从集合中选择伪随机元素时,我看到了非确定性行为,即使 RNG 已播种(示例代码如下所示)。为什么会发生这种情况,我是否应该期望其他 Python 数据类型表现出类似的行为? 注意:我只在 Pyth
关于在 openssl/bn.h 中使用 BN_generate_prime 生成质数的内容,我无法找到答案。另外,我将如何播种此函数使用的任何 PRNG? 单独的问题但与我的代码相关(我正在编写一个
所以,我是 MEAN 堆栈的新手,我在尝试播种 MongoDB 时碰壁了。我正在使用 Mongoose 与数据库进行通信,并且有一堆文档建议我应该能够使用填充的 JSON 文件进行播种。 我尝试过的:
我有一个非常简单的情况:我想使用 testcontainers 测试 AWS 中现有的 mysql 数据库。 我遵循了官方指南( https://www.testcontainers.org/modu
我有一个很长(500K+ 行)的两列电子表格,如下所示: Name Code 1234 A 1234 B 1456 C 4556 A 4556 B 4556
我有一个要播种的数据透视表。除了 PK 和 FK,该表还包含另外两列:Arrival & Departure(类型:时间戳)。我正在使用 Carbon 随机填充前面的列。这是我的代码: $faker
我是一名优秀的程序员,十分优秀!