ObservableCollection AddRange vs Async Method
Hi. ObservableCollection does not support adding a list at a time, so I implemented my own class that contains methods for working with ranges:
The collection works, but the problem is in the event type NotifyCollectionChangedAction. If you call the Add event, the UI updates the collection perfectly. However, if you update the collection in the aync method, Unity will crash:
At the same time, if you call the reset event, then everything works fine.
Code: Select all
public void AddRange(IEnumerable<T> collection, bool sendResetAction = false)
{
if (collection == null)
throw new ArgumentNullException(nameof(collection) + " is null!");
if (!collection.Any())
return;
CheckReentrancy();
var tempList = new List<T>();
foreach (var item in collection)
{
tempList.Add(item);
Items.Add(item);
}
if (sendResetAction)
InvokeCollectionModificationEvent(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset));
else
InvokeCollectionModificationEvent(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Add, tempList));
}
Code: Select all
public async void ReloadYards()
{
var yards = new List<YardHeader>();
await Task.Delay(100);
for (int i = 0; i < 15; i++)
{
yards.Add(new YardHeader(Guid.Empty, i.ToString(), null));
}
_yardHeaders.AddRange(yards);
}
-
-
sfernandez
Site Admin
- Posts: 2908
- Joined:
Re: ObservableCollection AddRange vs Async Method
Hi,
Modifying a bound collection in a different thread is not supported and can lead to crashes.
But we have plans to support that scenario directly in the binding engine: #2256
In the meantime you will need to enqueue the change notification to the main thread where UI tree is created and updated.
Could you please try that?
Modifying a bound collection in a different thread is not supported and can lead to crashes.
But we have plans to support that scenario directly in the binding engine: #2256
In the meantime you will need to enqueue the change notification to the main thread where UI tree is created and updated.
Could you please try that?
-
-
sfernandez
Site Admin
- Posts: 2908
- Joined:
Re: ObservableCollection AddRange vs Async Method
Regarding this, could you please create a ticket in our bugtracker and attach a crash dump, because we don't want Unity to crash.However, if you update the collection in the aync method, Unity will crash
Re: ObservableCollection AddRange vs Async Method
Doesn't the collection modification happens in the main thread in the above code example since there's no ConfigureAwait(false) ?
Re: ObservableCollection AddRange vs Async Method
Source: "Unity overwrites the default SynchronizationContext with a custom UnitySynchronizationContext and runs all the tasks on the main thread in both Edit and Play modes. To utilize async tasks, you must manually create and handle your own threads with a TaskFactory, as well as use the default SynchronizationContext instead of the Unity version. To listen for enter and exit play mode events to stop the tasks manually, use EditorApplication.playModeStateChanged. However, if you take this approach, most of the Unity scripting APIs are not available to use because you are not using the UnitySynchronizationContext."
The above implementation does not use multithreading. Technically, it's corutine. It might be worth checking the implementation with IEnumerator as well.
The above implementation does not use multithreading. Technically, it's corutine. It might be worth checking the implementation with IEnumerator as well.
-
-
sfernandez
Site Admin
- Posts: 2908
- Joined:
Re: ObservableCollection AddRange vs Async Method
Would it be possible to get the crash dump when using the async method?
Who is online
Users browsing this forum: Ahrefs [Bot] and 0 guests