Skip to content

Commit

Permalink
Added nonisolated to all async background methods to ensure they run …
Browse files Browse the repository at this point in the history
…off the main thread.
  • Loading branch information
ryanlintott committed Aug 16, 2024
1 parent 40d928f commit 4988c1c
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 11 deletions.
8 changes: 4 additions & 4 deletions Sources/WordpressReader/Extensions/URLSession+async.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ internal extension URLSession {
/// - dateDecodingStrategy: Date strategy for the JSON decoder. Default is .deferredToDate.
/// - Returns: An asynchronously-delivered type decoded from the Data contents of the URL.
/// - Throws: Error if there is a bad response or DecodingError if the type cannot be decoded.
func fetchJsonData<T: Decodable>(
nonisolated func fetchJsonData<T: Decodable>(
_ type: T.Type,
url: URL,
keyDecodingStrategy: JSONDecoder.KeyDecodingStrategy = .useDefaultKeys,
Expand Down Expand Up @@ -47,7 +47,7 @@ internal extension URLSession {
/// - Parameter url: URL to retrieve.
/// - Returns: An asynchronously-delivered HTTP URL respose.
/// - Throws: Error if there is a bad response or a non-HTTP URL
func fetchHTTPURLResponse(url: URL) async throws -> HTTPURLResponse {
nonisolated func fetchHTTPURLResponse(url: URL) async throws -> HTTPURLResponse {
let response: URLResponse

(_, response) = try await self.data(from: url)
Expand All @@ -65,7 +65,7 @@ internal extension URLSession {
/// - header: header to retrieve.
/// - Returns: An asynchronously-delivered value of the supplied header from the URL.
/// - Throws: WordpressReaderError if there's a bad response, non-HTTP URL or a bad header name.
func fetchHeader(url: URL, forHTTPHeaderField header: String) async throws -> String {
nonisolated func fetchHeader(url: URL, forHTTPHeaderField header: String) async throws -> String {
let httpResponse = try await fetchHTTPURLResponse(url: url)

guard let value = httpResponse.value(forHTTPHeaderField: header) else {
Expand All @@ -79,7 +79,7 @@ internal extension URLSession {
/// - Parameter url: URL to retrieve.
/// - Returns: An asynchronously-delivered dictionary of all header-value pairs from the URL.
/// - Throws: WordpressReaderError if there is a bad response or a non-HTTP URL
func fetchAllHeaders(url: URL) async throws -> [AnyHashable: Any] {
nonisolated func fetchAllHeaders(url: URL) async throws -> [AnyHashable: Any] {
let httpResponse = try await fetchHTTPURLResponse(url: url)

return httpResponse.allHeaderFields
Expand Down
4 changes: 2 additions & 2 deletions Sources/WordpressReader/WordpressSite+async-internal.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ extension WordpressSite {
/// - Parameter request: Request used to retrieve paginated URLs
/// - Returns: An array of paginated URLs based on the supplied request.
/// - Throws: WordpressReaderError if there are URL errors, badly formatted query items, or if there is no totalPages value in the header.
func fetchPaginatedUrls<T: WordpressItem>(_ request: WordpressRequest<T>) async throws -> [URL] {
nonisolated func fetchPaginatedUrls<T: WordpressItem>(_ request: WordpressRequest<T>) async throws -> [URL] {
let baseUrl = restAPIv2Url.appendingPathComponent(T.self.urlComponent)
guard var urlComponents = URLComponents(url: baseUrl, resolvingAgainstBaseURL: true) else {
throw WordpressReaderError.URLError.badURL(urlString: baseUrl.absoluteString)
Expand Down Expand Up @@ -99,7 +99,7 @@ extension WordpressSite {
/// - urls: An array of URLs used to retrieve Wordpress items.
/// - Returns: An array of Wordpress items asynchronously.
/// - Throws: WordpressReaderError if there are network errors or if the URLs to not return JSON results that match the provided type.
func fetchItems<T: WordpressItem>(
nonisolated func fetchItems<T: WordpressItem>(
urlSession: URLSession = .shared,
_ type: T.Type,
urls: [URL]
Expand Down
10 changes: 5 additions & 5 deletions Sources/WordpressReader/WordpressSite+async-public.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ extension WordpressSite {
/// - Parameter urlSession: URL session to use for this request.
/// - Returns: Wordpress settings.
/// - Throws: WordpressReaderError if there is a bad response or DecodingError if the type cannot be decoded.
public func fetchSettings(urlSession: URLSession = .shared) async throws -> WordpressSettings {
nonisolated public func fetchSettings(urlSession: URLSession = .shared) async throws -> WordpressSettings {
try await urlSession.fetchJsonData(
WordpressSettings.self,
url: settingsUrl,
Expand All @@ -27,7 +27,7 @@ extension WordpressSite {
/// - id: Unique identifier for Wordpress Item.
/// - Returns: A Wordpress item matching a supplied unique identifier.
/// - Throws: WordpressReaderError if there is a bad response or DecodingError if the type cannot be decoded.
public func fetchById<T: WordpressItem>(
nonisolated public func fetchById<T: WordpressItem>(
urlSession: URLSession = .shared,
_ type: T.Type,
id: Int
Expand All @@ -45,7 +45,7 @@ extension WordpressSite {
/// - Parameter request: Wordpress request used to retrieve Wordpress items.
/// - Returns: An asynchronous throwing stream of arrays of Wordpress items.
/// - Throws: WordpressReaderError, or DecodingError.
public func stream<T: WordpressItem>(
nonisolated public func stream<T: WordpressItem>(
_ request: WordpressRequest<T>
) async throws -> AsyncThrowingStream<[T], Error> {
let urls = try await fetchPaginatedUrls(request)
Expand All @@ -58,7 +58,7 @@ extension WordpressSite {
/// - Parameter request: Wordpress request used to retrieve Wordpress items.
/// - Returns: An array of Wordpress items asynchronously.
/// - Throws: WordpressReaderError, or DecodingError.
public func fetch<T: WordpressItem>(
nonisolated public func fetch<T: WordpressItem>(
_ request: WordpressRequest<T>
) async throws -> [T] {
let urls = try await fetchPaginatedUrls(request)
Expand All @@ -71,7 +71,7 @@ extension WordpressSite {
/// - Parameter type: The type of Wordpress item to retrieve using a default request.
/// - Returns: An array of Wordpress items asynchronously.
/// - Throws: WordpressReaderError, or DecodingError.
public func fetch<T: WordpressItem>(
nonisolated public func fetch<T: WordpressItem>(
_ type: T.Type
) async throws -> [T] {
try await fetch(T.self.request())
Expand Down

0 comments on commit 4988c1c

Please sign in to comment.