Я недостаточно хорошо знаю RxSwift и не могу самостоятельно разобраться в своей проблеме. Буду очень признателен за помощь. Извините за мой английский.
//declare variables
var loadedImage = BehaviorRelay<UIImage?>(value: nil)
//Subscribe when creating a cell
loadedImage
.do(onNext: { (_) in
cell.activityIndicator.startAnimating()
cell.activityIndicator.isHidden = false
})
.flatMap { (image) -> Observable<String> in
guard let image = image else { return Observable.just("") }
return service.uploadPhoto(image: image)
}
.observeOn(MainScheduler.instance)
.subscribeNext { (imageName) in
vc.createEventData.imageName = imageName
cell.activityIndicator.stopAnimating()
cell.activityIndicator.isHidden = true
let currentPhoto = self.object?.placeholderPhoto
let image = self.loadedImage.value
cell.photoEventImageView.image = image == nil ? currentPhoto : image
if let photoPath = self.object?.photoPath, image == nil {
if photoPath != "uploads/photos/.png" {
let url = URL(string: "http://test-around.profsoft.online/" + photoPath)
cell.photoEventImageView.kf.setImage(with: url)
if let r = photoPath.range(of: "/", options: .backwards) {
let imageName = photoPath.substring(from: r.upperBound)
vc.createEventData.imageName = imageName
}
}
}
}
.disposed(by: disposeBag)
//I throw a signal when choosing a photo
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey: Any]) {
guard let image = info[.originalImage] as? UIImage else { return }
loadedImage.accept(image)
self.viewController?.dismiss(animated: true, completion: nil)
}
Я не могу понять, почему подписка происходит 2 раза. То есть я захожу в service.uploadPhoto (изображение: изображение) дважды.