메뉴 건너뛰기

정보자료게시판

로그인 과정을 비동기화로 했더니 발생하는 문제

by Summa posted Sep 11, 2024
?

단축키

Prev이전 문서

Next다음 문서

크게 작게 위로 아래로 댓글로 가기 인쇄
?

단축키

Prev이전 문서

Next다음 문서

크게 작게 위로 아래로 댓글로 가기 인쇄

빈화면의 뷰가 먼저 나온다.

 

코파일럿에게

 

func loginInfo(parameters: [String: Any], completion: @escaping () -> Void) { // func createPost(parameters: [String: Any]) { guard let url = URL(string: "\(prefixURL)/login") else { print("Not Found URL") return } let data = try! JSONSerialization.data(withJSONObject: parameters) var request = URLRequest(url: url) request.httpMethod = "POST" request.httpBody = data request.setValue("application/json", forHTTPHeaderField: "Content-Type") URLSession.shared.dataTask(with: request) { (data, res, error) in if(error != nil) { print("error", error?.localizedDescription ?? "") return } do { if let data = data { let result = try JSONDecoder().decode(apikeyModel.self,from: data) DispatchQueue.main.async { let value = result.apikey print(result) print(value) UserDefaults.standard.set(value, forKey: "apikey") } } else { print("No Data") } completion() } catch let JsonError { print("fetch json error:", JsonError.localizedDescription) } }.resume() }

 

동기 버전으로 바꿔줘

 

라고 요청했다.

 

코파일럿이

 

func loginInfoSync(parameters: [String: Any]) -> Bool {
    guard let url = URL(string: "\(prefixURL)/login") else {
        print("Not Found URL")
        return false
    }
    
    let data = try! JSONSerialization.data(withJSONObject: parameters)
    var request = URLRequest(url: url)
    request.httpMethod = "POST"
    request.httpBody = data
    request.setValue("application/json", forHTTPHeaderField: "Content-Type")
    
    let semaphore = DispatchSemaphore(value: 0)
    var success = false
    
    URLSession.shared.dataTask(with: request) { (data, res, error) in
        if let error = error {
            print("error", error.localizedDescription)
            semaphore.signal()
            return
        }
        
        do {
            if let data = data {
                let result = try JSONDecoder().decode(apikeyModel.self, from: data)
                let value = result.apikey
                print(result)
                print(value)
                UserDefaults.standard.set(value, forKey: "apikey")
                success = true
            } else {
                print("No Data")
            }
        } catch let jsonError {
            print("fetch json error:", jsonError.localizedDescription)
        }
        semaphore.signal()
    }.resume()
    
    semaphore.wait()
    return success
}
 

요렇게 하라고 한다.

 

했더니 된다. 헐~~~

 

 

 


  1. notice by Summa 2025/07/22 by Summa

    각종 사진들

  2. No Image notice by Summa 2025/04/02 by Summa

    꾸준히 하는 취미를 가져보기로 한다

  3. No Image 11Aug
    by Summa
    2025/08/11 by Summa

    클라우드 플레어 방화벽 설정

  4. 홈페이지 속도 지연 feat. 클라우드플레어

  5. No Image 27May
    by Summa
    2025/05/27 by Summa

    메일 서버 시놀로지 메일플러스의 구조

  6. No Image 23May
    by Summa
    2025/05/23 by Summa
    Replies 2

    라이믹스 홈페이지들 상태 이상

  7. No Image 20May
    by Summa
    2025/05/20 by Summa

    SSL 서비스에 있어 클라우드 플레어의 놀라운 점

  8. No Image 17May
    by Summa
    2025/05/17 by Summa

    전기요금 아끼는 방법 - 전기요금 절약 프로젝트

  9. No Image 16May
    by Summa
    2025/05/16 by Summa

    구글 제미나이 오류

  10. No Image 15May
    by Summa
    2025/05/15 by Summa
    Replies 1

    KT 인터넷 전화기 IP375 초기화하기

  11. 냉장고 교체 전력소비량 feat. 오래된 냉장고

  12. No Image 15Apr
    by Summa
    2025/04/15 by Summa

    삼성 냉장고 와이파이 연결 안됨 feat. 스마트싱스

  13. 01Apr
    by Summa
    2025/04/01 by Summa

    홈팟미니 이상 해결

  14. No Image 31Mar
    by Summa
    2025/03/31 by Summa

    맥 Numbers 넘버스에서 홈택스 txt 파일 올리기

  15. No Image 26Mar
    by Summa
    2025/03/26 by Summa

    아이폰 듀얼심 문자 안될때 해결 방법

  16. 시놀로지 클라우드플레어 DDNS 설정

  17. 집밥 그리고 밥솥

  18. 단렌즈를 포기하게 된 결정적 이유

  19. 공부폰 / 공신폰 만드는 방법

  20. No Image 03Feb
    by Summa
    2025/02/03 by Summa

    2번 서버 다운

  21. 워드프레스 멀티사이트 만들기

  22. 올해 산것 중 제일 잘 산것 - 전기자전거

Board Pagination Prev 1 2 3 4 5 6 7 8 9 10 ... 47 Next
/ 47
위로