gpt4 book ai didi

ios - Swift/iOS Core Data - 后台线程保存大量记录

转载 作者:行者123 更新时间:2023-11-28 07:38:35 29 4
gpt4 key购买 nike

我正在开发一个 iOS 应用程序 (Swift),它通过 Web 服务获取大量数据(21000 条记录)(每个请求以 1000 条记录为一组)。在每个请求结束时,我需要将这 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

View Controller

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 卡住问题。我知道这可以通过使用后台线程或类似的东西来完成,但我仍然无法找到任何解决方案。任何帮助将不胜感激。谢谢

最佳答案

谢谢大家的建议。我设法解决了这个问题。在下面发布我的代码以防其他人需要它。

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

}

}

}

关于ios - Swift/iOS Core Data - 后台线程保存大量记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52829048/

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