gpt4 book ai didi

arrays - 从核心数据中获取数据

转载 作者:行者123 更新时间:2023-11-28 08:14:55 26 4
gpt4 key购买 nike

我是 Swift 的新手,正在开始学习 Core Data,但我遇到了一个问题。我无法从我的 Core Data 数据库中获取数据。我想在核心数据中使用 Firebase 下载出于某种原因,数据显示来自 Firebase 而不是来自数据库。如何从数据库中提取数据?

这是我的 Controller :

    class InfoProfileViewController: UIViewController {

@IBOutlet weak var imageProfile: UIImageView!

@IBOutlet weak var nickProfile: UILabel!

@IBOutlet weak var emailProfile: UILabel!

@IBOutlet weak var numberProfile: UILabel!

var userData: UserData?

var managedObjextContext:NSManagedObjectContext!

var infoProfile = [User]()

override func viewDidLoad() {
super.viewDidLoad()
self.userData = UserData(user: (FIRAuth.auth()?.currentUser)!)
managedObjextContext = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext
showAllUsers()
}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}

private func loadData(){
let presentRequest:NSFetchRequest<User> = User.fetchRequest()

do {
infoProfile = try managedObjextContext.fetch(presentRequest)
print()

}catch {
print("Could not load data from database \(error.localizedDescription)")
}
}

private func showAllUsers() {
FIRDatabase.database().reference().child(UserData.DRIVERS_LOCATION_KEY).observeSingleEvent(of: FIRDataEventType.value, with: { (snapshot) in

let userItem = User(context: self.managedObjextContext)

if self.emailProfile.text != ""{

userItem.name = (self.userData?.nick)!
userItem.number = (self.userData?.number)!
userItem.avataURL = (self.userData?.avatarUrl)!
userItem.email = FIRAuth.auth()?.currentUser?.email!

do {
try self.managedObjextContext.save()
self.loadData()
print("Good save")
}catch {
print("Could not save data \(error.localizedDescription)")
}
}
//this my problem
do {
for profil in self.infoProfile{
self.emailProfile.text = profil.email
print(profil.email as Any)
self.nickProfile.text = profil.name
self.numberProfile.text = profil.number
}
}

})
}
}

我的模型: enter image description here

最佳答案

Firebase 不只是填充您的 Core Data 数据库——您必须自己完成。

您可能想要研究更自动化的 Firebase 离线功能(并且不使用 CoreData):https://www.firebase.com/docs/ios/guide/offline-capabilities.html

关于arrays - 从核心数据中获取数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42841410/

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