我的苹果手表上的第一个复杂功能是空白的。

huangapple go评论77阅读模式
英文:

First add of my complication on apple watch is blank

问题

以下是您的翻译内容,已去除代码部分:

I am developing an app with apple watch complications. When I first add one of the complication to the watch face it stays blank. When I tap on the complication the app opens and the complication on the watch face is displayed after closing the app again. Same behavior when I add a second complication on the same watch face or even another. Both the new and the old one is showing up.
Is there a problem with my timeline provider or are the API calls taking too long?

提前感谢。

Edit (18.02.2023): Provider with Result

import WidgetKit
import CoreLocation

struct Provider: TimelineProvider {
    let locationManager = LocationManager()
    
    func placeholder(in context: Context) -> SimpleEntry {
        SimpleEntry(date: .now, station: .success(Station.placeholder), recentMeasurements: .success([]))
    }
    
    func getSnapshot(in context: Context, completion: @escaping (SimpleEntry) -> ()) {
        if context.isPreview {
            let entry = SimpleEntry(date: .now, station: .success(Station.placeholder), recentMeasurements: .success(Station.measurmentsPlaceholder))
            completion(entry)
        } else {
            Task {
                guard let stations = try? await APIService.getStations(),
                      let station = stations.first
                else {
                    completion(SimpleEntry(date: .now, station: .failure(ProviderError.stationFetch), recentMeasurements: .failure(ProviderError.stationFetch)))
                    return
                }
                guard let recentMeasurements = try? await APIService.getStationMeasurements(station: station, days: 7)
                else {
                    completion(SimpleEntry(date: .now, station: .failure(ProviderError.measurementsFetch), recentMeasurements: .failure(ProviderError.measurementsFetch)))
                    return
                }
                let entry = SimpleEntry(date: .now, station: .success(station), recentMeasurements: .success(recentMeasurements))
                completion(entry)
            }
        }
    }

    func getTimeline(in context: Context, completion: @escaping (Timeline<SimpleEntry>) -> ()) {
        Task {
            let stationResult: Result<Station, Error>
            let station: Station?
            if let stations = try? await APIService.getStations() {
                station = locationManager.getNearestStation(in: stations) ?? stations.first!
                stationResult = .success(station!)
            }
            else {
                station = nil
                stationResult = .failure(ProviderError.stationFetch)
            }
            let recentMeasurements: Result<[Measurement], Error>
            if let result = try? await APIService.getStationMeasurements(station: station!, days: 7) {
                recentMeasurements = .success(result)
            }
            else {
                recentMeasurements = .failure(ProviderError.measurementsFetch)
            }
            let entry = SimpleEntry(date: .now, station: stationResult, recentMeasurements: recentMeasurements)
            let secondEntry = SimpleEntry(date: .now.advanced(by: 60 * 30), station: stationResult, recentMeasurements: recentMeasurements)

            let timeline = Timeline(entries: [entry, secondEntry], policy: .atEnd)
            completion(timeline)
        }
    }
}

enum ProviderError: Error {
    case stationFetch
    case measurementsFetch
}

Data fetching function:

static func getJSON<T: Decodable>(from path: String) async throws -> [T] {
    guard let url = URL(string: path) else { return [] }
        
    let (data, _) = try await URLSession.shared.data(from: url)
    let collection = try JSONDecoder().decode([T].self, from: data)
    return collection
}
英文:

I am developing an app with apple watch complications. When I first add one of the complication to the watch face it stays blank. When I tap on the complication the app opens and the complication on the watch face is displayed after closing the app again. Same behavior when I add a second complication on the same watch face or even another. Both the new and the old one is showing up.
Is there a problem with my timeline provider or are the API calls taking too long?

import WidgetKit
import CoreLocation

struct Provider: TimelineProvider {
    let locationManager = LocationManager()
    
    func placeholder(in context: Context) -&gt; SimpleEntry {
        SimpleEntry(date: .now, station: .placeholder, recentMeasurements: [])
    }
    
    func getSnapshot(in context: Context, completion: @escaping (SimpleEntry) -&gt; ()) {
		if context.isPreview {
			let station = Station.placeholder
			let recentMeasurements = Station.measurmentsPlaceholder
			let entry = SimpleEntry(date: .now, station: station, recentMeasurements: recentMeasurements)
			completion(entry)
		} else {
			Task {
				let station = await APIService.getStations().first!
				let recentMeasurements = await APIService.getStationMeasurements(station: station, days: 7)
				let entry = SimpleEntry(date: .now, station: station, recentMeasurements: recentMeasurements)
				completion(entry)
			}
		}
    }

    func getTimeline(in context: Context, completion: @escaping (Timeline&lt;SimpleEntry&gt;) -&gt; ()) {
        Task {
            let stations = await APIService.getStations()
            let station = locationManager.getNearestStation(in: stations) ?? stations.first!
            let recentMeasurements = await APIService.getStationMeasurements(station: station, days: 7)
            let entry = SimpleEntry(date: .now, station: station, recentMeasurements: recentMeasurements)
			let secondEntry = SimpleEntry(date: .now.advanced(by: 60 * 30), station: station, recentMeasurements: recentMeasurements)

			let timeline = Timeline(entries: [entry, secondEntry], policy: .atEnd)
            completion(timeline)
        }
    }
}

Thanks in advance.

Edit (18.02.2023): Provider with Result

import WidgetKit
import CoreLocation

struct Provider: TimelineProvider {
    let locationManager = LocationManager()
    
    func placeholder(in context: Context) -&gt; SimpleEntry {
		SimpleEntry(date: .now, station: .success(Station.placeholder), recentMeasurements: .success([]))
    }
    
    func getSnapshot(in context: Context, completion: @escaping (SimpleEntry) -&gt; ()) {
		if context.isPreview {
			let entry = SimpleEntry(date: .now, station: .success(Station.placeholder), recentMeasurements: .success(Station.measurmentsPlaceholder))
			completion(entry)
		} else {
			Task {
				guard let stations = try? await APIService.getStations(),
					  let station = stations.first
				else {
					completion(SimpleEntry(date: .now, station: .failure(ProviderError.stationFetch), recentMeasurements: .failure(ProviderError.stationFetch)))
					return
				}
				guard let recentMeasurements = try? await APIService.getStationMeasurements(station: station, days: 7)
				else {
					completion(SimpleEntry(date: .now, station: .failure(ProviderError.measurementsFetch), recentMeasurements: .failure(ProviderError.measurementsFetch)))
					return
				}
				let entry = SimpleEntry(date: .now, station: .success(station), recentMeasurements: .success(recentMeasurements))
				completion(entry)
			}
		}
    }

    func getTimeline(in context: Context, completion: @escaping (Timeline&lt;SimpleEntry&gt;) -&gt; ()) {
        Task {
			let stationResult: Result&lt;Station, Error&gt;
			let station: Station?
			if let stations = try? await APIService.getStations() {
				station = locationManager.getNearestStation(in: stations) ?? stations.first!
				stationResult = .success(station!)
			}
			else {
				station = nil
				stationResult = .failure(ProviderError.stationFetch)
			}
			let recentMeasurements: Result&lt;[Measurement], Error&gt;
			if let result = try? await APIService.getStationMeasurements(station: station!, days: 7) {
				recentMeasurements = .success(result)
			}
			else {
				recentMeasurements = .failure(ProviderError.measurementsFetch)
			}
            let entry = SimpleEntry(date: .now, station: stationResult, recentMeasurements: recentMeasurements)
			let secondEntry = SimpleEntry(date: .now.advanced(by: 60 * 30), station: stationResult, recentMeasurements: recentMeasurements)

			let timeline = Timeline(entries: [entry, secondEntry], policy: .atEnd)
            completion(timeline)
        }
    }
}

enum ProviderError: Error {
	case stationFetch
	case measurementsFetch
}

Data fetching function:

static func getJSON&lt;T: Decodable&gt;(from path: String) async throws -&gt; [T] {
    guard let url = URL(string: path) else { return [] }
		
	let (data, _) = try await URLSession.shared.data(from: url)
	let collection = try JSONDecoder().decode([T].self, from: data)
	return collection
}

答案1

得分: 0

数据视图中出现了数据筛选的问题。感谢帮助。

英文:

There was an issue with filtering the data in the view.
Thanks for the help.

huangapple
  • 本文由 发表于 2023年2月14日 02:26:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/75439868.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定