Я работаю над приложением для iOS (Swift), которое извлекает огромный объем данных (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
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)
}
}
}
}
}
Пока мой веб-сервис обрабатывается, мой пользовательский интерфейс работает нормально, но как только логика сохранения данных выполняется, мой пользовательский интерфейс замирает.Я хочу избавиться от этой проблемы заморозки пользовательского интерфейса.Я знаю, что это можно сделать с помощью фоновых потоков или чего-то подобного, но я все еще не могу найти какое-либо решение.Любая помощь будет принята с благодарностью.Спасибо