0
votes

I am just playing with RxSwift to better understand the concepts. I am trying to validate email. However, the subscriber doesn't call when I return an Observable back. I thought this might work. Any suggestions?

var emailAddressValid: Observable<String>{
        return self.userEmailAddress.asObservable().filter({ (userEmail) -> Bool in
            userEmail.count > 0
        }).flatMap({ (userEmail) -> Observable<String> in
            if userEmail == "[email protected]"{
                //This works perfectly
                return .just("007")
            } else {
                let emailDomain = userEmail.components(separatedBy: "@").last
                if emailDomain != nil {
                    //nothing happens in onNext
                    //This returns back an Observable<String>
                    return DataService.instance.checkDomainIsRegistered(domainName: emailDomain!)
                } else {
                    return .just("0")
                }
            }
        })
    }

Although the app works. However, there isn't any compiler error as well. But the onNext in the Observer doesn't work when I return DataService.instance.checkDomainIsRegistered(domainName: emailDomain!)

func checkDomainIsRegistered(domainName: String) -> Observable<String>{
        print(domainName)
        return Observable<String>.create{ data in
            self._REF_DOMAIN_NAMES.queryOrdered(byChild: "domainName").queryEqual(toValue: domainName).observeSingleEvent(of: .value, with: { (domainNameSnapshot) in
                if(domainNameSnapshot.exists()){
                    print("1")
                    data.onNext("1")
                } else {
                    print("0")
                    data.onNext("0")
                }
            }, withCancel: { (error) in
                data.onError(error)
            })
            data.onCompleted()
            return Disposables.create()
        }
    }
2

2 Answers

1
votes

In your example, the call to

self._REF_DOMAIN_NAMES.queryOrdered(byChild: "domainName")
    .queryEqual(toValue: domainName)
    .observeSingleEvent(of: .value, with: { (domainNameSnapshot) in })

is likely being dispatched to a background thread or queue.

In the meantime, your Observable checkDomainIsRegistered then completes with data.onCompleted() while probably running on the main thread.

The results is that onNext() is never being allowed to be called. You can verify this is happening by temporarily removing the onCompleted().

0
votes

And if called data.onError(error), event stream may be broken. You should catch that.