How to Clear UI contents after each function call

Hello:


I have a function which when called, populates the labels and textView with data from a CoreData Fetch Request, for the user. Each time the function is called I want ONLY the new data in the UI controls. Presently, the new data is appended to the old which is totally unacceptable.


How can I clear the old data so that the User sees ONLY the new data?


Thanks for helping me with the solution.

Accepted Reply

OK.


But I don't understand what you are doing here.

You loop over several items. What do they contain ?

You set questionText?.string on each loop, but only the last value will be kept

What is questionItem. A String visibly ?

You don't use stringvalue anywhere: why do you define it ?


for item in items { 
        questionText?.string = "" 
        if let record = item.value(forKey: "question") as? String { 
           questionItem.append(record) 
          let stringvalue = "questionText: \(questionItem)" 
          questionText?.string = questionItem 
          }
   }



So, probably you want something closer to this:


questionText?.string = ""     // in fact, not needed, just to make it easier to understand
questionItem = ""
for item in items {
    if let record = item.value(forKey: "question") as? String {
        questionItem.append(record)
    }
}
let stringvalue = "questionText: \(questionItem)"   // What is it used for ?
questionText?.string = questionItem // Or may be stringvalue   ?????


Are you looping through items just to get the "question" key ?


If so, code could be:


questionItem = ""
for item in items {
    if let record = item.value(forKey: "question") as? String {
        questionItem = record
        break          // We have found the item containing question, let's leave the loop
    }
}
let stringvalue = "questionText: \(questionItem)"   // What is it used for ?
questionText?.string = questionItem // Or may be stringvalue   ?????

Replies

That's not totally clear.


When you speak of UIControls, do you mean only labels and textFields, TexyViews or other as well ?


Let's consider a label.

declared as @IBOutlet var weak myLabel: UILabel!

- its text is Text1

- then you fetch new data

- label becomes Text1Text2


Is it the problem ?


In that case, solution is easy.

In the fetch function, at start, call

myLabel.text = ""

then the fetch wilml just add Text2


Do the same for all items that need to be refreshed (you may have to manage the list of which has to be cleared)

Hello Claude 31:


I tried that without sucess. I think I am putting the Update in the wrong place. First I created a function as follows, then call the function, but nothing happens. Where do I put the function call?


func updateQuestionUI (){
    if questionItem != "" {
        questionText?.string = ""
        distractor1Label.stringValue = ""
        distractor2Label.stringValue = ""
        distractor3Label.stringValue = ""
        distractor4Label.stringValue = ""
        distractor5Label.stringValue = ""
        idLabel.stringValue = ""
    }
    }


  @IBAction func getQuestion(_ sender: Any) {
      
        func runConsistencyCheck() {
            do {

                try managedContext.save()
            } catch {
                print("An error occurred during consistency checking: \(error)")
            }
        }
     
        let number = Int.random(in: 0 ..< 2246)
      
       guard let appDelegate = NSApplication.shared.delegate as? AppDelegate else {
          
                        return
                }
      
        _ = appDelegate.persistentContainer.viewContext
        let fetchRequest = NSFetchRequest(entityName: "SCQ")
        fetchRequest.returnsObjectsAsFaults = false
      
        fetchRequest.predicate = NSPredicate(format: "qid = %i", number)
      
          do {
                  
                    items = try managedContext.fetch(fetchRequest)
         
                    print ("Number is \(number)")
   //               print ("Record Returned is \(items)")
                  
                } catch let error as NSError {
                  
                    print("Could not fetch. \(error), \(error.userInfo)")
                }
//One UI item the textView
        for item in items {
            questionText?.string = ""
        if let record = item.value(forKey: "question") as? String {
                
                questionItem.append(record)
          let stringvalue = "questionText: \(questionItem)"
          questionText?.string = questionItem
//                print(stringvalue)
            print ("PProcess E")
                    }
        }

I think I am putting the Update in the wrong place

Where did you put ? I cannot find in code

First I created a function as follows, then call the function

IO do not see any call to updateQuestionUI


What is the IBOutlet you want to update ?


Do you update in updateQuestionUI() ?


With what content ?

Sorry. I didn't include the items:


for item in items {
         
        if let record = item.value(forKey: "question") as? String {
               
                questionItem.append(record)
          let stringvalue = "questionText: \(questionItem)"
          questionText?.string = questionItem
//                print(stringvalue)
            print ("PProcess E")
                    }
        }

OK.


But I don't understand what you are doing here.

You loop over several items. What do they contain ?

You set questionText?.string on each loop, but only the last value will be kept

What is questionItem. A String visibly ?

You don't use stringvalue anywhere: why do you define it ?


for item in items { 
        questionText?.string = "" 
        if let record = item.value(forKey: "question") as? String { 
           questionItem.append(record) 
          let stringvalue = "questionText: \(questionItem)" 
          questionText?.string = questionItem 
          }
   }



So, probably you want something closer to this:


questionText?.string = ""     // in fact, not needed, just to make it easier to understand
questionItem = ""
for item in items {
    if let record = item.value(forKey: "question") as? String {
        questionItem.append(record)
    }
}
let stringvalue = "questionText: \(questionItem)"   // What is it used for ?
questionText?.string = questionItem // Or may be stringvalue   ?????


Are you looping through items just to get the "question" key ?


If so, code could be:


questionItem = ""
for item in items {
    if let record = item.value(forKey: "question") as? String {
        questionItem = record
        break          // We have found the item containing question, let's leave the loop
    }
}
let stringvalue = "questionText: \(questionItem)"   // What is it used for ?
questionText?.string = questionItem // Or may be stringvalue   ?????

Hello Claude:


This Works:

        questionText?.string = ""
        questionItem = ""
        for item in items {
        if let record = item.value(forKey: "question") as? String { 
                questionItem.append(record)
          let stringvalue = "questionText: \(questionItem)"
          questionText?.string = questionItem
//                print(stringvalue)
            print ("PProcess E")
                    }


Thanks a million.

Great. Thanks for feedback.