I have just started playing around with Narek Adonts' Background Task library and it seems to be really impressive. So good in fact that I thought I might be missing some significant side-effects of using the library. Are there any?
I know the background task can't update the UI but is there a thread safe way of sharing a variable between the background and UI threads so that the following would be safe?
B4X:
Dim myBGTask As BackgroundTask
Dim progress as long
Dim timer1 As Timer
Sub MyTask
for i=1 to aBigNumber
doSomeHeavyDutyStuff
progress=i
next
End Sub
'runs in UI thread
Sub go
timer1.Initialize("timer1",500)
timer1.Enabled=True
myBGTask.Initialize("MyTask",Me,Array(Null))
End Sub
Sub timer1_Tick
Label1.text=progress
End Sub
Even if this is not possible, the Background Task library seems to be very useful in keeping the UI thread functional.
Answering my second question myself, adding the following to Narek's class seems to work.
B4X:
public Sub callUISub(subName As String, msg As String)
No(Me).RunMethod("callUISub::", Array(subName, msg))
End Sub
-(void)callUISub: (NSString*) subName :(NSString*)msg
{
dispatch_async(dispatch_get_main_queue(), ^(void){
[self.__c CallSub2:self.bi :self :subName :msg];
});
}
The background task would use callUISub to send updates to the UI thread.
I am still interested in my first question. To me, Narek's class is so useful that I'm not sure why there isn't more discussion about it. Perhaps it just works, and everyone is using it?