iOS client for Grain grain.social
ios photography atproto
7
fork

Configure Feed

Select the types of activity you want to include in your feed.

perf: debounce UserDefaults writes in ViewedStoryStorage

Rapid story tapping was writing UserDefaults on every view, O(n²) in
tests and wasteful in production. Coalesce writes with a 1s debounce;
cleanup() still flushes immediately.

+12 -1
+12 -1
Grain/Utilities/ViewedStoryStorage.swift
··· 9 9 private static let urisKey = "viewedStoryUris" 10 10 private static let authorKey = "viewedStoryAuthors" 11 11 12 + private var saveTask: Task<Void, Never>? 13 + 12 14 init() { 13 15 #if DEBUG 14 16 Self.wipeDefaults() ··· 52 54 } else { 53 55 authorLastViewed[authorDid] = createdAt 54 56 } 55 - save() 57 + scheduleSave() 56 58 } 57 59 58 60 /// Check if a specific story has been viewed. ··· 104 106 let decoded = try? JSONDecoder().decode([String: String].self, from: data) 105 107 { 106 108 authorLastViewed = decoded 109 + } 110 + } 111 + 112 + private func scheduleSave() { 113 + saveTask?.cancel() 114 + saveTask = Task { [weak self] in 115 + try? await Task.sleep(for: .seconds(1)) 116 + guard !Task.isCancelled else { return } 117 + self?.save() 107 118 } 108 119 } 109 120