Networking structure for Swift iOS app

If you attempt to grapple an opponent that you are hidden from, do they roll at disadvantage?

Greatest common substring

when is out of tune ok?

Will it be accepted, if there is no ''Main Character" stereotype?

Applicability of Single Responsibility Principle

Your magic is very sketchy

How could Frankenstein get the parts for his _second_ creature?

Bash method for viewing beginning and end of file

Why did Kant, Hegel, and Adorno leave some words and phrases in the Greek alphabet?

How can I get through very long and very dry, but also very useful technical documents when learning a new tool?

Is expanding the research of a group into machine learning as a PhD student risky?

Time travel short story where a man arrives in the late 19th century in a time machine and then sends the machine back into the past

is this a spam?

What is the oldest known work of fiction?

Go Pregnant or Go Home

Can a monster with multiattack use this ability if they are missing a limb?

Short story about space worker geeks who zone out by 'listening' to radiation from stars

Is there any easy technique written in Bhagavad GITA to control lust?

Can somebody explain Brexit in a few child-proof sentences?

Is this Spell Mimic feat balanced?

Is the destination of a commercial flight important for the pilot?

Curses work by shouting - How to avoid collateral damage?

Teaching indefinite integrals that require special-casing

How was Earth single-handedly capable of creating 3 of the 4 gods of chaos?



Networking structure for Swift iOS app














1












$begingroup$


I would like any constructive comments regarding the structure of this simple App that takes an API response and then displays on a table view.



The URL is written in a ConstantsAPI file



let baseUrl : String = "https://haveibeenpwned.com/api/v2"
let breachesExtensionURL : String = "/breaches"


Displayed on a tableviewcontroller



class SitewideTableViewController: UITableViewController, DataManagerDelegate 
var pwnedData = [BreachModel]()
var session: URLSession!
var task: URLSessionDownloadTask!

override func viewDidLoad()
super.viewDidLoad()
session = URLSession.shared
task = URLSessionDownloadTask()
DataManager.shared.delegate = self
DataManager.shared.fetchBreaches()


func didDownloadBreaches()
DispatchQueue.main.async
self.pwnedData = DataManager.shared.sortedBreaches()
self.tableView.reloadData()



override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int
return pwnedData.count


override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
let cell = tableView.dequeueReusableCell(withIdentifier: "Sitewide", for: indexPath)
cell.textLabel?.text = pwnedData[indexPath.row].name
return cell




Using the following model



import Foundation

class BreachModel : Codable
let name : String
let title : String
let domain : String
let breachDate : String
let addedDate : String
let modifiedData : String
let pwnCount : Int
let description: String

private enum CodingKeys: String, CodingKey
case name = "Name"
case title = "Title"
case domain = "Domain"
case breachDate = "BreachDate"
case addedDate = "AddedDate"
case modifiedData = "ModifiedDate"
case pwnCount = "PwnCount"
case description = "Description"




With a Data manager that would manage all of the data



@objc protocol DataManagerDelegate: class 
// optional delegate to practice
@objc optional func didDownloadBreaches() // called when the manager has completed downloading all the breaches



class DataManager
static let shared: DataManager = DataManager()
public weak var delegate: DataManagerDelegate? = nil

private var breaches = [BreachModel]()
func fetchBreaches()

HTTPManager.shared.get(urlString: baseUrl + breachesExtensionURL, completionBlock: [weak self] (data: Data?) -> Void in
let decoder = JSONDecoder()
if let data = data
print(data.count)
do
self?.breaches = try decoder.decode([BreachModel].self, from: data)
self?.delegate?.didDownloadBreaches?()
catch let error
print ("Error in reading data", error)



)


func sortedBreaches() -> [BreachModel]
return breaches.sorted a,b in a.name < b.name




That calls a HTTP manager whose only responsibility is to call url's



class HTTPManager 
static let shared: HTTPManager = HTTPManager()
public func get (urlString: String, completionBlock: ((Data?) -> Void)?)
let url = URL(string: urlString)
if let usableUrl = url
let request = URLRequest(url: usableUrl)
let task = URLSession.shared.dataTask(with: request, completionHandler: (data, response, error) in
completionBlock?(data)
)
task.resume()





So is this a reasonable extendable structure?
Should I have used dataTask or URLSessionDownloadTask?
Have I unwittingly introduced some memory leaks?



Any comments appreciated.



Git link: https://github.com/stevencurtis/basicnetworking









share









$endgroup$
















    1












    $begingroup$


    I would like any constructive comments regarding the structure of this simple App that takes an API response and then displays on a table view.



    The URL is written in a ConstantsAPI file



    let baseUrl : String = "https://haveibeenpwned.com/api/v2"
    let breachesExtensionURL : String = "/breaches"


    Displayed on a tableviewcontroller



    class SitewideTableViewController: UITableViewController, DataManagerDelegate 
    var pwnedData = [BreachModel]()
    var session: URLSession!
    var task: URLSessionDownloadTask!

    override func viewDidLoad()
    super.viewDidLoad()
    session = URLSession.shared
    task = URLSessionDownloadTask()
    DataManager.shared.delegate = self
    DataManager.shared.fetchBreaches()


    func didDownloadBreaches()
    DispatchQueue.main.async
    self.pwnedData = DataManager.shared.sortedBreaches()
    self.tableView.reloadData()



    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int
    return pwnedData.count


    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
    let cell = tableView.dequeueReusableCell(withIdentifier: "Sitewide", for: indexPath)
    cell.textLabel?.text = pwnedData[indexPath.row].name
    return cell




    Using the following model



    import Foundation

    class BreachModel : Codable
    let name : String
    let title : String
    let domain : String
    let breachDate : String
    let addedDate : String
    let modifiedData : String
    let pwnCount : Int
    let description: String

    private enum CodingKeys: String, CodingKey
    case name = "Name"
    case title = "Title"
    case domain = "Domain"
    case breachDate = "BreachDate"
    case addedDate = "AddedDate"
    case modifiedData = "ModifiedDate"
    case pwnCount = "PwnCount"
    case description = "Description"




    With a Data manager that would manage all of the data



    @objc protocol DataManagerDelegate: class 
    // optional delegate to practice
    @objc optional func didDownloadBreaches() // called when the manager has completed downloading all the breaches



    class DataManager
    static let shared: DataManager = DataManager()
    public weak var delegate: DataManagerDelegate? = nil

    private var breaches = [BreachModel]()
    func fetchBreaches()

    HTTPManager.shared.get(urlString: baseUrl + breachesExtensionURL, completionBlock: [weak self] (data: Data?) -> Void in
    let decoder = JSONDecoder()
    if let data = data
    print(data.count)
    do
    self?.breaches = try decoder.decode([BreachModel].self, from: data)
    self?.delegate?.didDownloadBreaches?()
    catch let error
    print ("Error in reading data", error)



    )


    func sortedBreaches() -> [BreachModel]
    return breaches.sorted a,b in a.name < b.name




    That calls a HTTP manager whose only responsibility is to call url's



    class HTTPManager 
    static let shared: HTTPManager = HTTPManager()
    public func get (urlString: String, completionBlock: ((Data?) -> Void)?)
    let url = URL(string: urlString)
    if let usableUrl = url
    let request = URLRequest(url: usableUrl)
    let task = URLSession.shared.dataTask(with: request, completionHandler: (data, response, error) in
    completionBlock?(data)
    )
    task.resume()





    So is this a reasonable extendable structure?
    Should I have used dataTask or URLSessionDownloadTask?
    Have I unwittingly introduced some memory leaks?



    Any comments appreciated.



    Git link: https://github.com/stevencurtis/basicnetworking









    share









    $endgroup$














      1












      1








      1





      $begingroup$


      I would like any constructive comments regarding the structure of this simple App that takes an API response and then displays on a table view.



      The URL is written in a ConstantsAPI file



      let baseUrl : String = "https://haveibeenpwned.com/api/v2"
      let breachesExtensionURL : String = "/breaches"


      Displayed on a tableviewcontroller



      class SitewideTableViewController: UITableViewController, DataManagerDelegate 
      var pwnedData = [BreachModel]()
      var session: URLSession!
      var task: URLSessionDownloadTask!

      override func viewDidLoad()
      super.viewDidLoad()
      session = URLSession.shared
      task = URLSessionDownloadTask()
      DataManager.shared.delegate = self
      DataManager.shared.fetchBreaches()


      func didDownloadBreaches()
      DispatchQueue.main.async
      self.pwnedData = DataManager.shared.sortedBreaches()
      self.tableView.reloadData()



      override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int
      return pwnedData.count


      override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
      let cell = tableView.dequeueReusableCell(withIdentifier: "Sitewide", for: indexPath)
      cell.textLabel?.text = pwnedData[indexPath.row].name
      return cell




      Using the following model



      import Foundation

      class BreachModel : Codable
      let name : String
      let title : String
      let domain : String
      let breachDate : String
      let addedDate : String
      let modifiedData : String
      let pwnCount : Int
      let description: String

      private enum CodingKeys: String, CodingKey
      case name = "Name"
      case title = "Title"
      case domain = "Domain"
      case breachDate = "BreachDate"
      case addedDate = "AddedDate"
      case modifiedData = "ModifiedDate"
      case pwnCount = "PwnCount"
      case description = "Description"




      With a Data manager that would manage all of the data



      @objc protocol DataManagerDelegate: class 
      // optional delegate to practice
      @objc optional func didDownloadBreaches() // called when the manager has completed downloading all the breaches



      class DataManager
      static let shared: DataManager = DataManager()
      public weak var delegate: DataManagerDelegate? = nil

      private var breaches = [BreachModel]()
      func fetchBreaches()

      HTTPManager.shared.get(urlString: baseUrl + breachesExtensionURL, completionBlock: [weak self] (data: Data?) -> Void in
      let decoder = JSONDecoder()
      if let data = data
      print(data.count)
      do
      self?.breaches = try decoder.decode([BreachModel].self, from: data)
      self?.delegate?.didDownloadBreaches?()
      catch let error
      print ("Error in reading data", error)



      )


      func sortedBreaches() -> [BreachModel]
      return breaches.sorted a,b in a.name < b.name




      That calls a HTTP manager whose only responsibility is to call url's



      class HTTPManager 
      static let shared: HTTPManager = HTTPManager()
      public func get (urlString: String, completionBlock: ((Data?) -> Void)?)
      let url = URL(string: urlString)
      if let usableUrl = url
      let request = URLRequest(url: usableUrl)
      let task = URLSession.shared.dataTask(with: request, completionHandler: (data, response, error) in
      completionBlock?(data)
      )
      task.resume()





      So is this a reasonable extendable structure?
      Should I have used dataTask or URLSessionDownloadTask?
      Have I unwittingly introduced some memory leaks?



      Any comments appreciated.



      Git link: https://github.com/stevencurtis/basicnetworking









      share









      $endgroup$




      I would like any constructive comments regarding the structure of this simple App that takes an API response and then displays on a table view.



      The URL is written in a ConstantsAPI file



      let baseUrl : String = "https://haveibeenpwned.com/api/v2"
      let breachesExtensionURL : String = "/breaches"


      Displayed on a tableviewcontroller



      class SitewideTableViewController: UITableViewController, DataManagerDelegate 
      var pwnedData = [BreachModel]()
      var session: URLSession!
      var task: URLSessionDownloadTask!

      override func viewDidLoad()
      super.viewDidLoad()
      session = URLSession.shared
      task = URLSessionDownloadTask()
      DataManager.shared.delegate = self
      DataManager.shared.fetchBreaches()


      func didDownloadBreaches()
      DispatchQueue.main.async
      self.pwnedData = DataManager.shared.sortedBreaches()
      self.tableView.reloadData()



      override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int
      return pwnedData.count


      override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
      let cell = tableView.dequeueReusableCell(withIdentifier: "Sitewide", for: indexPath)
      cell.textLabel?.text = pwnedData[indexPath.row].name
      return cell




      Using the following model



      import Foundation

      class BreachModel : Codable
      let name : String
      let title : String
      let domain : String
      let breachDate : String
      let addedDate : String
      let modifiedData : String
      let pwnCount : Int
      let description: String

      private enum CodingKeys: String, CodingKey
      case name = "Name"
      case title = "Title"
      case domain = "Domain"
      case breachDate = "BreachDate"
      case addedDate = "AddedDate"
      case modifiedData = "ModifiedDate"
      case pwnCount = "PwnCount"
      case description = "Description"




      With a Data manager that would manage all of the data



      @objc protocol DataManagerDelegate: class 
      // optional delegate to practice
      @objc optional func didDownloadBreaches() // called when the manager has completed downloading all the breaches



      class DataManager
      static let shared: DataManager = DataManager()
      public weak var delegate: DataManagerDelegate? = nil

      private var breaches = [BreachModel]()
      func fetchBreaches()

      HTTPManager.shared.get(urlString: baseUrl + breachesExtensionURL, completionBlock: [weak self] (data: Data?) -> Void in
      let decoder = JSONDecoder()
      if let data = data
      print(data.count)
      do
      self?.breaches = try decoder.decode([BreachModel].self, from: data)
      self?.delegate?.didDownloadBreaches?()
      catch let error
      print ("Error in reading data", error)



      )


      func sortedBreaches() -> [BreachModel]
      return breaches.sorted a,b in a.name < b.name




      That calls a HTTP manager whose only responsibility is to call url's



      class HTTPManager 
      static let shared: HTTPManager = HTTPManager()
      public func get (urlString: String, completionBlock: ((Data?) -> Void)?)
      let url = URL(string: urlString)
      if let usableUrl = url
      let request = URLRequest(url: usableUrl)
      let task = URLSession.shared.dataTask(with: request, completionHandler: (data, response, error) in
      completionBlock?(data)
      )
      task.resume()





      So is this a reasonable extendable structure?
      Should I have used dataTask or URLSessionDownloadTask?
      Have I unwittingly introduced some memory leaks?



      Any comments appreciated.



      Git link: https://github.com/stevencurtis/basicnetworking







      swift ios





      share












      share










      share



      share










      asked 3 mins ago









      stevenpcurtisstevenpcurtis

      1505




      1505




















          0






          active

          oldest

          votes











          Your Answer





          StackExchange.ifUsing("editor", function ()
          return StackExchange.using("mathjaxEditing", function ()
          StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix)
          StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
          );
          );
          , "mathjax-editing");

          StackExchange.ifUsing("editor", function ()
          StackExchange.using("externalEditor", function ()
          StackExchange.using("snippets", function ()
          StackExchange.snippets.init();
          );
          );
          , "code-snippets");

          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "196"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: false,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: null,
          bindNavPrevention: true,
          postfix: "",
          imageUploader:
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          ,
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );













          draft saved

          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f216310%2fnetworking-structure-for-swift-ios-app%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown

























          0






          active

          oldest

          votes








          0






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes















          draft saved

          draft discarded
















































          Thanks for contributing an answer to Code Review Stack Exchange!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid


          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.

          Use MathJax to format equations. MathJax reference.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f216310%2fnetworking-structure-for-swift-ios-app%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          कुँवर स्रोत दिक्चालन सूची"कुँवर""राणा कुँवरके वंशावली"

          Why is a white electrical wire connected to 2 black wires?How to wire a light fixture with 3 white wires in box?How should I wire a ceiling fan when there's only three wires in the box?Two white, two black, two ground, and red wire in ceiling box connected to switchWhy is there a white wire connected to multiple black wires in my light box?How to wire a light with two white wires and one black wireReplace light switch connected to a power outlet with dimmer - two black wires to one black and redHow to wire a light with multiple black/white/green wires from the ceiling?Ceiling box has 2 black and white wires but fan/ light only has 1 of eachWhy neutral wire connected to load wire?Switch with 2 black, 2 white, 2 ground and 1 red wire connected to ceiling light and a receptacle?

          चैत्य भूमि चित्र दीर्घा सन्दर्भ बाहरी कडियाँ दिक्चालन सूची"Chaitya Bhoomi""Chaitya Bhoomi: Statue of Equality in India""Dadar Chaitya Bhoomi: Statue of Equality in India""Ambedkar memorial: Centre okays transfer of Indu Mill land"चैत्यभमि