Swift / iOS核心数据-在后台线程中保存大量记录

时间:2018-10-16 06:19:50

标签: ios swift core-data

我正在开发一个iOS应用程序(Swift),该应用程序通过Web服务(每个请求1000条记录的块)获取大量数据(21000条记录)。在每个请求的结尾,我需要将这1000条记录存储在核心数据中。这是我到目前为止所做的:

AppDelegate

// MARK: - Core Data stack

lazy var persistentContainer: NSPersistentContainer = {
    /*
     The persistent container for the application. This implementation
     creates and returns a container, having loaded the store for the
     application to it. This property is optional since there are legitimate
     error conditions that could cause the creation of the store to fail.
    */
    let container = NSPersistentContainer(name: "ABC")
    container.loadPersistentStores(completionHandler: { (storeDescription, error) in
        if let error = error as NSError? {
            // Replace this implementation with code to handle the error appropriately.
            // fatalError() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.

            /*
             Typical reasons for an error here include:
             * The parent directory does not exist, cannot be created, or disallows writing.
             * The persistent store is not accessible, due to permissions or data protection when the device is locked.
             * The device is out of space.
             * The store could not be migrated to the current model version.
             Check the error message to determine what the actual problem was.
             */
            fatalError("Unresolved error \(error), \(error.userInfo)")
        }
    })
    return container
}()

// MARK: - Core Data Saving support

func saveContext () {
    let context = persistentContainer.viewContext
    if context.hasChanges {
        do {
            try context.save()
        } catch {
            // Replace this implementation with code to handle the error appropriately.
            // fatalError() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.
            let nserror = error as NSError
            fatalError("Unresolved error \(nserror), \(nserror.userInfo)")
        }
    }
}

全局变量(在AppDelegate的末尾)

let global_appDelegate = UIApplication.shared.delegate as! AppDelegate
let global_context = global_appDelegate.persistentContainer.viewContext

ViewController

func downloadMedicines( offset: Int64 )
    {
        let total_drugs_count = UserDefaults.standard.integer(forKey: "drug_count")

        var dCount: Int64 = offset

        ClassNetworkService.request_data(TagText: "Medicines", myURL: "&resource=meds", myPostParam: "dcount=\(dCount)&token=\(UserDefaults.standard.getToken())", showAlert: nil) { ( data, thread_error_title, thread_error_message ) in

            DispatchQueue.main.async {

                print("now its ----> M E D I C I N E S -@- \(dCount)")

                if ( thread_error_title == "" )
                {
                    if let _d_count = data["dcount"] as? Int64
                    {
                        dCount = _d_count
                    }

                    if let _data = data["data"] as? NSArray
                    {
                        for tmp_data in _data
                        {
                            if let tmp_data_dictionary = tmp_data as? NSDictionary
                            {
                                let table_medicine      = Medicine(context: global_context)

                                table_medicine.id                  = Int64(tmp_data_dictionary["mID"] as! String)!
                                table_medicine.supplier            = (tmp_data_dictionary["supplier"] as! String)
                                table_medicine.displayNdc          = (tmp_data_dictionary["display_ndc"] as! String)
                                table_medicine.medispanGpi         = (tmp_data_dictionary["medispan_gpi"] as! String)
                                table_medicine.medicationName      = (tmp_data_dictionary["selldescription"] as! String)
                                table_medicine.genericTherapClass  = (tmp_data_dictionary["generic_therap_class"] as! String)
                                table_medicine.ahfsTherapClass     = (tmp_data_dictionary["ahfs_therap_class"] as! String)
                                table_medicine.keyword             = (tmp_data_dictionary["keyword"] as! String)
                                table_medicine.memberNumber        = Int64(tmp_data_dictionary["member_number"] as! String)!
                                table_medicine.notes               = (tmp_data_dictionary["notes"] as! String)
                                table_medicine.pricePerUnit        = Double(tmp_data_dictionary["price_per_unit"] as! String)!
                                table_medicine.drugOrder           = Int64(tmp_data_dictionary["drug_order"] as! String)!
                                table_medicine.displayedStrength   = (tmp_data_dictionary["displayed_strength"] as! String)
                                table_medicine.displayUnits        = (tmp_data_dictionary["display_units"] as! String)
                                table_medicine.expDate             = (tmp_data_dictionary["exp_date"] as! String)
                                table_medicine.soldUnits           = (tmp_data_dictionary["sold_units"] as! String)
                                table_medicine.soldUnitsPlural     = (tmp_data_dictionary["sold_units_p"] as! String)
                                table_medicine.pkgQty              = (tmp_data_dictionary["pkg_qty"] as! String)
                                table_medicine.genericInd          = (tmp_data_dictionary["generic_ind"] as! String)
                                table_medicine.defaultQty          = Int64(tmp_data_dictionary["default_qty"] as! String)!

                                global_appDelegate.saveContext()
                            }

                        }

                    }

                    // download and sync more medicines here

                    let request_medicine = NSFetchRequest<NSFetchRequestResult>(entityName: "Medicine")

                    do
                    {
                        let all_medicine =  try global_context.fetch(request_medicine)

                        if ( all_medicine.count < total_drugs_count ) // total_drugs_count
                        {
                            self.downloadMedicines( offset: dCount )
                        }
                        else
                        {
                            // syncing complete
                        }
                    }
                    catch
                    {
                        print (error)
                    }
                }

            }
        }

    }

只要正在处理我的Web服务,我的UI就会保持平稳,但是一旦执行数据保存逻辑,我的UI就会冻结。我想摆脱这个UI冻结问题。我知道可以通过使用后台线程或类似的方法来完成,但是我仍然无法找到任何解决方案。任何帮助将不胜感激。谢谢

1 个答案:

答案 0 :(得分:0)

谢谢大家的建议。我设法解决了这个问题。如果有人需要我的代码,请在下面发布。

    // Creates a task with a new background context created on the fly

    global_appDelegate.persistentContainer.performBackgroundTask { (context) in

        for tmp_data in _data
        {
            if let tmp_data_dictionary = tmp_data as? NSDictionary
            {
                let table_medicine      = Medicine(context: context)

                table_medicine.id                  = Int64(tmp_data_dictionary["mID"] as! String)!
                table_medicine.supplier            = (tmp_data_dictionary["supplier"] as! String)
                table_medicine.displayNdc          = (tmp_data_dictionary["display_ndc"] as! String)
                table_medicine.medispanGpi         = (tmp_data_dictionary["medispan_gpi"] as! String)
                table_medicine.medicationName      = (tmp_data_dictionary["selldescription"] as! String)
                table_medicine.genericTherapClass  = (tmp_data_dictionary["generic_therap_class"] as! String)
                table_medicine.ahfsTherapClass     = (tmp_data_dictionary["ahfs_therap_class"] as! String)
                table_medicine.keyword             = (tmp_data_dictionary["keyword"] as! String)
                table_medicine.memberNumber        = Int64(tmp_data_dictionary["member_number"] as! String)!
                table_medicine.notes               = (tmp_data_dictionary["notes"] as! String)
                table_medicine.pricePerUnit        = Double(tmp_data_dictionary["price_per_unit"] as! String)!
                table_medicine.drugOrder           = Int64(tmp_data_dictionary["drug_order"] as! String)!
                table_medicine.displayedStrength   = (tmp_data_dictionary["displayed_strength"] as! String)
                table_medicine.displayUnits        = (tmp_data_dictionary["display_units"] as! String)
                table_medicine.expDate             = (tmp_data_dictionary["exp_date"] as! String)
                table_medicine.soldUnits           = (tmp_data_dictionary["sold_units"] as! String)
                table_medicine.soldUnitsPlural     = (tmp_data_dictionary["sold_units_p"] as! String)
                table_medicine.pkgQty              = (tmp_data_dictionary["pkg_qty"] as! String)
                table_medicine.genericInd          = (tmp_data_dictionary["generic_ind"] as! String)
                table_medicine.defaultQty          = Int64(tmp_data_dictionary["default_qty"] as! String)!

                if let tmp_insurances              = tmp_data_dictionary["insurance"] as? NSArray
                {
                    do
                    {
                        let jsonData = try JSONSerialization.data(withJSONObject: tmp_insurances, options: JSONSerialization.WritingOptions.prettyPrinted)

                        if let JSONString       = String(data: jsonData, encoding: String.Encoding.utf8)
                        {
                            table_medicine.insurance = JSONString
                        }
                    }
                    catch
                    {
                        print(error)
                    }
                }

                //global_appDelegate.saveContext()
                do {
                    try context.save()
                } catch {
                    fatalError("Failure to save context: \(error)")
                }

            }

        }

    }