iOS - 如何更新 CoreDataModel 实体?

标签 ios swift xcode8

我想从我的应用程序更新我的实体(在 Swift 3 和 Xcode 8 中)。

所以我使用了核心数据模型。

这是我的 CoreDataController.swift 的代码:

// Updates a person
    func update(updateLuci: LuciKit){
        if let luci = getById(id: updateLuci.objectID){
            luci.descrizione = updateLuci.descrizione
            luci.pin_arduino = updateLuci.pin_arduino
        }
    }

    // Gets a person by id
    func getById(id: NSManagedObjectID) -> LuciKit? {
        return context.object(with: id) as? LuciKit
    }

在这种模式下我没有任何错误,但是如果我尝试重新启动我的应用程序我看不到更新信息。

最佳答案

试试这个我想你忘了保存上下文

func update(updateLuci: LuciKit){
        if let luci = getById(id: updateLuci.objectID){
            luci.descrizione = updateLuci.descrizione
            luci.pin_arduino = updateLuci.pin_arduino
            context.save()
        }
    }

关于iOS - 如何更新 CoreDataModel 实体?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43539853/

相关文章:

swift - 使用 Alamofire 快速上传图像和参数

ios - 如何检查 indexPath 是否有效,从而避免 "attempt to scroll to invalid index path"错误?

macos - Xcode 8 扩展在编辑器菜单中不可见

app-store-connect - 是什么让 NSCalendarsUsageDescription 成为必需?

swift - RxSwift 在没有主题或变量的情况下保持字符串的状态

ios - 后退按钮在 subview Controller 中不可见

ios - 即使在添加 setcontentsize 后,UIScrollview 也无法正常工作

iphone - 错误是什么意思?

ios - 使用 cellForRowAtIndexPath 从 UITableView 获取 UITableViewCell

ios - 如何确定 Google map 图 block 是否已加载