-
Notifications
You must be signed in to change notification settings - Fork 12
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
change fetch function from network layer to one with an escaping closure #3
base: main
Are you sure you want to change the base?
Conversation
test FeedDataManager test models gotten from network call can be mapped to cell view models
enum TweetAPIError: Error { | ||
case noData | ||
case response | ||
case parsingData | ||
} | ||
|
||
class FeedDataManager: FeedDataManagerProtocol { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Any class that does not implement inheritance should be defined as final class
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It is a good practice to define all classes as final
when there is no inheritance. This help to the compiler's performance.
} | ||
|
||
|
||
let decoder = JSONDecoder() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think is better to pass the JSONDecoder
instance as a parameter instead of creating it inside this method. If you do that, you are applying dependency injection because the JSONDecoder is a dependency for your method.
let url = URL(string: "https://gist.githubusercontent.com/ferdelarosa-wz/0c73ab5311c845fb7dfac4b62ab6c652/raw/6a39cffe68d87f1613f222372c62bd4e89ad06fa/tweets.json") | ||
guard let url else { return } | ||
|
||
let request = URLRequest(url: url) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the request should be a parameter for the fetch method. I understand that it is just one url
for this project but I just trying to do this in a general way.
} | ||
|
||
do { | ||
let jsonData = try decoder.decode(TweetsResponse.self, from: data) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It only works for the TweetsResponse
. Here you can define a generic method to do the data decoding. Just like this:
func decode<T: Decodable>(data: Data?, decoder: JSONDecoder = JSONDecoder()) -> T? {
return try? decoder.decode(T.self, from: data ?? Data())
}
import XCTest | ||
@testable import MiniBootcamp | ||
|
||
class FeedDataManagerTests: XCTestCase { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also, it's good to define final
the classes for the test cases.
test FeedDataManager
test models gotten from network call can be mapped to cell view models