惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Stack Overflow Blog
Stack Overflow Blog
WordPress大学
WordPress大学
罗磊的独立博客
S
Secure Thoughts
Schneier on Security
Schneier on Security
博客园 - Franky
www.infosecurity-magazine.com
www.infosecurity-magazine.com
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
爱范儿
爱范儿
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
Hacker News: Ask HN
Hacker News: Ask HN
PCI Perspectives
PCI Perspectives
Google DeepMind News
Google DeepMind News
S
Security Affairs
SecWiki News
SecWiki News
博客园 - 聂微东
Security Archives - TechRepublic
Security Archives - TechRepublic
Google Online Security Blog
Google Online Security Blog
H
Heimdal Security Blog
S
Security @ Cisco Blogs
Engineering at Meta
Engineering at Meta
C
CXSECURITY Database RSS Feed - CXSecurity.com
Cloudbric
Cloudbric
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
V
Visual Studio Blog
P
Proofpoint News Feed
Project Zero
Project Zero
T
Threat Research - Cisco Blogs
Webroot Blog
Webroot Blog
Blog — PlanetScale
Blog — PlanetScale
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
W
WeLiveSecurity
Last Week in AI
Last Week in AI
月光博客
月光博客
Microsoft Azure Blog
Microsoft Azure Blog
M
MIT News - Artificial intelligence
有赞技术团队
有赞技术团队
S
Securelist
GbyAI
GbyAI
Application and Cybersecurity Blog
Application and Cybersecurity Blog
C
CERT Recently Published Vulnerability Notes
Recent Commits to openclaw:main
Recent Commits to openclaw:main
Cyberwarzone
Cyberwarzone
B
Blog RSS Feed
P
Palo Alto Networks Blog
H
Hacker News: Front Page
D
Docker
雷峰网
雷峰网
Latest news
Latest news
Microsoft Security Blog
Microsoft Security Blog

Swift

都 vibe coding 了,为啥不把 Rust 项目 rewrite 到 Swift ? 应该怎么解读 swiftui 的崩溃报告? FoundationModels 框架实践 - V2EX Swift DocC 构建的博客 - V2EX Swift server 要支棱起来了 - V2EX 求问贴,如果在 swiftUI 中接入 MySQL C client - V2EX Swift 萌新, DispatchQueue 的一个问题 - V2EX 分享一下一个 Web 前端刚学习 Swift 时候的心得博文 - V2EX 5 分钟用 Swift 写个 ripripgrep - V2EX Swift 操作 plist 文件 使用 SwiftUI + TCA 重构 Instagram Clone:从 Flutter 到 SwiftUI 的迁移实践 激发编程激情,轻松掌握 Swift SwiftUI 分享个基于 UICollectionView 封装的数据驱动和声明式组件库 ListKit 问一个关于 CALayer 的资源释放问题 SwiftUI 的 TextEditor 在 macOS Sequoia 15.0 上输入中文时又会这样一跳一跳的了 Swift 中系统 API delegate 参数一定要在当前类实现传 self 吗 ScreenCaptureKit 里面的 SCStreamOutput 没有被顺利触发,是那个步骤错了,求解惑 - V2EX 似乎 SwiftUI app 在 macOS 15 上启动速度变快很多? - V2EX 请教一个关于 Swift 的基础小问题 - V2EX 离线 iOS app 也需要 ICP ? - V2EX 请教 SwiftUI 布局的问题 - V2EX ios 17 widget 无法正确更新 core data - V2EX 有没有 Swift ui 版本的 tailwind css? - V2EX swiftui 里,这里用 hstack,为什么是垂直布局,而不是水平布局? - V2EX 大家觉得 Swift 的参数必须按序提供怎么样? - V2EX Swift 开源的组件库好像不是很多啊 - V2EX Swift 如何像 Python 那样 崩溃了输出 backtrace 或者至少是哪行出错了。。。 - V2EX 请教一下各位 iOSer 巨佬们一个 SwiftUI sheet 组件的问题 - V2EX 请教一下各位 iOSer 巨佬们一个 SwiftUI 中 HStack 的布局问题 - V2EX swiftui 有好用的组件库吗? - V2EX 求问:苹果手机是否无法像安卓一样,可以扫描出本地的音乐? - V2EX macOS 沙盒中如何获得对任意目录的写权限? - V2EX swiftui 中的语法糖疑问 - V2EX 开发 macOS 应用无法访问用户选择的文件夹下的文件 - V2EX 弄了一个 Swift 官方教程的速览版 - V2EX 2024 年了,现在学 SwiftUI 还有必要学 UIkit 吗? - V2EX 大佬们指点下苹果生态下音频相关的这些库都是什么关系? - V2EX 升级到 Xcode 15 之后遇到的 GeneratedAssetSymbols 过不了编译这个问题 - V2EX 现在用 Swift 开发后端成熟了吗? - V2EX Composable SwiftUI - V2EX 2023 年, swiftui 使用 WKWebView 加载 html 字符串,如何自适应 frame 高度? - V2EX 用 network extension 框架开启 Tunnel,怎么能保持后台请求接口,解决请喝杯咖啡 - V2EX 学生党求问各位大佬,一个关于 ios 小组件的问题 - V2EX 比较好的 Swift UI 学习交流群 - V2EX CAID 这垃圾东西是谁在推,一边喊着保护隐私,一边想着法子来跟踪用户 - V2EX Swift 的 Optional 如何比较包装类型的关系呢? - V2EX Alamofire 推荐使用吗? - V2EX 我自己是初学者想找个朋友带我一起写两个工作用的 app 有偿的 - V2EX 现在感觉 Swift 真的被 OC 拖后腿了 - V2EX
Swift 如何优化数据加载方式? - V2EX
0littleboy · 2023-04-13 · via Swift

我有一个用于记录全局数据的类

class GlobalData {
//    static let shared = GlobalData()
    // 不使用单例模式,没有实例,直接调用类
    static var allProblems: [Problem] = getAllProblems()
    static var allContests: [Contest] = getAllContests()
}

问题其一是使用 static 加载速度太慢了,必须等其加载完才能显示 APP 的内容

给出其中一个调用 GlobalData 的样例

struct HistoryContestsView: View {
    @State var contests: [Contest] = GlobalData.allContests
    @State var allProblems: [Problem] = GlobalData.allProblems
    
    var body: some View {
        NavigationView {
            // ...
        }
        .refreshable {
            reloadHistoryContests()
        }
    }
    
    private func reloadHistoryContests() {
        GlobalData.allProblems = getAllProblems()
        allProblems = GlobalData.allProblems
        GlobalData.allContests = getAllContests()
        contests = GlobalData.allContests
        print("Successfully reload history contests")
    }
}

其二是 reloadHistoryContests 内容显得十分冗余

目前想到的策略是定义一个 ObservableObject 类 HistoryContestsView 就可以直接订阅 GlobalData 类 并且在 .task 中异步加载数据,也能解决性能问题

class GlobalData: ObservableObject {
    @Published var allProblems: [Problem] = []
    @Published var allContests: [Contest] = []

    func loadData() {
        // 加载数据的代码
        // ...
        self.allProblems = getAllProblems()
        self.allContests = getAllContests()
    }
}

但又出现了个问题,在其他 model 文件中,原本调用 GlobalData.allContests 的函数,都无法使用了

func getPredictiveContestList(by userid: String) -> [User.AttendedContest] {
        let predictiveContests = Global.allContests.filter {
            $0.avgRatingOfProblems == nil
        }
    //    print(predictiveContests)
        var res: [User.AttendedContest] = []
        for contest in predictiveContests {
            /// 参加了且有了预测结果
            // TODO - 后面统计参加了还没有结果的
            if let tmp = predictRatingOfUser(by: contest.titleSlug, userid: userid) {
                res.append(tmp)
            }
        }
        
        return res
    }

如何解决?