This isn’t specifically about iOS but as it relates to an iOS project I have posted it here.

I have a Thread on a View that parses JSON data and saves it into an SQLite database. I’m using a Thread so that my UI isn’t interrupted by this process which can take a little while (e.g. sometimes 10 seconds, depending on data size, device, etc). It works away nicely in the background, so I’m happy. :slight_smile:


If I try to refresh my iOSTable, which displays data from the SQLite database, at the end of the parsing routine in the Thread.Run method, I get an exception. I guess that’s because I’m trying to update the UI from within the Thread. So, instead, I have a timer that checks for a flag (View property) to know that the Thread is finished before refreshing the Table and, at the end of Thread.Run, the Thread sets this flag.

That seems a bit clumsy to me. Is there another way to know that my Thread has finished, so that I can update the UI, without using a Timer? I tried also setting a callback method using AddressOf but Invoke(ing) it from Thread.Run causes the error. How can my Thread tell me that it has finished or is setting a flag/property and examining it the only way?

Create a task class. There’s an example included with Xojo, though I personally feel it’s a bit heavy-handed. You would be creating a subclass of Thread.

  1. Define two events: Run and Completed
  2. Define a Timer property. In the constructor, create the timer and attach a method to its Action even using AddHandler and WeakAddressOf. AddressOf will cause a memory leak if you don’t explicitly detach the handler with RemoveHandler when done. This method should raise the Completed event. The timer’s mode should default to off, and its period should be 1.
  3. In the Run event, raise the new Run event and set the timer’s mode to single.

You now have a simple reusable class that can be a drop-in replacement for your existing thread, but adds a Completed event that fires on the main thread when finished.

I think that’s what I was looking for! Thank you Thom. :slight_smile: