gpt4 book ai didi

ios - 如何在 Swift 中使用来自不同 View Controller 的多个核心数据实体

转载 作者:搜寻专家 更新时间:2023-11-01 05:56:20 31 4
gpt4 key购买 nike

我想将来自两个 viewControllers 的数据存储到两个 CoreData 实体中。

我可以实现这一点,但是来自单个 viewController。通过使用以下代码。

import UIKit
import CoreData
class ViewController: UIViewController {

override func viewDidLoad()
{

let appDelegate = UIApplication.shared.delegate as! AppDelegate

let context = appDelegate.persistentContainer.viewContext

let phoneContactRequest = NSFetchRequest<NSFetchRequestResult>(entityName: "PhoneContacts")

phoneContactRequest.returnsObjectsAsFaults = false

let emailContactRequest = NSFetchRequest<NSFetchRequestResult>(entityName: "EmailContacts")

emailContactRequest.returnsObjectsAsFaults = false
/*
// Save to database

//object one
let phoneContactsObject = NSEntityDescription.insertNewObject(forEntityName: "PhoneContacts", into: context)

phoneContactsObject.setValue("John", forKey: "name")
phoneContactsObject.setValue("8335636532", forKey: "number")

//object two
let emailContactObject = NSEntityDescription.insertNewObject(forEntityName: "EmailContacts", into: context)
emailContactObject.setValue("Peter@gmail.com", forKey: "email")
emailContactObject.setValue("Peter", forKey: "emailname")
*/


do{

// Fetch from PhoneContacts entity
let phoneContactResults = try context.fetch(phoneContactRequest)

if phoneContactResults.count > 0
{
for phoneContactResult in phoneContactResults as! [NSManagedObject]
{
if let phoneContactName = phoneContactResult.value(forKey: "name")
{
print(phoneContactName)
}
if let phoneContactNumber = phoneContactResult.value(forKey: "number")
{
print(phoneContactNumber)
}
}
}
// Fetch from EmailContacts entity
let emailContactResults = try context.fetch(emailContactRequest)

if emailContactResults.count > 0
{
for emailContactResult in emailContactResults as! [NSManagedObject]
{
if let emailContactName = emailContactResult.value(forKey: "emailname")
{
print(emailContactName)
}
if let emailContactEmail = emailContactResult.value(forKey: "email")
{
print(emailContactEmail)
}
}
}
}
catch
{
}
}
}

正如您在上面的代码中看到的,我有两个 CoreData 实体 PhoneContactsEmailContacts。我可以将数据从单个 viewController 存储到两个实体中。它有效。

现在,我想要的是,如何将数据存储到 viewController 1 的 entity1(PhoneContacts) 和来自 entity2(EmailContacts) viewController 2 ?

我应该将 managedObjectContext 共享给第二个 viewController 吗?或者接受任何其他更好的解决方案。

我正在使用 Xcode 8.2、Swift 3.0

提前致谢。

最佳答案

为 CoreData 操作创建并使用单例类。

例如

class CoreDataHelper: NSObject
{
static let sharedInstance = CoreDataHelper()

func appDelegate()->AppDelegate
{
return UIApplication.sharedApplication().delegate as! AppDelegate
}

func managedObjectContext() -> NSManagedObjectContext
{
return self.appDelegate().managedObjectContext
}
func fetchPhoneContacts() -> [NSManagedObject]
{
// write code to fetch contacts from PhoneContacts entity
}
func fetchEmailContacts() -> [NSManagedObject]
{
// write code to fetch contacts from EmaliContacts entity
}
}

来自 viewController1

let phoneContatcs = CoreDataHelper.sharedInstance().fetchPhoneContacts()

来自 viewController2

let emailContatcs = CoreDataHelper.sharedInstance().fetchEmailContacts()

注意:我知道问题是针对 Swift3 的。但我写这个答案是为了让我们知道如何做到这一点。

关于ios - 如何在 Swift 中使用来自不同 View Controller 的多个核心数据实体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42291027/

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