Thursday, 28 March 2019

CoreData returns empty data

coreData returns empty data when there should not be any, even if you uninstall the application and reinstall it and make a request to Сore Data, the context.fetch returns the data

get all Data in Сore Data

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()

}

I have these functions, but they are not called when I get data from coreData

// creates a path and checks for the presence of an element

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)

    }

}

// remove data from Сore Data

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) }

    })

}

// add data to Сore Data

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)
            }
        })

    }

}

that's what result returns

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



from CoreData returns empty data

No comments:

Post a Comment