代码之家  ›  专栏  ›  技术社区  ›  ICL1901

将数据插入核心数据模型的速度慢得令人无法接受

  •  0
  • ICL1901  · 技术社区  · 7 年前

    我需要每周能进口超过10万张唱片。数据是以csv文件的形式从web服务中获取的。下载它很快,就像把它转换成可用的形式一样。然而,将这些记录添加到模型中是可行的,但速度慢得令人无法接受——几乎一个小时!

    我意识到每次录完唱片我都在存钱。一定有更好的办法。

    请告诉我或给我另一个答案。这是我的工作代码。多谢。

    func loadDataBase() {
    
        for i in 1..<objectArray.count - 1 {
    
            let item: [String] = objectArray[i]
    
    
            s_stop_id = Int(item[0])
            s_stop_code = Int(item[1])
            s_stop_name = item[2]
    
            let mainDelegate = UIApplication.shared.delegate as! AppDelegate
            let context = mainDelegate.persistentContainer.viewContext
    
            let newResource = NSEntityDescription.insertNewObject(forEntityName: stopEntity, into: context)
    
            newResource.setValue(s_stop_id, forKey: "stop_id")
            newResource.setValue(s_stop_name, forKey: "stop_name")
            newResource.setValue(s_stop_code, forKey: "stop_code")
    
    
            do {
                try context.save()
    
            } catch let error as NSError  {
                print("Error While Saving Data: \(error.userInfo)")
            }
        }
    

    我在展示一些使用信息。我似乎在使用100%的CPU。在后台运行此进程是否可行?那么时机就不那么重要了。

    enter image description here

    1 回复  |  直到 7 年前
        1
  •  2
  •   Eduardo Leite    7 年前

    您可能应该实例化上下文并将其保存在for之外,它应该如下所示:

    func loadDataBase() {
    
        let mainDelegate = UIApplication.shared.delegate as! AppDelegate
        let context = mainDelegate.persistentContainer.viewContext
    
        for i in 1..<objectArray.count - 1 {
    
            let item: [String] = objectArray[i]
    
    
            s_stop_id = Int(item[0])
            s_stop_code = Int(item[1])
            s_stop_name = item[2]
    
    
    
            let newResource = NSEntityDescription.insertNewObject(forEntityName: stopEntity, into: context)
    
            newResource.setValue(s_stop_id, forKey: "stop_id")
            newResource.setValue(s_stop_name, forKey: "stop_name")
            newResource.setValue(s_stop_code, forKey: "stop_code")
        }
        do {
                try context.save()
    
            } catch let error as NSError  {
                print("Error While Saving Data: \(error.userInfo)")
            }
    }
    
        2
  •  2
  •   El Tomato    7 年前

    使用测试以下内容 autoreleasepool

    let mainDelegate = UIApplication.shared.delegate as! AppDelegate
    let context = appDelegate.persistentContainer.viewContext
    let newResource = NSEntityDescription.insertNewObject(forEntityName: stopEntity, into: context)
    
    for i in 1..<objectArray.count - 1 {
        autoreleasepool(invoking: { () -> () in
            let item: [String] = self.objectArray[i]
            s_stop_id = Int(item[0])
            s_stop_code = Int(item[1])
            s_stop_name = item[2]
            newResource.setValue(s_stop_id, forKey: "stop_id")
            newResource.setValue(s_stop_name, forKey: "stop_name")
            newResource.setValue(s_stop_code, forKey: "stop_code")
    
            do {
                try context.save()
            } catch let error as NSError  {
                print("Error While Saving Data: \(error.userInfo)")
            }    
        })
    }