Major performance improvements to prevent crashes and lag when working with large label collections: Main App: - Switch to Core Data as primary source for labels (instant loading) - Implement background API sync to keep labels up-to-date - Add LazyVStack for efficient rendering of large label lists - Use batch operations instead of individual queries (1 query vs 1000) - Generate unique IDs for local labels to prevent duplicate warnings Share Extension: - Convert getTags() to async with background context - Add saveTags() method with batch insert support - Load labels from Core Data first, then sync with API - Remove duplicate server reachability checks - Reduce memory usage and prevent UI freezes Technical Details: - Labels now load instantly from local cache - API sync happens in background without blocking UI - Batch fetch operations for optimal database performance - Proper error handling for offline scenarios - Fixed duplicate ID warnings in ForEach loops Fixes crashes and lag reported by users with 1000+ labels.
103 lines
3.6 KiB
Swift
103 lines
3.6 KiB
Swift
import Foundation
|
|
import CoreData
|
|
|
|
class OfflineBookmarkManager: @unchecked Sendable {
|
|
static let shared = OfflineBookmarkManager()
|
|
|
|
private init() {}
|
|
|
|
// MARK: - Core Data Stack for Share Extension
|
|
|
|
var context: NSManagedObjectContext {
|
|
return CoreDataManager.shared.context
|
|
}
|
|
|
|
// MARK: - Offline Storage Methods
|
|
|
|
func saveOfflineBookmark(url: String, title: String = "", tags: [String] = []) -> Bool {
|
|
let tagsString = tags.joined(separator: ",")
|
|
|
|
do {
|
|
try context.safePerform { [weak self] in
|
|
guard let self = self else { return }
|
|
|
|
// Check if URL already exists offline
|
|
let fetchRequest: NSFetchRequest<ArticleURLEntity> = ArticleURLEntity.fetchRequest()
|
|
fetchRequest.predicate = NSPredicate(format: "url == %@", url)
|
|
|
|
let existingEntities = try self.context.fetch(fetchRequest)
|
|
if let existingEntity = existingEntities.first {
|
|
// Update existing entry
|
|
existingEntity.tags = tagsString
|
|
existingEntity.title = title
|
|
} else {
|
|
// Create new entry
|
|
let entity = ArticleURLEntity(context: self.context)
|
|
entity.id = UUID()
|
|
entity.url = url
|
|
entity.title = title
|
|
entity.tags = tagsString
|
|
}
|
|
|
|
try self.context.save()
|
|
print("Bookmark saved offline: \(url)")
|
|
}
|
|
return true
|
|
} catch {
|
|
print("Failed to save offline bookmark: \(error)")
|
|
return false
|
|
}
|
|
}
|
|
|
|
func getTags() async -> [String] {
|
|
let backgroundContext = CoreDataManager.shared.newBackgroundContext()
|
|
|
|
do {
|
|
return try await backgroundContext.perform {
|
|
let fetchRequest: NSFetchRequest<TagEntity> = TagEntity.fetchRequest()
|
|
fetchRequest.sortDescriptors = [NSSortDescriptor(key: "name", ascending: true)]
|
|
|
|
let tagEntities = try backgroundContext.fetch(fetchRequest)
|
|
return tagEntities.compactMap { $0.name }
|
|
}
|
|
} catch {
|
|
print("Failed to fetch tags: \(error)")
|
|
return []
|
|
}
|
|
}
|
|
|
|
func saveTags(_ tags: [String]) async {
|
|
let backgroundContext = CoreDataManager.shared.newBackgroundContext()
|
|
|
|
do {
|
|
try await backgroundContext.perform {
|
|
// Batch fetch existing tags
|
|
let fetchRequest: NSFetchRequest<TagEntity> = TagEntity.fetchRequest()
|
|
fetchRequest.propertiesToFetch = ["name"]
|
|
|
|
let existingEntities = try backgroundContext.fetch(fetchRequest)
|
|
let existingNames = Set(existingEntities.compactMap { $0.name })
|
|
|
|
// Only insert new tags
|
|
var insertCount = 0
|
|
for tag in tags {
|
|
if !existingNames.contains(tag) {
|
|
let entity = TagEntity(context: backgroundContext)
|
|
entity.name = tag
|
|
insertCount += 1
|
|
}
|
|
}
|
|
|
|
// Only save if there are new tags
|
|
if insertCount > 0 {
|
|
try backgroundContext.save()
|
|
print("Saved \(insertCount) new tags to Core Data")
|
|
}
|
|
}
|
|
} catch {
|
|
print("Failed to save tags: \(error)")
|
|
}
|
|
}
|
|
|
|
}
|