1

每当我运行我的应用程序并使用我的请求实用程序时,我都会遇到奇怪的崩溃。

这是在 XCode Beta 13.5 上

我应该提到我也在使用 Thread Sanitizer。此错误仅在启用 Thread Sanitizer 时出现。

这是代码:

首先,我有一个按钮,点击时提交数据

Button(action: { Task { await viewModel.submitData() } } ) {
     Text("Log In")
}

这是 submitData 函数

func submitData() async -> Void {
    state = .loading
     
    do {
      let result = try await AuthenticationAPI.login(user: user)
       
      self.state = .success
      self.authenticator.authenticate(token: result.value.token)
    } catch let error as APIError {
      self.handleAPIError(error)
    } catch {
      // TODO: Log Error
      self.handleAPIError(.unexpectedError)
    }
  }

这是 AuthenticationAPI.login 函数

   static func login(user: LoginUser) async throws -> APIService.Response<AuthenticatedUser> {
    let endpoint = Endpoint.login
     
    let data = try? encode(user.toDictionary())
     
    return try await apiService.post(type: AuthenticatedUser.self,
                url: endpoint.url, body: data)
  }

最后这是网络调用

  func executeRequest<T: Decodable>(_ request: URLRequest) async throws -> Response<T> {
    guard let (data, response) = try? await URLSession.shared.data(for: request) else {
        throw APIError.networkError
    }

     
    if let response = response as? HTTPURLResponse {
      if (200...299).contains(response.statusCode) {
        guard let data = try? decoder.decode(T.self, from: data) else {
          throw APIError.decodingError
        }
         
        return Response(value: data, response: response)
      } else if (400...499).contains(response.statusCode) {
        throw APIError.httpError(data, response)
      } else if response.statusCode == 500 {
        throw APIError.serverError
      }
    }
     
    throw APIError.unexpectedError
  }

  func post<T: Decodable>(
    type: T.Type,
    url: URL,
    headers: Headers = [:],
    body: Data?
  ) async throws -> Response<T> {
    var request = URLRequest(url: url)
     
    request.httpMethod = "POST"
    request.httpBody = body
     
    constructHeaders(request: &request, headers: headers)
     
    return try await executeRequest(request) // This is where the compiler tells me the error occurs
   
  }

永远不会调用 executeRequest 函数,因为它永远不会发出请求(我检查了我的本地服务器)。

这些是线程日志:

Thread 1 Queue : com.apple.main-thread (serial)
#0  0x00007fff30bb03f4 in swift::TargetMetadata<swift::InProcess>::isCanonicalStaticallySpecializedGenericMetadata() const ()
#1  0x00007fff30bbc7b1 in swift_checkMetadataState ()
#2  0x00007fff30b713d5 in type metadata completion function for ClosedRange<>.Index ()
#3  0x00007fff30bbf75d in swift::MetadataCacheEntryBase<(anonymous namespace)::GenericCacheEntry, void const*>::doInitialization(swift::ConcurrencyControl&, swift::MetadataCompletionQueueEntry*, swift::MetadataRequest) ()
#4  0x00007fff30bb1941 in _swift_getGenericMetadata(swift::MetadataRequest, void const* const*, swift::TargetTypeContextDescriptor<swift::InProcess> const*) ()
#5  0x00007fff30b8b440 in __swift_instantiateCanonicalPrespecializedGenericMetadata ()
#6  0x00000001017f9e00 in APIService.post<τ_0_0>(type:url:headers:body:) at /Users/jayaikendu/Documents/projects/Xcode - Swift/squizelstudents/Shared/Services/APIService.swift:124
#7  0x00000001017a0dd0 in static AuthenticationAPI.login(user:) at /Users/jayaikendu/Documents/projects/Xcode - Swift/squizelstudents/Shared/Networking/Authentication/AuthenticationAPI.swift:21
#8  0x000000010170b3c0 in LoginViewModel.submitData() at /Users/jayaikendu/Documents/projects/Xcode - Swift/squizelstudents/Shared/App/Login/ViewModels/LoginViewModel.swift:42
#9  0x00000001016317d0 in closure #1 in closure #1 in closure #2 in closure #1 in closure #1 in LoginView.body.getter at /Users/jayaikendu/Documents/projects/Xcode - Swift/squizelstudents/Shared/App/Login/Views/LoginView.swift:26
#10 0x0000000101637960 in partial apply for closure #1 in closure #1 in closure #2 in closure #1 in closure #1 in LoginView.body.getter ()
#11 0x0000000101631ae0 in thunk for @escaping @callee_guaranteed @Sendable @async () -> () ()
#12 0x0000000101637ac0 in partial apply for thunk for @escaping @callee_guaranteed @Sendable @async () -> () ()
#13 0x00000001016353f0 in thunk for @escaping @callee_guaranteed @Sendable @async () -> (@out τ_0_0) ()
#14 0x0000000101637d70 in partial apply for thunk for @escaping @callee_guaranteed @Sendable @async () -> (@out τ_0_0) ()
4

0 回答 0