如何使用Swift在Realm中插入许多记录?

时间:2017-02-09 23:32:04

标签: swift realm

我正在尝试在Realm中执行大量数据插入,但这使得它非常慢。

这是我的代码:

let realm = try! Realm()


let group = GroupRealm()

group.id = 1
group.name = "test"

try! realm.write { 
    realm.add(group)
}

var product = [String:AnyObject]()
product["id"] = "" as AnyObject!
product["code"] = ("") as AnyObject!
product["detail"] = ("") as AnyObject!
product["onHand"] = (0) as AnyObject!
product["price"] = (0) as AnyObject!
product["isSerialized"] = (0) as AnyObject!
product["isLotNumber"] = (0) as AnyObject!

var groupProduct = [String:AnyObject]()
groupProduct["group"] = group as AnyObject!

for r in 0..<300 {
    realm.beginWrite()
    for i in 0..<100 {
        product["id"] = "\(i)-\(r)" as AnyObject!
        realm.create(ProductRealm.self, value: product, update: true)

        groupProduct["id"] = "\(i)-\(r)" as AnyObject!
        groupProduct["product"] = product as AnyObject!
        realm.create(GroupProductRealm.self, value: groupProduct, update: true)
    }
    try! realm.commitWrite()
}

物件:

class ProductRealm: Object {
    public dynamic var id: String = ""
    public dynamic var code: String = ""
    public dynamic var detail: String = ""
    public dynamic var onHand: Int = 0
    public dynamic var price: Double = 0
    public dynamic var isSerialized: Int = 0
    public dynamic var isLotNumber: Int = 0
    override static func primaryKey() -> String? {
        return "id"
    }
}

class GroupProductRealm: Object {
    public dynamic var id = ""
    public dynamic var group: GroupRealm!
    public dynamic var product: ProductRealm!
    override static func primaryKey() -> String? {
        return "id"
    }
}
class GroupRealm: Object {
    public dynamic var id = 1
    public dynamic var name = ""
    public dynamic var update = Date()
    override static func primaryKey() -> String? {
        return "id"
    }
}

我的iphone 5s上插入30000条记录的测试延迟超过30秒,而在一个旧项目中我使用sqlite需要大约16秒。

我已经读过,这个领域比sqlite要快得多,所以我知道这是我的错误,但是我没有给D:

抱歉我的英文。

1 个答案:

答案 0 :(得分:0)

我的英语水平不是很高,所以我会用西班牙语发表评论代码,如果有人可以将其修改成英文我会很感激。

https://es.stackoverflow.com/questions/48676/como-insertar-muchos-registros-en-realm-con-swift/48913#48913

//
//  ViewController.swift
//  TestRealm
//
//  Created by Sergio Castro on 10/02/17.
//  Copyright © 2017 BasTechSoluctions. All rights reserved.
//

import UIKit
import RealmSwift

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        // Mostrar la ubicación del proyecto para ubicar la base de datos
        print(FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first!.absoluteString)


        DispatchQueue(label: "background").async { //Abrir un nuevo hilo para insertar de forma asíncrona
            autoreleasepool {

                let date1 = Date() // Inicia el conteo del tiempo

                let realm = try! Realm() // Instanciar realm en el hilo nuevo

                let products = realm.objects(ProductRealm.self) //Crear una lista de todos los productos

                //Crear grupo
                let group = GroupRealm()
                group.id = 1
                group.name = "test"

                try! realm.write { //Insertar/Reemplazar grupo, sin productos
                    realm.create(GroupRealm.self, value: group, update: true)
                }

                ///Crear json
                //Primero creo un String, donde ingreso todos los datos en formato [{},{},...]
                var stringJson = "["
                for i in 0..<30000 {
                    if(i > 0){ stringJson.append(",") }
                    let dataOrigin = "{\"id\": \"\(i)\", \"code\": \"\", \"detail\": \"\", \"onHand\": 0, \"price\": 0, \"isSerialized\": 0, \"isLotNumber\": 0}"
                    stringJson.append("\(dataOrigin)")
                }
                stringJson.append("]")

                //Convertir el String en Datos y luego pasarlo a jsonObject (esto para simular el proceso de descarga de datos)
                let data = stringJson.data(using: .utf8)!
                let jsonP = try! JSONSerialization.jsonObject(with: data, options: .allowFragments) as! [Any]

                //Mostrar el tiempo empleado para la creación del json en consola, el tiempo está en segundos por defecto
                let date2 = Date()
                var time = date2.timeIntervalSince(date1)
                print("Json creation in seconds \(time)")

                //Recorrer el json en grupos de 100, tantas veces haga falta para recorrer por completo el json, en este caso 300 veces
                let limit = 100
                for c in 0..<(jsonP.count / limit){
                    //Insertar/Reemplazar los grupos de datos
                    realm.beginWrite()
                    for i in 0..<limit{
                        let row = jsonP[i + (c*limit)]
                        realm.create(ProductRealm.self, value: row, update: true)
                    }
                    try! realm.commitWrite()
                    //Mostrar en consola la cantidad de procesos realizados vs la cantidad de registros actuales
                    ///print("\((c+1)*limit) / \(products.count)") //Este proceso aumenta el tiempo de cálculo
                    print("\((c+1)*limit)") //Este proceso no afecta mucho el tiempo de cálculo
                }

                group.products.append(objectsIn: products) //Agregar los productos al grupo, aquí se pueden poner filtros

                //Actualizar el grupo, para que el array products tenga una referencia a sus productos correspondientes
                try! realm.write {
                    realm.create(GroupRealm.self, value: group, update: true)
                }

                // Calcular el tiempo de inserción y mostrarlo en consola
                let date3 = Date()
                time = date3.timeIntervalSince(date2)
                print("Finish time in seconds \(time)")

            }
        }
    }
}


class ProductRealm: Object {
    public dynamic var id: String = ""
    public dynamic var code: String = ""
    public dynamic var detail: String = ""
    public dynamic var onHand: Int = 0
    public dynamic var price: Double = 0
    public dynamic var isSerialized: Int = 0
    public dynamic var isLotNumber: Int = 0
    override static func primaryKey() -> String? {
        return "id"
    }
}

class GroupRealm: Object {
    public dynamic var id = 1
    public dynamic var name = ""
    public dynamic var update = Date()
    let products = List<ProductRealm>()
    override static func primaryKey() -> String? {
        return "id"
    }
}