Reputation: 5111
I am working on an iOS app (Swift) which fetches a huge amount of data (21000 records) through a web service (in chunks of 1000 records per request). And at the end of each request I need to store those 1000 records in core data. This is what I have done so far:
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)")
}
}
}
Global Variables (At the end of 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)
}
}
}
}
}
As long as my web service is being processed my UI stands smooth but as soon as data save logic executes my UI freezes. I want to get rid of this UI freeze problem. I know it can be done by using background threads or something like this but I am still unable to figure out any solution. Any help would be greatly appreciated. Thanks
Upvotes: 2
Views: 1128
Reputation: 5111
Thank you all for your suggestions. I managed to solve this issue. Posting my code below in case someone else needs it.
// 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)")
}
}
}
}
Upvotes: 2