📄
목차
Error
// 로그창
Error Domain=NSURLErrorDomain Code=-999 "cancelled"
발생 상황
Combine
으로 네트워크 통신을 구현해보려고 시도하다가 발생한 문제.
var cancellables = Set<AnyCancellable>()
AnyPublisher<Data, Error>
. ( 각종 메소드 ... )
.sink ( ... )
.store(in: &cancellables)
- 다음과 같이 코드를 작성했는데 에러가 발생했다.
원인
sink
로 구독한 후에 반환되는AnyCancellable
를 메모리에 올리지 않아 바로 구독이 해지되어 생기는 문제. → 때문에 변수로 설정한 후에 메모리에 올려야 한다.
var cancellable: AnyCancellable?
cancellable = AnyPublisher<Data, Error>
. ( 각종 메소드 ... )
.sink { completion in
switch completion {
case .finished:
...
cancellable?.cancel()
}
}
질문
- 개발자 문서에 보면
store(in: )
가 저장하는 역할도 하는것 같은데 왜 에러가 떴는지 잘 이해가 안간다.deinit
상황에서 호출되는데 deinit이 안되어서 그런가?_?
Reference
Apple Developer Documentation
Swift Combine: finished with error [-999] Error Domain=NSURLErrorDomain Code=-999 "cancelled"
I am trying to fetch data by Restcountries API, but there is a problem. When I try to fetch data the error immediately shows up:
2021-08-24 17:36:44.851463+0200 Countries[1498:19786] Task <3FF6...
![](https://cdn.sstatic.net/Sites/stackoverflow/Img/apple-touch-icon.png?v=c78bd457575a)
![](https://cdn.sstatic.net/Sites/stackoverflow/Img/apple-touch-icon@2.png?v=73d79a89bded)
Uploaded by N2T