2017-05-05 77 views
0

首先,讓我解釋一下我的應用程序及其流程。應用程序打開,用戶創建配置文件(通過Core Data存儲所有數據)。在用戶點擊創建之後,它將它們發送到控制檯屏幕(其顯示用戶輸入的部分信息,例如通過搜索的名稱)。 Theres一個標籤,讓他們編輯他們的個人資料(姓名,體重,地址等)。當用戶編輯他們的信息(改變他們的名字,體重等)時,它也應該更新控制檯頁面上顯示的信息。Swift:保存(全部寫入)&加載CoreData

我已經得到的數據保存和加載。我遇到的問題是當試圖從編輯配置文件屏幕編輯數據時...用戶更改字段中的文本,然後單擊保存。出於某種原因,數據不會保存......至少我認爲這是問題所在。當按下「保存」按鈕時,無論輸入什麼文本,文本字段都會返回到用戶最初在「創建配置文件」屏幕上輸入的內容。

下面的代碼...

Person.swift

// This struct would to get the data from the user 
struct PInfo { 
var firstName: String? 
var lastName: String? 
var cityState: String? 
var streetAddress: String? 
var gender: String? 
var weight: NSNumber? 
var phoneNumber: String? 
var contactName: String? 
var contactPhone: String?  
} 

func save(withPersonInfo p: PInfo, withContext context: NSManagedObjectContext) { 
    let entityDescription = NSEntityDescription.entity(forEntityName: "Person", in: context) 
    let managedObject = NSManagedObject(entity: entityDescription!, insertInto: context) as! Person 

    managedObject.firstName = p.firstName 
    managedObject.lastName = p.lastName 
    managedObject.cityState = p.cityState 
    managedObject.streetAddress = p.streetAddress 
    managedObject.gender = p.gender 
    managedObject.weight = p.weight as! Int16 
    managedObject.phoneNumber = p.phoneNumber 
    managedObject.contactName = p.contactName 
    managedObject.contactPhone = p.contactPhone 

    do { 
     try context.save() 
     print("Saved Successful") 
    } 
    catch let error as NSError { 
     print("Could not save. \(error), \(error.userInfo)") 
    } 

} 

func fetchSingleUser(withContext context:NSManagedObjectContext) -> PInfo { 

    let request: NSFetchRequest<Person> = Person.fetchRequest() 
    let coreData_items = try? context.fetch(request) 

    guard let items = coreData_items, 
     let firstItem = items.first 
     else { 
      fatalError("Error while querying") } 
    print("Loaded CoreData Items: \(firstItem)") 

    return PInfo(firstName: firstItem.firstName!, lastName:firstItem.lastName!, cityState: firstItem.cityState!, streetAddress: firstItem.streetAddress!, gender: firstItem.gender!, weight: firstItem.weight as NSNumber, phoneNumber: firstItem.phoneNumber!, contactName: firstItem.contactName, contactPhone: firstItem.contactPhone) 
} 

func userDataExists(withContext context: NSManagedObjectContext) -> Bool { 
    let request: NSFetchRequest<Person> = Person.fetchRequest() 
    let coreData_items = try? context.fetch(request) 

    guard let items = coreData_items, 
     let _ = items.first 
     else { 
      return false } 

    return true 
} 

EditProfileViewController.swift

@IBAction func saveButton(_ sender: UIButton) { 
    //Save to CoreData 
    saveUsersInfo() 

    alertPopup(title: "Saved!", message: "Your information has been updated!") 
    updateTextFields() 
} 

    func updateTextFields() { 
     guard let appDelegate = 
      UIApplication.shared.delegate as? AppDelegate else { 
       return 
     } 
     let managedContext = appDelegate.persistentContainer.viewContext 
     let userInformation = fetchSingleUser(withContext: managedContext) 

     //Set UI Text Fields with Users Data 
     firstNameField.text = userInformation.firstName! 
     lastNameField.text = userInformation.lastName! 
     weightInputField.text = String(describing: userInformation.weight!) 
     genderInputField.text = userInformation.gender! 
     phoneNumberField.text = userInformation.phoneNumber! 
     streetAddressField.text = userInformation.streetAddress! 
     cityStateInputField.text = userInformation.cityState! 
     contactNameField.text = userInformation.contactName 
     contactPhoneNumberField.text = userInformation.contactPhone 
     print("Updated User Info Text Fields") 
    } 

     func saveUsersInfo() { 
     //Save to CoreData 
     guard let appDelegate = 
      UIApplication.shared.delegate as? AppDelegate else { 
       return 
     } 
     let managedContext = appDelegate.persistentContainer.viewContext 
     let userInfo = PInfo(firstName: firstNameField.text!, lastName: lastNameField.text!, cityState: cityStateInputField.text!, streetAddress: streetAddressField.text!, gender: genderInputField.text!, weight: Int16(weightInputField.text!)! as NSNumber, phoneNumber: phoneNumberField.text!, contactName: contactNameField.text!, contactPhone: contactPhoneNumberField.text!) 
     save(withPersonInfo: userInfo, withContext: managedContext) 
     print("User Info Saved") 

     updateTextFields() 

    } 
} 

我相信這是與儲蓄(由於調試)的問題,但我不太熟悉CoreData確切地知道問題是什麼。

任何幫助/信息非常感謝!

+0

你不需要'PInfo'結構 - 核心數據是一個對象持久化系統。使用'Person'(Xcode根據您的Enity自動爲您創建)而不是'NSManagedObject' – Paulw11

回答

1

我懷疑你的數據被保存。但是您每次都創建一個新對象,而不是更新現有對象的值。當你打電話給你save方法,這行:

let managedObject = NSManagedObject(entity: entityDescription!, insertInto: context) as! Person 

創建一個新的Person對象。而當你調用fetchSingleUser方法,您獲取所有Person對象:

let coreData_items = try? context.fetch(request) 

但隨後只用前這些項目:

let firstItem = items.first 

它發生的第一個項目是原Person對象,與原始值:因此textFields恢復到那些原始值。

如果您的應用程序應該只有一個Person對象,在你save方法改變save方法來獲取現有對象,並更新該實例的屬性值,例如:

var managedObject : Person 
let request: NSFetchRequest<Person> = Person.fetchRequest() 
let coreData_items = try? context.fetch(request) 
if let items = coreData_items { 
    if items.count > 0 { 
     managedObject = items.first 
    } else { 
     managedObject = NSManagedObject(entity: entityDescription!, insertInto: context) as! Person 
    } 
    managedObject.firstName = p.firstName 
    ... etc 
} else { 
    // coreData_items is nil, so some error handling here 
} 
+0

謝謝!現在一切都說得通了!我感謝幫助! – szady