0

When creating a label that uses data from an API within a closure that returns data from that api:

APIData().getRequest(epicGamesUsername: "test") { (output) in

        DispatchQueue.main.async {

            let winsLabelForProfile = Label().createLabel(labelText: output[0], font: "Avenir-HeavyOblique", fontSize: 45, center: center, centerX: centerX, centerY: centerY, offsetX: 0, offsetY: -(self.view.frame.width / 13), height: self.view.frame.width / 6, width: self.view.frame.width / 2, textAlignment: NSTextAlignment.center)

            self.view.addSubview(winsLabelForProfile)

        }

I have to put the creation of the label back onto the main thread.

My question is simply: is adding the label this way going to slow my app down? If so, what is a better way to accomplish this?

When I test adding a label without adding it asynchronously, it definitely seems faster.

Julian L
  • 84
  • 1
  • 3
  • 10
  • Is this closure already running on the main thread? Check [`Thread.isMainThread`](https://developer.apple.com/documentation/foundation/thread/1412704-ismainthread). Many API already dispatch their completion handlers to the main thread, so if that's the case here, dispatching asynchronously to the main thread again will only unnecessarily slow it down. But, if this completion handler is not running on the main thread, then you have no choice, you must dispatch it back to the main thread. – Rob Apr 12 '18 at 05:03
  • @Rob it seems that it isn't running on the main thread. Darn, it seems like it's just extra slow now. – Julian L Apr 12 '18 at 16:04
  • Unfortunately, without a [reproducible example of the problem](https://stackoverflow.com/help/mcve), we're just guessing. But generally, if UI is slow, it's either because you did something for UI from background thread or you have something blocking the main thread (a `sync` call, a `wait` or `sleep`, some synchronous API, etc.). – Rob Apr 12 '18 at 16:13
  • I'd suggest adding debug statements before `getRequest`, before `DispatchQueue.main.async`, and inside the `async` call. Measure the time for each of those. Identify how much time elapsed between each one. When you know where the delay is observed, then you can start to research it. – Rob Apr 12 '18 at 16:16

1 Answers1

0

The trade of is ignorable if APIData().getRequest is not invoked veery frequently. Since this is network related I don't think this is the case and in practice there is no overhead.

Also remember that if you have some performance issues you should use Xcode instruments to locate source of the problems instead guesting what is the coursing performance issues. Often people blame some part of code which is not responsible for performance issue (spatially newbies) and they wasting a time to optimize something that works properly.

Your code complexity is constant in time so if you have performance issue this is not a problem.

offtopic: this pice of code looks terrible it is hard to read and maintain. Consider make it simpler, split it into multiple lines.
In fact this looks like a bad dissing since network layer is to close to UI part. You do not have a data model which should maintain data structure and business logic.

Marek R
  • 32,568
  • 6
  • 55
  • 140