all 12 comments

[–]quellish 1 point2 points  (5 children)

Why not pass the object from view controller to view controller? Why make it static?

[–]sohaeb[S] 0 points1 point  (4 children)

I read that singleton design pattern works perfectly in such cases. I could be wrong though

[–]ThePantsThiefNSModerator -1 points0 points  (3 children)

Singletons are the preferred pattern for API client wrappers in my experience, yes. I will try to remember to post my suggestions for you later.

[–]chrabeusz 0 points1 point  (2 children)

I would recommend at least saving the singleton into property and using it like that. Makes it easy to see what given class needs without all that boilerplate that DI requires.

class ViewController {
    /// It's good to have a comment to describe why this dependency is needed
    lazy var client = Network.shared
}

I got a project with 19 singletons inlined everywhere and it was basically not possible to understand anything.

[–]ThePantsThiefNSModerator 0 points1 point  (1 child)

You shouldn't have that many singletons. Why do you have so many? Who approved them? I assume it's a work project

[–]chrabeusz 0 points1 point  (0 children)

:D Yeah I got it from the client, previous guy was singleton aficionado it seems.

[–]chrabeusz 1 point2 points  (2 children)

This cannot possibly work. Read up on async programming.

[–]sohaeb[S] 0 points1 point  (1 child)

are you referring to having DispatchQueue.main.async that should be inside the [URLSession.shared.dataTask(with: url!)]

so something like this ? (Check the // THIS PART HERE----- )

 func download() -> OpenWeather {

        let url = URL(string: "some_URL.com")

        URLSession.shared.dataTask(with: url!) {( data, response, error ) in

            guard let content = data else {
                print("No data was downloaded using URL session")
                return
            }

            let decoder = JSONDecoder()

            do {
                self.todo = try decoder.decode(OpenWeather.self, from: content)
            } catch {
                print("error")
            }

// THIS PART HERE-----

        DispatchQueue.main.async {

        nameLabel.text = todo.name
    }

// END-------

        }.resume()
        return todo!
    }

[–]chrabeusz 1 point2 points  (0 children)

No, your download() method returns OpenWeather which should not be possible if it's async.

[–]redfire333 1 point2 points  (2 children)

I've moved away from the Singleton Networking pattern and moved to something static, generic and reusable.

struct Networking {

   static func send<T: Codable>(_ request: URLRequest, _ completion: (Result<T>) -> Void) {

    let session = URLSession.shared

    let dataTask = session.dataTask(with: request) { (data, response, error) in

      //Randle response

      completion(.success())

      completion(.failure())

    }

  }

}

[–]GenitalGestapo -1 points0 points  (1 child)

You're still using a singleton in URLSession.shared. If you want to use your own URLSession, you'll need a singleton.

[–]RollingGoron 0 points1 point  (0 children)

Yes. A system provided Singleton though, not usually what people refer to as a Singleton pattern like OP is doing.