CoreData返回空数据

时间:2019-03-15 09:08:00

标签: ios swift core-data nsfetchrequest

coreData在不应该包含任何内容的情况下返回空数据,即使您卸载了该应用程序并重新安装并向СoreData请求后,context.fetch也会返回该数据

获取Сore数据中的所有数据

func getMyLoadBook(){
    words.removeAll()

    let appDelegate = UIApplication.shared.delegate as! AppDelegate
    let context = appDelegate.persistentContainer.viewContext
    let fetchRequest:NSFetchRequest<Favorite> = Favorite.fetchRequest()
    fetchRequest.returnsObjectsAsFaults = false

    do {
        let result = try! context.fetch(fetchRequest)
        print(result)
        if result.isEmpty {
            emptyBookMark()
            return
        } else {
            tableView.isHidden = false
        }

        for data in result as [NSManagedObject] {
            if let _ = data.value(forKey: "word"){
                let initData = Words(word: (data.value(forKey: "word") as? [String]) ?? [""], wordDesc: (data.value(forKey: "wordDesc") as? [String]) ?? nil, translation: (data.value(forKey: "translation") as? [String]) ?? [""], translDesc: (data.value(forKey: "translDesc") as? [String]) ?? nil)
                words.append(initData)
            }
        }

    }

    tableView.reloadData()

}

我有这些功能,但是当我从coreData获取数据时不会调用它们

//创建路径并检查元素的存在

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)

    }

}

///从Сore数据中删除数据

static func deleteFromCoreData(data: [[String?]?]?){

    coreDataResult(data: data, completion: { (result, taskObject, context) in

        do {
            let fetchedEntities = try context.fetch(result) as! [Favorite]
            if let entityToDelete = fetchedEntities.first {
                context.delete(entityToDelete)
            }
            do {
                try context.save()
                if let data = getDataFromContext(result:fetchedEntities){
                    Analytics.logEvent("RemovedFavorite", parameters: ["word": data.0, "translation": data.1])
                    YMMYandexMetrica.reportEvent("RemovedFavorite", parameters: ["word": data.0, "translation": data.1], onFailure: nil)

                }
            } catch {
                print(error)
            }
        } catch { print(error) }

    })

}

///将数据添加到СoreData

static func saveWithModelToCoreData(_ 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 {
                print(error)
            }
        })

    }

}

这就是结果返回

[<Favorite: 0x283478500> (entity: Favorite; id: 0x281306ee0 <x-coredata:///Favorite/t722DD7F9-8DD7-4AC4-AA20-02324AB1B08713> ; data: {
translDesc = nil;
translation = nil;
word = nil;
wordDesc = nil;
})

enter image description here

1 个答案:

答案 0 :(得分:1)

似乎您正在使用一个简单的核心数据设置,其中所有读取和写入都在对viewContext的主线程上完成。对于不希望进行批量导入或拥有大量实体的简单应用程序,此设置非常合适。它应该简化了许多多线程问题,所以当您仅在主线程上运行所有内容时,为什么会有这么复杂的回调和DispatchQueue.main.async设置,这让我有些困惑。 (也许您正在计划使用更复杂的设置的未来?)。

无论如何,后果之一是,viewContext的任何更改都将在应用程序的整个生命期内显示在您的应用程序中,即使您不调用save也是如此。这是因为只有一个上下文-因此即使不保存它,它仍然被更改。

在方法coreDataResult中创建一个空对象,然后在saveWithModelToCoreData中将其设置为值并保存上下文,或者发现该上下文已经存在并且不采取进一步的操作。如果coreDataResult在后​​台上下文中返回就可以了。当背景上下文消失时,空对象将消失。问题是您正在写viewContext,因此上下文不会消失,并且对象会卡住。

如果应用程序立即退出,您将在下次启动时看不到它。但是,如果在之后的任何时间调用save,那么空对象也将被保存。

我建议不要创建对象,除非您已经知道要使用它们。我将进行重构,以便只有一个函数检查重复项,然后创建并设置或不执行任何操作。因为我看不到这两种不同方法的价值。