-1

After adding DispatchQueue.global(qos: .background).async {} to a single function of my code (Xcode 10, Swift 5), the scrollbar looks like this:

enter image description here

These are more than 40 calls to vars and other funcs that need a "self.".

Fixing this isn't hard (Editor > Fix All Issues) but it reduces readability a lot.

I'm aware that it's possible to create new copies of/references to these vars and using them instead of the ones outside but then there'll be a lot of new copies/references.

Is there a third way of fixing this that helps with readability but doesn't change the initial code (too much)?

Neph
  • 1,823
  • 2
  • 31
  • 69
  • Please post some code what you have tried. – Sagar Chauhan Jul 23 '19 at 12:46
  • You should _always_ say `self`. That looks better, not worse. The mistake was omitting it in the first place. Put in `self` here and everywhere. – matt Jul 23 '19 at 12:49
  • @SagarChauhan Sorry, not sure how code would help here. Everyone knows what `self.` looks like and I'm not directly trying to solve a problem with my code but more looking for an alternative. – Neph Jul 23 '19 at 12:53
  • @matt `You should always say self` - Even if you aren't in a background thread? I've never seen anyone do that and imo there's really no need for `self` (unless you are using a second thread) because it doesn't add anything and reduces readability too. – Neph Jul 23 '19 at 13:05
  • I disagree. I think omitting `self` reduces readability and does add something (readability). Otherwise you've just got all these unidentifiable terms floating around. Every message should have a recipient. Of course that's a matter of opinion / style, but my point is, so is your question. – matt Jul 23 '19 at 13:18
  • Could you please post an example of when I should definitely use it in your opinion then, even though the code is running on the main thread. The function I'm currently working on has more than 40 `self` because of the second thread. I know these vars are declared "outside" and it's easy to see that they are and yet there's this pink `self` "blob" (I'm aware you can change the colors in Xcode) in front of almost every one that distracts from the actual name of the var. – Neph Jul 23 '19 at 13:27
  • Also, while using `self.` when Xcode doesn't cry for it, is opinion-based, it's not when there's code in a second thread and this is exactly what I was/am looking an alternative for. So in the end there's nothing opinion-based about the question. – Neph Jul 23 '19 at 13:30
  • 2
    Extract that code in the closure into a method/nested function and pass the name of the method/nested function instead? – Sweeper Jul 23 '19 at 13:40

1 Answers1

1

You can create a nested function or another method to put the code you want to execute in, then pass that nested function or method into DispatchQueue.main.async.

Here's an example with a nested function:

Original code:

class Foo {
    var a = 0
    var b = 0
    var c = 0

    func f() {
        a = 1
        b = 1
        c = 1
    }
}

Doing it asynchronously:

class Foo {
    var a = 0
    var b = 0
    var c = 0

    func f() {
        func doAsync() {
            a = 1
            b = 1
            c = 1
        }
        DispatchQueue.global(qos: .background).async(execute: doAsync)
    }
}

As you can see, you don't need to add any selfs.

Sweeper
  • 213,210
  • 22
  • 193
  • 313