Problem inserting weather metadata into workout.

I have two related functions to retrieve the weather and then insert it into a "Live Workout Builder"...

The first function calls the second to get the weather info after which it inserts the metadata into the builder.

	public func stopGatheringLocationData() {
		
		logger.info("Stopped gathering location data...")
		Task {
			let (humidity, temperature) = await getCurrentWeather()
			if humidity != nil && temperature != nil {
				logger.log("Current humidity \(humidity!), temperature: \(temperature!)")
				let metaData = [HKMetadataKeyWeatherHumidity: humidity!, HKMetadataKeyWeatherTemperature: temperature!]
				logger.log("Current metadata: \(metaData)")
				do {
					try await workoutManager.liveBuilder?.addMetadata(metaData)
					
				} catch {
					logger.error("FAILED to add weather metadata to workout -> \(error.localizedDescription)")
				}
			} else {
				logger.error("FAILED to retrieve weather data...")
			}
		}
		locationManager.stopUpdatingLocation()
	}
	private func getCurrentWeather() async -> (humidity: HKQuantity?, temperature: HKQuantity?) {
		
		// Get the weather for the starting location...
		let tempUnit = HKUnit.degreeFahrenheit()
		let humidityUnit = HKUnit.percent()
		guard let startingLocation else {
			logger.error("No starting location...")
			return (nil, nil)
		}
		do {
			let wx = try await wxService.weather(for: startingLocation)
			let humidity = wx.currentWeather.humidity
			let temp = wx.currentWeather.temperature.converted(to: .fahrenheit)
			let curWx = wx.currentWeather
			let wxAttr = try await wxService.attribution.legalAttributionText
			await MainActor.run {
				currentWx = curWx
				wxServiceAttribution = wxAttr
			}
			return (HKQuantity(unit: humidityUnit, doubleValue: humidity), HKQuantity(unit: tempUnit, doubleValue: temp.value))
			
		} catch {
			
			logger.error("FAILED to retrieve weather data -> \(error.localizedDescription)")
			return (nil, nil)
		}
	}

The two log statements in the first function print out the following information:

[Location/Weather] Current humidity 52 %, temperature: 76.226 degF
[Location/Weather] Current metadata: ["HKWeatherTemperature": 76.226 degF, "HKWeatherHumidity": 52 %]

However, when I look at the workout in the Activity app on my iPhone, the map generated by my app is present but the weather information is missing. The data looks formatted correctly per the limited documentation but for some reason that data is just not showing up.

Any help greatly appreciated...

  • @babt@ibm You may find the solution but I guess you need pass the following keys: HKWeatherHumidity, HKWeatherTemperature. I have tested and it worked as expected.

Add a Comment