Threads in iOS

Do threads in general work in iOS? I’m trying to update a loading screen’s text with what’s currently being loaded. All the code that does the data handling is in the thread’s run event and every few lines I’m calling the AddUserInterfaceUpdate but the text isn’t being updated until the code in the run event is finished. Do I misunderstand how threads work?

Code you are using in the run event to call the add user interface code when you want it to update would be helpful in diagnosing the issue.

This is the code:

loadingDict is a property on the MobileScreen

loadingDict = new Dictionary

//data handling code

loadingDict.Value("LoadingText") = "Loading users..."
me.AddUserInterfaceUpdate(loadingDict) 

//data handling code

loadingDict.Value("LoadingText") = "Loading misc tables..."
me.AddUserInterfaceUpdate(loadingDict)

//data handling code

loadingDict.Value("LoadingText") = "Signing user in..."
me.AddUserInterfaceUpdate(loadingDict)

Going through the debugger, the AddUserInterfaceUpdate only fires once, the last one.

You need to trigger it in the run event at the point where you want it to happen. Here’s some example code I’m using in a run event to update a logging window. There are multiple places in the code I do this for transactions and Exceptions.

LogString = "Sending Job Delete Command to Prinect for Job " + JobName
Me.AddUserInterfaceUpdate("TransactionBlack":LogString)

Corresponding code in UserInterfaceUpdate that fires when called from the run event

For Each update As Dictionary In data
  If update.HasKey("TransactionBlack") Then
    LogTransactions(update.Value("TransactionBlack").StringValue,"")
    
  ElseIf update.HasKey("TransactionBlue") Then
    LogTransactions(update.Value("TransactionBlue").StringValue,"blue")
    
  ElseIf update.HasKey("TransactionGreen") Then
    LogTransactions(update.Value("TransactionGreen").StringValue,"green")
    
  ElseIf update.HasKey("TransactionOrange") Then
    LogTransactions(update.Value("TransactionOrange").StringValue,"orange")
    
  ElseIf update.HasKey("Exception") Then
    LogExceptions(update.Value("Exception").StringValue,"red")
    
  End If
Next

I think that’s what I’m doing, the AddUserInterfaceUpdate just doesn’t fire.

The call to AddUserInterfaceUpdate is in the Run Event

Me.AddUserInterfaceUpdate(Key:Value)

The Dictionary is in the AddUserInterfaceUpdate Event.

For Each update As Dictionary In data
 If update.HasKey(Key) Then
// Do something(update.Value(Key).VariableTypeValue)
 End If
Next

Did you add the AddUserInterfaceUpdate Event to the thread?
I suggest you follow the format I gave you it is straight out of the Docs example

To test the theory that the UserInterfaceUpdate event isn’t firing promptly, is it possible to implement the old “use a timer to push the update to the main thread” trick in iOS?

I don’t actually know about iOS, but before the AddUserInterfaceUpdate feature was added to threads, this was how you had to update the user interface.

I might do it wrong but that creates an even bigger mess as I’m having RunTimeExceptions in the data handling code now.

The event contains an array of all the passed dictionaries. It could be that the thread completes too fast, or that the loading routines don’t yield. If the event that does fire contains all three members - which will all be identical since you’re changing a single dictionary - that’s probably what is happening.

1 Like

Thanks for your replies. I won’t bother with it, I’ll find a workaround that works rather than spending two more days trying to find out why the event doesn’t fire. As usual.