메뉴 건너뛰기

정보자료게시판

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

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. 꾸준히 하는 취미를 가져보기로 한다

    Date2025.04.02 BySumma
    read more
  2. 2025년 연간달력

    Date2024.11.24 Category장비/디자인/구조 BySumma
    Read More
  3. 2025년 대한민국 24절기 구글 캘린더 일정 링크

    Date2024.11.23 BySumma
    Read More
  4. 역방향 프록시에서의 보안 인증서 feat. 갑자기 뜬금없는 webdav 오류

    Date2024.10.22 Category장비/디자인/구조 BySumma
    Read More
  5. 부팅 중 컴퓨터 로고에서 넘어가지 않는 현상

    Date2024.09.21 BySumma
    Read More
  6. 시놀로지 최대 접속 오류로 ip 차단

    Date2024.09.20 BySumma
    Read More
  7. hp 파빌리온 데스크탑 tg01 오류

    Date2024.09.19 BySumma
    Read More
  8. 스크린타임 설정하기

    Date2024.09.11 BySumma
    Read More
  9. 로그인 과정을 비동기화로 했더니 발생하는 문제

    Date2024.09.11 BySumma
    Read More
  10. 시놀로지 도커 설치 metabase 불안정

    Date2024.08.22 BySumma
    Read More
  11. 시놀로지 Let's Encrypt 인증서 자동갱신하기 feat. 클라우드플레어

    Date2024.08.22 BySumma
    Read More
  12. 시놀로지 도커에서 돌리는 node js에서 모듈 설치하기

    Date2024.08.18 BySumma
    Read More
  13. 워드프레스 카테고리별 태그 관리

    Date2024.07.25 BySumma
    Read More
  14. 설문 응답지 구글폼 설문에서 Smore.im으로 업글

    Date2024.07.21 BySumma
    Read More
  15. [IT새소식] 2024-07-12

    Date2024.07.12 Category새소식 BySumma
    Read More
  16. 사이트 접속 오류 DB 느려짐

    Date2024.06.26 Category장비/디자인/구조 BySumma
    Read More
  17. 사진과 영상을 보관하는 최고의 방법

    Date2024.06.09 BySumma
    Read More
  18. 버추얼박스에 헤놀로지 설치하기

    Date2024.06.03 BySumma
    Read More
  19. 아무도 말해주지 않는 우리가 질병에 걸리는 충격적인 이유

    Date2024.06.03 Category생활/의료/안전 BySumma
    Read More
  20. 미디어위키 설치하기

    Date2024.05.28 BySumma
    Read More
  21. 전기자전거 전국 일주 계획

    Date2024.05.05 Category여행/맛집/음식 BySumma
    Read More
Board Pagination Prev 1 2 3 4 5 6 7 8 9 10 ... 47 Next
/ 47
위로