RxSwiftCommunity / RxCoreData

RxSwift extensions for Core Data
MIT License
164 stars 68 forks source link

rx.entities.asSingle() won't have a response to subscribe #34

Closed pokk closed 6 years ago

pokk commented 6 years ago

This is a good library!

Here is a problem about transforming from Observable to Single object. The code as following below

var rx = coreDataContext.rx
            .entities(InformationEntity.self, sortDescriptors: [NSSortDescriptor(key: INFO.UPDATE_DATE, ascending: false)])
            .map { list -> FakeEntity in
                var entity = FakeEntity()
                entity.infoList = list
                return entity
            }
            .asSingle()  // ⇐ doesn't work well.
            .subscribe {
                switch $0 {
                    case .next(let entity):
                        print(entity)
                    case .completed:
                        print("good")
                    case .error(let error):
                        print(error)
                }

I don't know if my usage has problems or not. If I don't use asSingle(), it works well. I just learn swift a little, sorry that I still don't understand swift well. Just let you know there's a problem about asSingle(). Thank you!

marshallxxx commented 6 years ago

Hi @pokk ,

asSingle works as expected, refer to docs:

The asSingle operator throws a RxError.noElements or RxError.moreThanOneElement if the source Observable does not emit exactly one element before successfully completing.

In order to make it work as you intend, you can add take(1) before asSingle()

Please let me know if you have any other issues.