I am very stumped with this issue. Basically on iOS my CollectionView will not work - it throws the following error:
Invalid update: invalid number of sections. The number of sections contained in the collection view after the update (2) must be equal to the number of sections contained in the collection view before the update (2), plus or minus the number of sections inserted or deleted (1 inserted, 0 deleted)
I have tried a few existing fixes from StackOverflow with the same error but none of them seem to fit. Here is the code whereby the changes are made to the ObservableCollection:
async Task DisplayMessage(object sender, CustomEventArgs e)
{
Message message = e.MessageSnapshot;
Console.WriteLine("Message Content: " + e.MessageSnapshot.Content);
if (message.TimeStamp.Date != lastMessageTime.Date)
{
lastMessageGroup = new MessageGroup
{
Name = message.TimeStamp.ToString("dd MMM")
};
}
else
{
MessagesList.Remove(lastMessageGroup);
}
lastMessageTime = message.TimeStamp;
Console.WriteLine("Arrived here at least once!");
lastMessageGroup.Add(message);
MessagesList.Add(lastMessageGroup);
Console.WriteLine("Completed at least once!");
}
This snippet is part of a messaging system and so is run by an event firing as a result of a change in the database. It works once the rest of the items have been displayed (at displaying the items one at a time) but initially on page load when the event fires multiple times to populate the view with the existing conversation, the view freezes and you receive this error. Sometimes it will completely crash, sometimes it will throw the error and leave the view empty.
So far I have tried:
- Adding
lock()
statement around the.Remove()
and.Add()
lines to ensure these lines within each event fire are not being run at the same time - Adding
await Task.Delay(1000);
after the.Remove()
line and at the bottom of the function
Any help will be greatly appreciated!