Skip to content

Commit

Permalink
watchOS 10: fix crash (#2573)
Browse files Browse the repository at this point in the history
  • Loading branch information
leandroalonso authored Dec 19, 2024
2 parents 2f2e2d7 + 75b76ec commit 4744f9d
Showing 1 changed file with 38 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ import Foundation
import PocketCastsDataModel
import PocketCastsUtils
import UIKit
#if os(watchOS)
import WatchKit
#endif

public class RefreshManager {
public static let shared = RefreshManager()
Expand Down Expand Up @@ -65,6 +68,7 @@ public class RefreshManager {
refresh(podcasts: DataManager.sharedManager.allPodcasts(includeUnsubscribed: false))
}

#if !os(watchOS)
private func refresh(podcasts: [Podcast], completion: (() -> Void)? = nil) {
UserDefaults.standard.set(Date(), forKey: ServerConstants.UserDefaults.lastRefreshStartTime)

Expand All @@ -78,6 +82,40 @@ public class RefreshManager {
}
}
}
#endif

#if os(watchOS)
// Watch OS 10 has a very very very weird issue that happened after using Xcode 16:
// if the refresh call uses a [weak self] the system kills the app.
// I'm not entirely sure why that happens but removing the weak part fixes the issue.
// We can safely remove this method once we remove support to watchOS 10.
private func refresh(podcasts: [Podcast], completion: (() -> Void)? = nil) {
UserDefaults.standard.set(Date(), forKey: ServerConstants.UserDefaults.lastRefreshStartTime)

DispatchQueue.global().async {
let watchOsMajorVersion = WKInterfaceDevice.current().systemVersion.split(separator: ".")[safe: 0]

if watchOsMajorVersion == "10" {
// Let's not use [weak self] so the app is not killed
MainServerHandler.shared.refresh(podcasts: podcasts) { refreshResponse in
self.processPodcastRefreshResponse(refreshResponse) { _ in
completion?()
}
}

return
}

MainServerHandler.shared.refresh(podcasts: podcasts) { [weak self] refreshResponse in
guard let self = self else { return }

self.processPodcastRefreshResponse(refreshResponse) { _ in
completion?()
}
}
}
}
#endif

public func refreshPodcasts(completion: @escaping (RefreshFetchResult) -> Void) {
DispatchQueue.global().async {
Expand Down

0 comments on commit 4744f9d

Please sign in to comment.