diff --git a/dimensionhub/Views/Index/IndexMainView.swift b/dimensionhub/Views/Index/IndexMainView.swift index 8c33739..1a1035e 100644 --- a/dimensionhub/Views/Index/IndexMainView.swift +++ b/dimensionhub/Views/Index/IndexMainView.swift @@ -112,7 +112,8 @@ struct IndexMainView: View { // 上拉刷新功能 self.headerRefreshing = true - Task { + + Task { @MainActor in await self.indexModel.loadPrevUpdateDramasTask(userId: self.userId) { anchorGroupElement in DispatchQueue.main.async { self.headerRefreshing = false @@ -140,7 +141,7 @@ struct IndexMainView: View { .ignoresSafeArea(edges: .bottom) .popover(isPresented: $showDateNavPopover) { DateNavView(selectGroupId: self.$selectGroupId, showDateNavPopover: $showDateNavPopover) { selectedDate in - Task { + Task { @MainActor in await indexModel.loadDateUpdateDramas(userId: self.userId, date: selectedDate) } } @@ -148,10 +149,10 @@ struct IndexMainView: View { .alert(isPresented: $showPrompt) { Alert(title: Text("提示"), message: Text(self.promptMessage), dismissButton: .default(Text("OK"))) } - .task { - await self.indexModel.loadData(userId: self.userId) - - + .onAppear { + Task { @MainActor in + await self.indexModel.loadData(userId: self.userId) + } } } diff --git a/dimensionhub/Views/Index/IndexModel.swift b/dimensionhub/Views/Index/IndexModel.swift index ba2fedd..50f6744 100644 --- a/dimensionhub/Views/Index/IndexModel.swift +++ b/dimensionhub/Views/Index/IndexModel.swift @@ -149,7 +149,7 @@ final class IndexModel { let timeDistance = self.updateInterval.distance(to: Date()) // 滑动停止的时候,检测是否到达了底部 if timeDistance > 1.0 && isCloseBottom { - Task { + Task { @MainActor in await self.loadMoreUpdateDramasTask(userId: userId) } self.updateInterval = Date() @@ -195,13 +195,11 @@ final class IndexModel { case .result(let result): preloadGroupImages(groups: result.update_dramas) - await MainActor.run { - self.updateDramaGroups = result.update_dramas - self.fixedDramaGroup = result.update_dramas.first - - self.dramaGroupElements = transformUpdateDramaGroups(groups: result.update_dramas) - self.follow_num = result.follow_num >= 100 ? "99+" : "\(result.follow_num)" - } + self.updateDramaGroups = result.update_dramas + self.fixedDramaGroup = result.update_dramas.first + + self.dramaGroupElements = transformUpdateDramaGroups(groups: result.update_dramas) + self.follow_num = result.follow_num >= 100 ? "99+" : "\(result.follow_num)" } self.isLoaded = true } @@ -212,9 +210,7 @@ final class IndexModel { case .error(let code, let message): print("reloadFollowNum get error: \(code), message: \(message)") case .result(let follow_num): - await MainActor.run { - self.follow_num = follow_num >= 100 ? "99+" : "\(follow_num)" - } + self.follow_num = follow_num >= 100 ? "99+" : "\(follow_num)" } self.isLoaded = true } @@ -245,12 +241,10 @@ final class IndexModel { preloadGroupImages(groups: groups) displayDramaGroups(self.updateDramaGroups, label: "before") - await MainActor.run { - self.updateDramaGroups = appendMergeDramaGroups(groups: self.updateDramaGroups, mergeGroups: groups) - self.fixedDramaGroup = self.updateDramaGroups.first - - self.dramaGroupElements = transformUpdateDramaGroups(groups: self.updateDramaGroups) - } + self.updateDramaGroups = appendMergeDramaGroups(groups: self.updateDramaGroups, mergeGroups: groups) + self.fixedDramaGroup = self.updateDramaGroups.first + + self.dramaGroupElements = transformUpdateDramaGroups(groups: self.updateDramaGroups) displayDramaGroups(self.updateDramaGroups, label: "after") } @@ -278,14 +272,12 @@ final class IndexModel { preloadGroupImages(groups: groups) displayDramaGroups(self.updateDramaGroups, label: "before") - await MainActor.run { - self.updateDramaGroups = preappendMergeDramaGroups(groups: self.updateDramaGroups, mergeGroups: groups) - self.fixedDramaGroup = self.updateDramaGroups.first - - self.dramaGroupElements = transformUpdateDramaGroups(groups: self.updateDramaGroups) - // 处理回调 - callback(anchorGroupElement) - } + self.updateDramaGroups = preappendMergeDramaGroups(groups: self.updateDramaGroups, mergeGroups: groups) + self.fixedDramaGroup = self.updateDramaGroups.first + + self.dramaGroupElements = transformUpdateDramaGroups(groups: self.updateDramaGroups) + // 处理回调 + callback(anchorGroupElement) displayDramaGroups(self.updateDramaGroups, label: "after") } } @@ -329,11 +321,9 @@ final class IndexModel { if case let .result(groups) = response { preloadGroupImages(groups: groups) - await MainActor.run { - self.updateDramaGroups = groups - self.dramaGroupElements = transformUpdateDramaGroups(groups: self.updateDramaGroups) - self.fixedDramaGroup = groups.first - } + self.updateDramaGroups = groups + self.dramaGroupElements = transformUpdateDramaGroups(groups: self.updateDramaGroups) + self.fixedDramaGroup = groups.first } }