Когда в моем приложении нажата кнопка «Назад», вызывается делегат. Все функции работают правильно, однако между переходом вида существует большая задержка. Это дает пользователю ощущение, что приложение не работает или работает неправильно.
Здесь показаны методы делегата, которые запускаются и вызывают задержку.
func setValues(obj: overviewProps){
overview = obj
print("Values Set")
surveyToEdit.overview = obj
updateSurveyObj()
}
func setArray(obj: [areaProps]) {
areas = obj
print("Set Array Sucessfully")
print(areas.count)
surveyToEdit.areas = obj
updateSurveyObj()
}
func saveSurveys(x : addedSurveys){
let isSuccessfulSave = NSKeyedArchiver.archiveRootObject(x, toFile: addedSurveys.ArchiveURL.path)
if isSuccessfulSave {
os_log("Surveys Saved", log: OSLog.default, type: .debug)
} else {
os_log("Failed to Save", log: OSLog.default, type: .error)
}
}
func updateSurveyObj(){
//this must be the issue
surveyToEdit.name = surveyToEdit.overview?.name ?? surveyToEdit.name
surveyToEdit.areas! = areas
surveyToEdit.overview! = overview
surveyListSurveys.surveys[surveyIndex] = surveyToEdit
saveSurveys(x: surveyListSurveys)
delegate?.saveInfo(updatedArray: surveyListSurveys)
print("the name is : \(surveyToEdit.overview!.name)")
}
При подключении к отладчику устройство показывает высокую загрузку памяти и процессор в течение этой задержки.
Вот весь контроллер вида для обзора проекта:
import UIKit
import os.log
class projectOverview: UIViewController, OverviewDelegate, AreaDelegate {
var surveyToEdit = survey(areas: nil, settings: nil, overview: nil, name: "New Survey")
let IMG = UIImage(named: "noImage.png")
var areas = [areaProps(x: "New Area", y: "", z: "", a: "", b: "", p1: nil, p2: nil, p3: nil), areaProps(x: "New Area", y: "", z: "", a: "", b: "", p1: nil, p2: nil, p3: nil)]
var settings = companySettings(manager: "")
var overview = overviewProps(x: "", y: "", a: "", b: "", c: "", d: "", e: "", f: "", st: "", ct: "", stt: "")
var projectTitle: String = ""
let areaView = areaDetails()
let surveyHomeScreen = surveyList()
var surveyIndex = 0
var surveyListSurveys = addedSurveys(survey: [survey]())
var delegate:projectDelegate?
// need this to eventually pass back the data in the the survey object
func saveSurveys(x : addedSurveys){
let isSuccessfulSave = NSKeyedArchiver.archiveRootObject(x, toFile: addedSurveys.ArchiveURL.path)
if isSuccessfulSave {
os_log("Surveys Saved", log: OSLog.default, type: .debug)
} else {
os_log("Failed to Save", log: OSLog.default, type: .error)
}
}
func setProjDetails(){
print("gay")
if surveyToEdit.areas != nil{
areas = surveyToEdit.areas!
print("There is area")
}
if surveyToEdit.overview != nil{
overview = surveyToEdit.overview!
print("there is overview")
}
}
func updateSurveyObj(){
//this must be the issue
surveyToEdit.name = surveyToEdit.overview?.name ?? surveyToEdit.name
surveyToEdit.areas! = areas
surveyToEdit.overview! = overview
surveyListSurveys.surveys[surveyIndex] = surveyToEdit
saveSurveys(x: surveyListSurveys)
delegate?.saveInfo(updatedArray: surveyListSurveys)
print("the name is : \(surveyToEdit.overview!.name)")
}
let overviewController = addOverview()
override func viewDidLoad() {
super.viewDidLoad()
//areas = surveyToEdit.areas!
//overview = surveyToEdit.overview!
// Do any additional setup after loading the view.
//overviewController.delegate = self
//areaView.delegate = self
}
// MARK : - Actions
@IBAction func showPDF(_ sender: Any) {
performSegue(withIdentifier: "goToShare", sender: self)
print(overview.name)
}
@IBAction func editAreas(_ sender: Any) {
performSegue(withIdentifier: "goToServiceArea", sender: self)
}
@IBAction func editOverView(_ sender: Any) {
performSegue(withIdentifier: "goToMainDetails", sender: self)
}
func setValues(obj: overviewProps){
overview = obj
print("Values Set")
surveyToEdit.overview = obj
updateSurveyObj()
}
// MARK: - Navigation
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "goToMainDetails"{
let vc = segue.destination as! addOverview
vc.overview = overview
vc.delegate = self
} else if segue.identifier == "goToServiceArea"{ // this is the segue
let vc = segue.destination as! areaDetails
vc.areaArray = self.areas
vc.delegate = self
print(" \(areas.count) Areas in Array ") // the array contains only 1 object
} else {
let vc = segue.destination as! shareReport
vc.surveyToDisplay = surveyToEdit
}
}
override func willMove(toParent parent: UIViewController?) {
super.willMove(toParent:parent)
if parent == nil {
updateSurveyObj()
}
}
func setArray(obj: [areaProps]) {
areas = obj
print("Set Array Sucessfully")
print(areas.count)
surveyToEdit.areas = obj
updateSurveyObj()
}
@IBOutlet var floorPlanIMG: UIImageView!
а вот вид, на котором нажимается кнопка «назад»:
import UIKit
class addOverview: UIViewController, UIPickerViewDelegate, UIPickerViewDataSource {
var Overview = OverviewProps (x: "", y: "", a: "", b: "", c: "", d: "", e: "", f: "", st: "", ct: "", stt: "")
// MARK: - Init all UIVariables
let buildingTypes = ["Industrial", "School", "Office", "Multi-Tenant", "Church", "Manufacturing", "Misc"]
let cleaningFreqs = ["1/Week", "2/Week", "3/Week", "4/Week", "5/Week", "6/Week", "7/Week", "1/EOW", "1/Month"]
@IBOutlet var datePicker: UIDatePicker!
@IBOutlet var buildingName: UITextField!
@IBOutlet var street: UITextField!
@IBOutlet var city: UITextField!
@IBOutlet var State: UITextField!
@IBOutlet var contactName: UITextField!
@IBOutlet var phoneNumber: UITextField!
@IBOutlet var email: UITextField!
@IBOutlet var buildingPicker: UIPickerView!
@IBOutlet var cleaningFreqPicker: UIPickerView!
@IBOutlet var sqFt: UITextField!
var delegate:overviewDelegate?
// MARK: - Conform to PickerView
func numberOfComponents(in pickerView: UIPickerView) -> Int {
return 1
}
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
if pickerView.tag == 2
{
return buildingTypes.count
}
else{
return cleaningFreqs.count
}
}
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
if (pickerView.tag == 2)
{
return buildingTypes[row]
}else{
return cleaningFreqs[row]
}
}
override func viewDidLoad() {
super.viewDidLoad()
updateView()
buildingPicker.delegate = self
cleaningFreqPicker.delegate = self
// Do any additional setup after loading the view.
}
override func viewWillAppear(_ animated: Bool) {
if overview.building != ""{
buildingPicker.selectRow(buildingTypes.index(of: overview.building)!, inComponent: 0, animated: false)
cleaningFreqPicker.selectRow(cleaningFreqs.index(of: overview.freq)!, inComponent: 0, animated: false)
}
}
// set all the values of the object
func updateView(){
buildingName.text = overview.name
city.text = overview.city
contactName.text = overview.contact
email.text = overview.email
phoneNumber.text = overview.phone
sqFt.text = overview.sft
State.text = overview.state
street.text = overview.street
}
func setValues(){
overview.name = buildingName.text!
overview.city = city.text!
overview.contact = contactName.text!
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "MM-dd-yyyy"
overview.date = dateFormatter.string(from: datePicker.date)
overview.email = email.text!
overview.building = buildingTypes[buildingPicker.selectedRow(inComponent: 0)]
overview.freq = cleaningFreqs[cleaningFreqPicker.selectedRow(inComponent: 0)]
overview.phone = phoneNumber.text!
overview.sft = sqFt.text!
overview.state = State.text!
overview.street = street.text!
}
// MARK: - Navigation
override func willMove(toParent parent: UIViewController?) {
super.willMove(toParent:parent)
if parent == nil {
setValues()
print("Back Button Pressed")
delegate?.setValues(obj: overview)
}
}
// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
// Get the new view controller using segue.destination.
// Pass the selected object to the new view controller.
}
}