Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

I want to create the data model asynchronously. I am using MVVM. My code looks like this.

class Page (code behind):

public partial class PageMusicPlayerView : TabbedPage
    public PageMusicPlayerView()
        InitializeComponent();
        PageMusicPlayerViewModel viewModel = PageMusicPlayerViewModel.GetInstance();
        BindingContext = viewModel;
    protected override async void OnAppearing()
        await PageMusicPlayerViewModel.GetInstance().InitAsync();

class PageMusicPlayerViewModel (view model):

public class PageMusicPlayerViewModel : NotifyProperty
    private static PageMusicPlayerViewModel instance = null;
    public static PageMusicPlayerViewModel GetInstance()
        if (instance == null)
            instance = new PageMusicPlayerViewModel();
        return instance;
    public MusicPlaylistModel MusicPlaylist { get; set; } = MusicPlaylistModel.GetInstance();
    public async Task InitAsync()
        await MusicPlaylist.InitAsync();

class MusicPlaylistModel (model):

public class MusicPlaylistModel : NotifyProperty
    private static MusicPlaylistModel instance = null;
    ObservableCollection<MusicItemModel> _listMusic;
    public ObservableCollection<MusicItemModel> ListMusic
        get { return _listMusic; }
        set { SetProperty(ref _listMusic, value); }
    public static MusicPlaylistModel GetInstance()
        if(instance == null)
            instance = new MusicPlaylistModel();
        return instance;
    private MusicPlaylistModel()
        ListMusic = new ObservableCollection<MusicItemModel>();
    public async Task InitAsync()
        int size = 8000;
        for(int i = 0;i<size;i++)
            ListMusic.Add(new MusicItemModel { SoundName = "TEST" });

class MusicItemModel (model):

public class MusicItemModel : NotifyProperty
    long _id = -1;
    string _soundName = string.Empty;
    string _soundDuration = string.Empty;
    bool _cached = false;
    string _downloadingLabel = string.Empty;
    public long Id
        get { return _id; }
        set { SetProperty(ref _id, value); }
    public string SoundName
        get { return _soundName; }
        set { SetProperty(ref _soundName, value); }
    public string SoundDuration
        get { return _soundDuration; }
        set { SetProperty(ref _soundDuration, value); }
    public bool Cached
        get { return _cached; }
        set { SetProperty(ref _cached, value); }
    [NotMapped]
    public string DownloadingLabel
        get { return _downloadingLabel; }
            SetProperty(ref _downloadingLabel, value); 

Model ListMusic is bound to a view. I provide the MusicItemModel class as is, everything else is reduced to a minimal example. I am getting errors while running the application. Also, the debugger starts to freeze a lot:

Explicit concurrent copying GC freed 9420(1219KB) AllocSpace objects, 0(0B) LOS objects, 71% free, 2406KB/8550KB, paused 33us total 10.883ms
Explicit concurrent copying GC freed 717(39KB) AllocSpace objects, 0(0B) LOS objects, 72% free, 2382KB/8526KB, paused 24us total 8.730ms
Explicit concurrent copying GC freed 15(48KB) AllocSpace objects, 0(0B) LOS objects, 72% free, 2382KB/8526KB, paused 20us total 8.058ms
Explicit concurrent copying GC freed 3(16KB) AllocSpace objects, 0(0B) LOS objects, 72% free, 2382KB/8526KB, paused 24us total 8.521ms
Explicit concurrent copying GC freed 6(32KB) AllocSpace objects, 0(0B) LOS objects, 72% free, 2382KB/8526KB, paused 24us total 8.279ms
Explicit concurrent copying GC freed 3(16KB) AllocSpace objects, 0(0B) LOS objects, 72% free, 2382KB/8526KB, paused 22us total 8.190ms
Explicit concurrent copying GC freed 3(16KB) AllocSpace objects, 0(0B) LOS objects, 72% free, 2382KB/8526KB, paused 22us total 8.067ms
Explicit concurrent copying GC freed 32(48KB) AllocSpace objects, 0(0B) LOS objects, 72% free, 2382KB/8526KB, paused 22us total 7.973ms

Errors are only under debugging in Visual Studio. If I run a debug build without debugging everything works fine and fast. Why is this happening? Android 10, Huawei P30.

P.S.: If I reduce the size of the variable size to 1000 and below, then the problems go away. But the amount of my data in the real sample is 8276 elements.

Those aren’t errors. Those messages are normal. Regarding performance, when debugger is attached certain operations are slower. (No JIT optimization.) Necessary, so that if an exception occurs, debugger can stop at exact line. Do not be concerned about performance when debugger is attached. Bottom line: what you have described is okay, no reason to be concerned. – ToolmakerSteve May 14, 2022 at 17:12 You have an ObservableCollection with 8000 elements. That's the fundamental problem, as you discovered. That represents a whole musical score? Just load some limited part of the music, then "page" to the next part, as needed. Maintain a List<MusicItemModel> AllMusicItems with all 8000. Have a startIndex for the section you are displaying in ObservableCollection ListMusic. With const int MaxItems = ...; controlling how much you load at one time. Make MaxItems smaller while debugging. – ToolmakerSteve May 15, 2022 at 0:41 @ToolmakerSteve The elements of the collection store only the name of the song. The byte array itself is unloaded from the server on demand. The size of this collection is ~ 600 kb. I found the solution to the problem here:. github.com/xamarin/xamarin-android/issues/5752 . It's about hot reload. – Range May 15, 2022 at 11:10 Ah, that is interesting. If you UNCHECK Hot Reload in Tools/Options, does that fix the performance problem? – ToolmakerSteve May 15, 2022 at 18:34

The extreme lagginess with an ObservableCollection having 8000 elements, was due to Hot Reload.

UNCHECK Hot Reload in Tools/Options resolved the performance issue.

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.