Networking structure for Swift iOS app

Multi tool use
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
$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
swift ios
$endgroup$
add a comment |
$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
swift ios
$endgroup$
add a comment |
$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
swift ios
$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
swift ios
asked 3 mins ago
stevenpcurtisstevenpcurtis
1505
1505
add a comment |
add a comment |
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
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
AjNyLwnK3rwo 1 TzJf,j1nx5YMyK,c12MZpM,WVlN10P,u,sBtjP3NgiT,jU3ICMQm3L01uMQ,5U0 YeH 1MECwZeafLdw1y,cm5j Yo