我想创建一个重复功能,使用组合在我的代码中创建一个循环。我注意到,Combine 没有通过这个伟大的 repo 重复发布者:https ://github.com/freak4pc/rxswift-to-combine-cheatsheet 。这是我编写的用于重复 2 个状态的代码。如何将其简化为更具可读性或创建自己的重复功能?
toggleShouldDisplay = Just<Void>(())
.delay(for: 2, scheduler:RunLoop.main)
.map({ _ in
self.shouldDisplay = true
self.didChange.send(())
})
.delay(for: 2, scheduler: RunLoop.main)
.map({ _ in
self.shouldDisplay = false
self.didChange.send(())
})
.setFailureType(to: NSError.self)
.tryMap({ _ in
throw NSError()
})
.retry(.max) // I might hit Int.max if I reduce the delays
.sink(receiveValue: { _ in
//Left empty
})