0

I have a code that uses self inside a background thread. It worked perfectly well until I migrated it to Swift 3, since then I get an error message every time I use self that goes like this: "Closure cannot implicitly capture a mutating self parameter".

My code goes like this:

mutating func fetchData() {  
    DispatchQueue.global(qos: .background).async {
        for className in self.classNames {
            let query = PFQuery(className: className)
            query.limit = 1000

            do {
                for obj in try query.findObjects() {
                    let typeObj = TypeObj(parseObject: obj, typeName: className)

                    if className == “type1”{
                        self.type1s.append(store)
                    } else if className == “type2” {
                        self.type2s.append(store)
                    } else if className == “type3” {
                        self.type3s.append(store)
                    } else if className == “type4” {
                        self.type4s.append(store)
                    } else if className == “type5” {
                        self.type5s.append(store)
                    }
                }
            }
            catch _ {
               // Error handling
            }
        }

        let dict = [
                “Type1s”:self.type1s,”Type2s”:self.type2s,”Type3s”:self.type3s,”Type4s”:self.type4s,”Type5s”:self.type5s
        ]
        self.delegate.getDictionary(dict)
    }
}
Hamish
  • 78,605
  • 19
  • 187
  • 280
FS.O6
  • 1,394
  • 2
  • 20
  • 42
  • @Xcoder123 Every line that includes `self` – FS.O6 Mar 01 '17 at 18:55
  • @PEEJWEEJ I've already saw that, It's not a duplicate. I'm not using Firbase and the solution mentioned there doesn't work for me – FS.O6 Mar 01 '17 at 19:00
  • The other question is not about Firebase but about implicitly/explicitly capturing `self` in an escaping closure with a value type (`struct`) – which makes it look similar to your problem. – Martin R Mar 01 '17 at 19:04
  • @MartinR I've looked at the answer there, I can't find what should I change... If I take the code out of the background thread it all works perfectly fine – FS.O6 Mar 01 '17 at 19:08
  • Does it compile if you change your `struct` to a `class`? – Martin R Mar 01 '17 at 19:08
  • @MartinR If I change ti to class I get "Command failed due to signal: Segmentation fault: 11" – FS.O6 Mar 01 '17 at 19:10
  • It's the same issue that is explained in that question. It seems like it's core to what you're doing. You may need to move this functionality to a static function that takes and returns a new struct with the values changed. – GetSwifty Mar 01 '17 at 19:32

0 Answers0