Skip to content

Commit

Permalink
fix: ipa library race condition (#881)
Browse files Browse the repository at this point in the history
  • Loading branch information
TheMoonThatRises committed Mar 21, 2023
1 parent 1625894 commit 1168ca8
Showing 1 changed file with 17 additions and 3 deletions.
20 changes: 17 additions & 3 deletions PlayCover/Views/Settings/IPASourceSettings.swift
Expand Up @@ -181,6 +181,9 @@ struct AddSourceView: View {
@Binding var addSourceSheet: Bool
@EnvironmentObject var storeVM: StoreVM

@State var checkTask: Task<Void, Error>?
@State var urlSessionTask: URLSessionTask?

var body: some View {
VStack {
TextField(text: $newSource, label: {Text("preferences.textfield.url")})
Expand Down Expand Up @@ -238,6 +241,13 @@ struct AddSourceView: View {
.padding()
.frame(width: 400, height: 100)
.onChange(of: newSource) { source in
if let task = checkTask, !task.isCancelled {
if let session = urlSessionTask {
session.cancel()
}

task.cancel()
}
validateSource(source)
}
.onAppear {
Expand All @@ -259,13 +269,15 @@ struct AddSourceView: View {

sourceValidationState = .empty

Task {
checkTask = Task {
if var url = URL(string: source) {
if url.scheme == nil {
url = URL(string: "https://" + url.absoluteString) ?? url
}

URLSession.shared.dataTask(with: URLRequest(url: url)) { jsonData, response, error in
newSourceURL = url

urlSessionTask = URLSession.shared.dataTask(with: URLRequest(url: url)) { jsonData, response, error in
guard error == nil,
((response as? HTTPURLResponse)?.statusCode ?? 200) == 200,
let jsonData = jsonData else {
Expand All @@ -290,7 +302,9 @@ struct AddSourceView: View {
self.sourceValidationState = .badjson
}
}
}.resume()
}

urlSessionTask?.resume()

sourceValidationState = .checking

Expand Down

0 comments on commit 1168ca8

Please sign in to comment.