From db3bf48f7ec27a45bd92c064e32cc7c666948691 Mon Sep 17 00:00:00 2001 From: Simon Risse <9327096+r1sim@users.noreply.github.com> Date: Sat, 13 Sep 2025 19:45:08 +0200 Subject: [PATCH 1/2] perf: use `LazyVStack` for music queue This significantly reduces the memory usage --- flo/PlayerView.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/flo/PlayerView.swift b/flo/PlayerView.swift index 36345aa..a770592 100644 --- a/flo/PlayerView.swift +++ b/flo/PlayerView.swift @@ -97,7 +97,7 @@ struct PlayerView: View { .padding(.bottom, 5) ScrollView { - VStack(alignment: .leading) { + LazyVStack(alignment: .leading) { ForEach(viewModel.queue.indices, id: \.self) { idx in HStack(alignment: .top) { VStack(alignment: .leading) { From a30b3102bff66d5078362e4aa4e3a6233647259d Mon Sep 17 00:00:00 2001 From: Simon Risse <9327096+r1sim@users.noreply.github.com> Date: Sat, 13 Sep 2025 19:47:53 +0200 Subject: [PATCH 2/2] perf: use batch api in addToQueue The batch api is a lot faster when inserting many of items --- flo/Shared/Services/PlaybackService.swift | 31 ++++++++++++----------- 1 file changed, 16 insertions(+), 15 deletions(-) diff --git a/flo/Shared/Services/PlaybackService.swift b/flo/Shared/Services/PlaybackService.swift index 2d5ca52..0a4e5bb 100644 --- a/flo/Shared/Services/PlaybackService.swift +++ b/flo/Shared/Services/PlaybackService.swift @@ -31,23 +31,24 @@ class PlaybackService { func addToQueue(item: T, isFromLocal: Bool = false) -> [QueueEntity] { self.clearQueue() - for song in item.songs { - let queue = QueueEntity(context: CoreDataManager.shared.viewContext) - - queue.id = song.mediaFileId == "" ? song.id : song.mediaFileId - queue.albumId = song.albumId - queue.albumName = item.name - queue.artistName = song.artist - queue.bitRate = Int16(song.bitRate) - queue.sampleRate = Int32(song.sampleRate) - queue.songName = song.title - queue.suffix = song.suffix - queue.isFromLocal = isFromLocal - queue.duration = song.duration - - CoreDataManager.shared.saveRecord() + let objects = item.songs.map { song in + return [ + "id": song.mediaFileId == "" ? song.id : song.mediaFileId, + "albumId": song.albumId, + "albumName": item.name, + "artistName": song.artist, + "bitRate": song.bitRate, + "sampleRate": song.sampleRate, + "songName": song.title, + "suffix": song.suffix, + "isFromLocal": isFromLocal, + "duration": song.duration + ] as [String : Any] } + let request = NSBatchInsertRequest(entity: QueueEntity.entity(), objects: objects) + _ = try? CoreDataManager.shared.viewContext.execute(request) + return self.getQueue() } }