ios - Throttle a signal whilst the application is not active -


i'd ensure racsignal sends events while application active. additional next events send in background should dropped, but, if @ least 1 occurs, latest should sent when application enters foreground.

i know can use uiapplicationstate determine current state, , uiapplicationdidbecomeactivenotification determine when application enters foreground. can't use -sample: because events occur in foreground should sent immediately.

the use case is: external device connects , communicates application. based on information provided device, network requests made - should not made in background, , latest data relevant.

this fun figure out i'm going jump straight code , explain why does.

racsignal * backgroundsignal = [nsnotificationcenter.defaultcenter rac_addobserverforname:uiapplicationdidenterbackgroundnotification object:nil]; racsignal * foregroundsignal = [nsnotificationcenter.defaultcenter rac_addobserverforname:uiapplicationwillenterforegroundnotification object:nil]; [[[[[backgroundsignal mapreplace:@no] merge:  [[foregroundsignal mapreplace:@yes] delay:0.1]] startwith:@yes]   map:^racstream *(nsnumber * value) {     if (value.boolvalue)     {         return [[racsignal interval:1 onscheduler:[racscheduler scheduler]] mapreplace:@"foreground"];     }     return [[[racsignal interval:10 onscheduler:[racscheduler scheduler]] mapreplace:@"background"] sample:foregroundsignal];  }].switchtolatest subscribenext:^(id x) {     nslog(@"%@", x); }]; 

so we're using both foreground , background signals boolean flags whether or not we're in foreground, pretty vanilla stuff here.

to avoid signal switching (and our latest signal background being lost), we're delaying foreground signal 0.1 seconds. can tune value taste, it's necessary evil.


Comments

Popular posts from this blog

toolbar - How to add link to user registration inside toobar in admin joomla 3 custom component -

linux - disk space limitation when creating war file -