Tuesday, March 21, 2023
HomeiOS Developmentios - I'm making an attempt to insert knowledge to CoreData entity...

ios – I’m making an attempt to insert knowledge to CoreData entity in the midst of array, however it doesn’t work


The objective of my app is to assist customers to be taught the phrases of one other language

I exploit for that two CoreData entities:
WordEntity with attributes ru and eng. There are I need to save phrases in Russian and English.

And QuizEntity with attributes to, from and identify. As I retailer phrases in

    var phrases: [WordEntity] = [],

I want to recollect indexes for each quiz and identify to indicate for consumer.

I’ve CoreDataManager class.

    class CoreDataManager{
        let context: NSManagedObjectContext
    
        personal init(){
            let appDelegate = UIApplication.shared.delegate as! AppDelegate
            context = appDelegate.persistentContainer.viewContext
            fetchWords()
            fetchQuizzes()
        }
    
        static let shared = CoreDataManager()
    
        //MARK: - WordEntity
    
        var phrases: [WordEntity] = []
    
        //MARK: - QuizEntity
    
        var quizzes: [QuizEntity] = []

        func fetchWords(){
            let fetchRequest: NSFetchRequest<WordEntity> = WordEntity.fetchRequest()
        
           do {
                let phrases = attempt context.fetch(fetchRequest)
                self.phrases = phrases
            } catch let error as NSError {
                print(error.localizedDescription)
            }
        }

        func fetchQuizzes(){
            let fetchRequest: NSFetchRequest<QuizEntity> = QuizEntity.fetchRequest()
        
            do {
                let quizzes = attempt context.fetch(fetchRequest)
                self.quizzes = quizzes
            } catch let error as NSError {
                print(error.localizedDescription)
            }
        }

I need to implement operate that enable consumer so as to add phrases to the quiz. To try this I attempted to insert phrase into phrases[] and transfer index for quiz.to(is final index of phrases on this quiz).

Instance(pseudocode):

    phrases = ["cat","audi","tesla","school"]
    quiz[0] ={
        .from = 0
        .to = 0
        identify = "animals"
    }
    quiz[1] ={
        .from = 1
        .to = 2
        identify = "vehicles"
    }
    quiz[2] ={
        .from = 3
        .to = 3
        identify = "training"
    }
    For instance I need to insert "bmw" to quiz2. phrases array shall be one thing like -             ["cat","audi","tesla","bmw","school"] and quizzes shall be: 
    quiz[1] ={
        .from = 1
        .to = 3
        identify = "vehicles"
    }
    quiz[2] ={
        .from = 4
        .to = 5
        identify = "training"
    }

I used to be making an attempt three alternative ways.
That is one of the best for my part:

    /*As i exploit tableViewController, I move indexOfQuis the place i need to add phrase.
    Within the high instance it's 1. dataWords is knowledge that i need to insert,
    countOfWords means how a lot phrases I need to insert and From is index quiz[indexOfQuiz].to .*/
    func addWordsToQuiz(indexOfQuiz: Int,dataWordsRu: [String], dataWordsEng: [String], countOfWords: Int, from: Int){
        
            guard let entity = NSEntityDescription.entity(forEntityName: "WordEntity", in:     context) else { return }
            let taskObject = WordEntity(entity: entity, insertInto: context)
        
            taskObject.setValue(dataWordsRu[0], forKey: "ru")
            taskObject.eng = dataWordsEng[0]
            //insert Entity
            phrases.insert(taskObject, at: from + 1)    
            //Shifting index for quiz.to(is final index of phrases on this quiz)
            for quiz in indexOfQuiz..<quizzes.depend{
                if quiz == indexOfQuiz{
                    quizzes[quiz].to = Int64(quizzes[quiz].to + 1)
                }else{
                    quizzes[quiz].from = Int64(quizzes[quiz].from + 1)
                    quizzes[quiz].to = Int64(quizzes[quiz].to + 1)
                }
            }
        
            do {
                attempt context.save()
            } catch let error as NSError {
                print(error.localizedDescription)
            }
        
        }

Under I’ll present you phrases[index].ru earlier than this operate and after.
And it really works nearly properly, besides …

Elective("1")
Elective("2")
Elective("3")
Elective("4")
Elective("5")
Elective("6")
Elective("7")
Elective("8")
Elective("9")

That is array of phrases earlier than inserting.

Elective("1")
Elective("2")
Elective("3")
Elective("4")
Elective("5")
Elective("6")
Elective("qwe")
Elective("7")
Elective("8")
Elective("9")

That is after. I inserted “qwe” phrase.

But when I restart my venture or shut and open app on simulator it seems to be like

Elective("1")
Elective("2")
Elective("3")
Elective("4")
Elective("5")
Elective("6")
Elective("7")
Elective("8")
Elective("9")
Elective("qwe")

I do not know why the place was modified and my query is how can I repair it?

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments