Skip to content

Commit

Permalink
Fix some crashes (#1141)
Browse files Browse the repository at this point in the history
* feat(Core Data): add fetchSlate to Source

* fix(home): simplify relationship chain in RecentSavesView and RecommendationsView to prevent potential SwiftData related crashes

* fix(home): let HomeActions fetch the slate from a Core Data background context instead of SlateDetailView to prevent some potential SwiftData related crashes

* fix(home): update RecommendationsView, retrieve analyticsID directly from the recommendation instead of going back and forth from the related item

* fix(home): update SlateDetailView, simplify relationship chain for domain
  • Loading branch information
Gio2018 authored Jan 30, 2025
1 parent cce9819 commit e378d90
Show file tree
Hide file tree
Showing 7 changed files with 29 additions and 44 deletions.
20 changes: 14 additions & 6 deletions PocketKit/Sources/PocketKit/Home/Models/HomeActions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -102,16 +102,24 @@ extension HomeActions {
}
}

func trackSlateDetailImpression(info: SlateInfo) {
func trackSlateDetailImpression(_ slateID: String) {
Task(priority: .background) {
let source = await Services.shared.source
guard let slate = source.fetchSlate(slateID) else {
return
}
let tracker = await Services.shared.tracker
var sortIndex: Int?
if let index = slate.sortIndex {
sortIndex = Int(truncating: index)
}
tracker.track(
event: Events.ExpandedSlate.slateExpanded(
slateId: info.slateId,
slateRequestId: info.slateRequestId,
slateExperimentId: info.slateExperimentId,
slateIndex: info.slateIndex,
slateLineupId: info.slateLineupId
slateId: slateID,
slateRequestId: slate.requestID,
slateExperimentId: slate.experimentID,
slateIndex: sortIndex ?? 0,
slateLineupId: slate.slateLineup?.remoteID ?? ""
)
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,7 @@ struct SlateDetailView: View {
}
}
.onAppear {
guard let slateInfo = slateInfo(destination) else {
return
}
homeActions.trackSlateDetailImpression(info: slateInfo)
homeActions.trackSlateDetailImpression(destination.slateID)
}
.animation(.smooth, value: cards)
.navigationTitle(destination.slateTitle ?? "")
Expand All @@ -66,7 +63,7 @@ private extension SlateDetailView {
type: .slateDetail,
index: $0.offset,
shareURL: item.shareURL,
domain: item.bestDomain,
domain: item.domain,
timeToRead: item.timeToRead,
isSyndicated: item.isSyndicated,
recommendationID: item.recommendation?.analyticsID,
Expand All @@ -83,23 +80,6 @@ private extension SlateDetailView {
}
}

/// Fetch analytics info for this slate
/// - Parameter destination: slate destination of this slate
/// - Returns: analytics info
func slateInfo(_ destination: SlateDestination) -> SlateInfo? {
guard let slate = fetchSlate(destination.slateID),
let lineup = fetchSlateLineup() else {
return nil
}
return SlateInfo(
slateId: slate.remoteID,
slateRequestId: slate.requestID,
slateExperimentId: slate.experimentID,
slateIndex: Int(slate.sortIndex ?? 0),
slateLineupId: lineup.remoteID
)
}

/// Fetch an `Item` from the underlying `Recommendation`
/// - Parameter recommendationID: `Recommendation` ID
/// - Returns: the item, if it was found
Expand All @@ -112,19 +92,6 @@ private extension SlateDetailView {
return result.first
}

/// Fetch the current slate from SwiftData
/// - Parameter remoteID: the remote id of this slate
/// - Returns: the slate, if it was found
@MainActor
func fetchSlate(_ remoteID: String) -> Slate? {
let predicate = #Predicate<Slate> { $0.remoteID == remoteID }
var fetchDescriptor = FetchDescriptor(predicate: predicate)
fetchDescriptor.fetchLimit = 1

let result = (try? modelContext.fetch(fetchDescriptor)) ?? []
return result.first
}

/// Fettch the current slate lineup
/// - Returns: the slate lineup, if it was found
func fetchSlateLineup() -> SlateLineup? {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ private extension RecentSavesView {
type: .recentSave,
index: $0.offset,
shareURL: item.shareURL,
domain: item.bestDomain,
domain: item.domain,
timeToRead: item.timeToRead,
isSyndicated: item.isSyndicated == true,
recommendationID: item.recommendation?.analyticsID,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -139,10 +139,10 @@ private extension RecommendationsView {
type: .recommendation,
index: Int($0.sortIndex),
shareURL: item.shareURL,
domain: item.bestDomain,
domain: item.domain,
timeToRead: item.timeToRead,
isSyndicated: item.isSyndicated,
recommendationID: item.recommendation?.analyticsID,
recommendationID: $0.analyticsID,
bestTitle: item.bestTitle,
slug: item.collectionSlug,
excerpt: item.excerpt,
Expand Down
4 changes: 4 additions & 0 deletions PocketKit/Sources/Sync/PocketSource.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1036,6 +1036,10 @@ extension PocketSource {
}
return updatedItem
}

public func fetchSlate( _ slateID: String) -> CDSlate? {
try? space.fetchSlate(byRemoteID: slateID)
}
}

// MARK: - Collections
Expand Down
2 changes: 2 additions & 0 deletions PocketKit/Sources/Sync/Source.swift
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,8 @@ public protocol Source {

func fetchSavedItem(_ url: String) -> CDSavedItem?

func fetchSlate( _ slateID: String) -> CDSlate?

/// Get the count of unread saves
/// - Returns: Int of unread saves
func unreadSaves() throws -> Int
Expand Down
4 changes: 4 additions & 0 deletions PocketKit/Tests/PocketKitTests/Support/MockSource.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ import CoreData
import Combine

class MockSource: Source {
func fetchSlate(_ slateID: String) -> Sync.CDSlate? {
nil
}

func makeNotesController() -> NSFetchedResultsController<Sync.CDNote> {
NSFetchedResultsController()
}
Expand Down

0 comments on commit e378d90

Please sign in to comment.