'How can I force ProgressView to run its task again?

I have content that is loaded in the background. Loading is a long running task and I want to show data as soon as it's available. My view show loaded content and a ProgressView whenever there is more content to be expected.

struct MyListView : View
{
  @MyContent
  var content: [MyItem]

  var body: some View
  {
    List
    {
      //...show content elements
      if content.hasMoreData()
      {
         ProgressView().task
         {
            await _content.load(.end)
         }
      }
    }
  }
}

I use a custom propertyWrapper to load the data.

@propertyWrapper
struct MyContent<E> : DynamicProperty
{
  final class Container<E> : ObservableObject
  {
    var wrappedValue : [E]
  }
  
  let container: Container<E>
  var wrappedValue : [E] { container.wrappedValue }
 
  func load() async
  {
     //...load more content
  }
}

When the view loads, the ProgressView spins and the load function is called. After more data has been loaded the view is refreshed. Unfortunately however the task on the ProgressView is not renewed. The load function is not called again.

I also tried wrapping the MyContent wrapper in an ObservableObject but with similar effects.

final class MyBox<E> : ObservableObject
{
  @MyContent
  var content: [MyItem]

  func load() async
  {
    await _content.load(position)
    await send()
  }

  @MainActor
  private func send() async
  {
    objectWillChange.send()
  }
}

If I look at FetchRequest which is a struct and which has a batchLimit, I think it should not be necessary to use MyBox or an ObservableObject' just to trigger and additional load` call.

How can I force the ProgressView to run the task again?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source