2016-03-25 8 views
0

tarafından üretilen EventProducerType çerçevesinden SwiftBond çerçevesinin oluşturduğu olayı tetiklemek istiyorum. İşteThrottle olayı, EventProducerType - Swift Bond

Bir etkinliği azaltmasını nasıl test ediyorum nasıl: Bu yaklaşımla

var throttledObserver: EventProducer<String?>! 

init() { 
    throttledObserver = Observable<String?>(nil).throttle(1000, queue: Queue.Main)  

    throttledObserver.observeNew { text in 
    // This is always printed no matter how large the throttle time interval is 
    print(text) 
    } 
} 

// UISearchBarDelegate method 
func searchBar(searchBar: UISearchBar, textDidChange searchText: String) { 
    throttledObserver.next(searchText) 
} 

, hiçbir azaltma ve metin metin değişikliği hemen sonra yazdırılır. Hedefim, bir UISearchBar’ın arama metnini, ağ taleplerinin bir son noktadan bazı verileri almasını sağlamaktır.

+0

aşağıdaki çalışmak için aldım: –

cevap

0

Ben şu çalışmak lazım:

var throttledObserver = Observable<String?>! 

init() { 
    throttledObserver = Observable<String?>(nil) 

    // the key is not to save the returned value from throttle into a variable, but to set the observation immediately 
    throttledObserver.throttle(1000, queue: Queue.Main) 
    .observeNew { text in 
     print(text) 
    } 
} 

// UISearchBarDelegate method 
func searchBar(searchBar: UISearchBar, textDidChange searchText: String) { 
    throttledObserver.next(searchText) 
}