【问题标题】:core Data deletes data after restarting the applicationcore Data 重启应用后删除数据
【发布时间】:2019-08-04 07:05:12
【问题描述】:

我改变了我的结构并做了一个迁移(我创建了一个新的模型),添加并显示了数据,一切正常,但是当我重新加载应用程序时,核心数据为空,这是什么问题?

  static func saveToCoreData(_ words: Words?){

    DispatchQueue.main.async {

        coreDataResult(data: [words?.word, words?.translation], completion: { (result, taskObject, context) in
            do {
                let fetchedEntities = try context.fetch(result) as! [Favorite]
                if let _ = fetchedEntities.first?.word {
                    print("the element already have in coreData")
                } else {

                    taskObject?.setValue(words?.word, forKey: "word")
                    taskObject?.setValue(words?.translation, forKey: "translation")
                    taskObject?.setValue(words?.descript, forKey: "wordDesc")
                    taskObject?.setValue(words?.translDesc, forKey: "translDesc")

                    do {
                        try context.save()
                    } catch {
                        print(error)
                    }
                }
            } catch {}
        })

    }

}

static func coreDataResult(data: [[String?]?]?, completion: @escaping (NSFetchRequest<NSFetchRequestResult>, Favorite?, NSManagedObjectContext) -> ()){

    guard let w = data?.first, let word = w, let t = data?.last, let transl = t else { return }

    DispatchQueue.main.async {
        let appDelegate = UIApplication.shared.delegate as! AppDelegate
        let context = appDelegate.persistentContainer.viewContext
        guard let entity = NSEntityDescription.entity(forEntityName: "Favorite", in: context) else { return }
        guard let taskObject = NSManagedObject(entity: entity, insertInto: context) as? Favorite else { return }

        let predicate = NSPredicate(format: "word == %@", word)
        let predicate2 = NSPredicate(format: "translation == %@", transl)
        let fetchRequest = NSFetchRequest<NSFetchRequestResult>(entityName: "Favorite")
        let andPredicate = NSCompoundPredicate(type: .and, subpredicates: [predicate, predicate2])

        fetchRequest.predicate = andPredicate
        completion(fetchRequest, taskObject, context)
    }

}

旧属性

新属性

新模型

【问题讨论】:

  • 您将属性“word”的数据类型从 String 更改为 Transformable。轻量级迁移无法自动执行此操作。您需要手动执行此操作。
  • 您需要根据外观编写迁移策略

标签: ios arrays swift core-data migration


【解决方案1】:

我决定了

这个答案帮助了我 - https://stackoverflow.com/a/40662940/8070211

  1. 创建模型(command + n -> 映射模型 -> 选择旧模型 -> 选择新模型 -> 添加名称并创建)
  2. 创建子类 NSEntityMigrationPolicy

    class FavoriteEntityMigrationPolicy: NSEntityMigrationPolicy {
    
       @objc func typeFor(word:String) -> [String] {
           return [word]
       }
    
    }
    
  3. 选择模型 -> 选择实体 -> 将子类添加到自定义策略

  4. 选择模型 -> 选择实体 -> 选择值表达式 -> 添加 @987654329 @

  5. 准备好了

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-10-13
    • 1970-01-01
    • 1970-01-01
    • 2023-03-15
    • 2013-08-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多