UIAapplication.registerForRemoteNotifications()는 기본 스레드에서만 호출해야 합니다.
푸시(원격) 알림 등록 중 오류/경고 표시 Xcode 9 (iOS 11)
여기 오류 메시지가 있습니다.
여기 코드가 있어요 시도해봤어요
let center = UNUserNotificationCenter.current()
center.delegate = self
center.requestAuthorization(options: [.sound, .alert, .badge]) { (granted, error) in
if error == nil{
UIApplication.shared.registerForRemoteNotifications()
}
}
오류/경고 줄:
UI 어플리케이션.shared.register원격 통지용()
이 문제를 해결하는 방법은?
인스피드4
다음을 통해 이 문제를 해결할 수 있습니다.
DispatchQueue.main.async {
UIApplication.shared.registerForRemoteNotifications()
}
이게 도움이 되기를...
Objective C의 경우 아래 코드가 작동합니다.
dispatch_async(dispatch_get_main_queue(), ^{
[[UIApplication sharedApplication] registerForRemoteNotifications];
});
TL;DR:
모든 UI 조작은 기본 스레드에서 수행해야 문제가 발생하지 않습니다.이렇게 하지 못하면 메인 쓰레드 체커(Xcode 9에 새로 도입된 디버깅 기능)에서 런타임에 문제가 발생합니다.따라서 결함과 런타임 경고를 피하기 위해 아래와 같이 메인 쓰레드 블록에 코드를 래핑합니다.
DispatchQueue.main.async {
UIApplication.shared.registerForRemoteNotifications()
}
버전 9 이전의 Xcode 릴리스에서는 메인 스레드와 관련된 경고가 콘솔 영역에 텍스트로 인쇄됩니다.어쨌든 선택적으로 Edit Scheme의 Diagnostic 설정에서 기본 스레드 체커를 비활성화(권장되는 접근 방식이 아님)할 수 있습니다.
설명:
애플은 Xcode 9에 UI 요소를 조작하는 Runtime for UIKit 및 기타 API의 문제를 확인하기 위한 새로운 디버깅 옵션을 도입했습니다.메인 쓰레드 블록이 없는 런타임에 UIKit API에서 UI 요소가 변경되면 UI 결함 및 크래시가 발생할 가능성이 높습니다.기본 쓰레드 체커는 기본적으로 실행 중에 이러한 문제를 탐지할 수 있도록 설정되어 있습니다.아래와 같이 Edit Scheme 창에서 Main Thread Checker를 비활성화할 수 있습니다.

이전 SDK 또는 Frameworks가 있는 경우 Xcode 9로 업데이트할 때 UIKit 메서드 호출 중 일부가 메인 스레드로 랩핑되지 않았기 때문에 이 경고가 발생할 수 있습니다.최신 버전으로 업데이트하면 문제가 해결됩니다(개발자가 알고 수정한 경우).
Xcode 9 베타 릴리즈 노트에서 인용한 내용:
- Xcode 9의 새로운 기능 – 메인 스레드 체커.
- 백그라운드 스레드에서 UI API 오용 탐지 사용
- 메인 스레드에서 이루어지지 않는 AppKit, UIKit 및 WebKit 메서드 호출을 탐지합니다.
- 디버깅하는 동안 자동으로 활성화되며, 스킴 편집기의 진단 탭에서 비활성화할 수 있습니다.
- 메인 스레드 체커는 스위프트 및 C 언어와 함께 작동합니다.
dispatch 라는합니다라는 가 꽤 분명합니다.registerForRemoteNotifications
나는 그것을 사용할 것입니다.granted 및 error에 따라서
center.requestAuthorization(options: [.sound, .alert, .badge]) { (granted, error) in
if granted {
DispatchQueue.main.async {
UIApplication.shared.registerForRemoteNotifications()
}
} else {
print(error!)
// handle the error
}
}
순식간에 4,5로
DispatchQueue.main.async {
UIApplication.shared.registerForRemoteNotifications()
}
Objectiv-C에
dispatch_async(dispatch_get_main_queue(), ^{
[[UIApplication sharedApplication] registerForRemoteNotifications];
});
이 방법은 Swift 4.0에서도 올바른 방법입니다.
UNUserNotificationCenter.current().delegate = self
UNUserNotificationCenter.current().requestAuthorization(options: [.alert,.sound,.badge], completionHandler: {(granted,error) in
if granted{
DispatchQueue.main.async {
application.registerForRemoteNotifications()
}
}
})
이것이 도움이 되기를 바랍니다.
DispatchQueue.main.async(execute: {
UIApplication.shared.registerForRemoteNotifications()
})
이것이 저에게 효과가 있었던 것입니다.@Mason11987님께 감사드립니다.
DispatchQueue.main.async() { code }
언급URL : https://stackoverflow.com/questions/44391367/uiapplication-registerforremotenotifications-must-be-called-from-main-thread-o
'programing' 카테고리의 다른 글
| C/C++에서 두 개의 '주요' (0) | 2023.09.21 |
|---|---|
| 어떻게 하면 중앙에 유연한 기둥이 하나 있는 고정 폭 기둥 두 개를 가질 수 있습니까? (0) | 2023.09.21 |
| 띄어쓰기/탭/새 선 - python (0) | 2023.09.21 |
| Github에서 프로젝트의 라이센스를 변경하는 방법은? (0) | 2023.09.21 |
| dataType json의 jQuery $.ajax 요청이 PHP 스크립트에서 데이터를 검색하지 않습니다. (0) | 2023.09.21 |
