0

I try to compare the current date to dates I've safed on Firebase. I convert them form timestamp to dates before comparing. And if the equal, I want to set the value of bool1 (variable) to true, in order to use it later to set a background color. By doing so, I get the Error:

Escaping closure mutating self parameter.

Thats the code:

struct RKDate1 {

    var date: Date

    let rkManager: RKManager1
    @State var bool1 = false
    var isDisabled: Bool = false
    var isToday: Bool = false
    var issafed: Bool = false
    var isSelected: Bool = false
    var isBetweenStartAndEnd: Bool = false

    init(date: Date, rkManager: RKManager1, isDisabled: Bool, isToday: Bool, isSelected: Bool, isBetweenStartAndEnd: Bool) {
        self.date = date
        self.rkManager = rkManager
        self.isDisabled = isDisabled

        self.isToday = isToday
        self.isSelected = isSelected
        self.isBetweenStartAndEnd = isBetweenStartAndEnd

        let db = Firestore.firestore()

        db.collection("Termin").order(by: "date").addSnapshotListener { (snap, err) in

            if err != nil{

                print((err?.localizedDescription)!)

                return
            }

            for i in snap!.documentChanges{

                let id = i.document.documentID

                let date1 = i.document.get("date") as! Timestamp

                let components = Calendar.current.dateComponents([.day,.month, .year], from: date1.dateValue())
                let components1 = Calendar.current.dateComponents([.day,.month, .year], from: date)
                let currentdate = Calendar.current.date(from: components1)
                let newdate = Calendar.current.date(from: components)

                if newdate == currentdate {
                    self.bool1 = true
                }
            }
        }
    }
}
Rob
  • 415,655
  • 72
  • 787
  • 1,044
Tobi M
  • 15
  • 4
  • I notice that RKDate1 is not a view (no var body: some View), so you should not use @State var bool1 in this struct. Even if you make it a view, it is not a good idea to do your firestore stuff in the init(...). – workingdog support Ukraine Apr 22 '20 at 06:27

2 Answers2

0

EDIT: Seems like you cannot mutate structs anymore in escaping closure without removing @escaping which not be possible in your case. You might want to consider changing your implementation to a class.


Structs are immutable. Which mean they cannot be mutated. In your case you are modifying the value of self.bool1 = true which is changing the value of self.

~~A better way (IMO) would be to create a mutating func to do your firebase call and update the values inside mutating function.~~

Also, you shouldn’t use State property wrappers in models. They should only be used in views.

Ishmeet
  • 707
  • 6
  • 18
0

Thank you for the response. I appreciate your efforts. I tried with a mutating func, sadly it isn't working either. I get the same error message. I didn't used a State property anymore. I'm a beginner and not quite sure whether I have fully understood what I have might have done wrong.

mutating func finddates() {


      let db = Firestore.firestore()

                            db.collection("Termin").order(by: "date").addSnapshotListener { (snap, err) in

                                if err != nil{

                                    print((err?.localizedDescription)!)

                                    return
                                }




                                for i in snap!.documentChanges{


                                  let id = i.document.documentID


                                        let date1 = i.document.get("date") as! Timestamp



                                   let components = Calendar.current.dateComponents([.day,.month, .year], from: date1.dateValue())
                                  let components1 = Calendar.current.dateComponents([.day,.month, .year], from: self.date)
                                    let currentdate = Calendar.current.date(from: components1)
                                   let newdate = Calendar.current.date(from: components)

                                    if newdate == currentdate {
                                      self.bool1 = true
                                    }



                       }

                   }

  }
Tobi M
  • 15
  • 4