Posts

Post not yet marked as solved
3 Replies
1k Views
While testing on device (Apple Watch) attempting to save an HKWorkout into HealthKit I am adding samples of distance samples, calories, heart rates and vo2Max to the workout. Unfortunately unlike this question I am not getting as detailed as a trace back...as far as I can tell it's crashing on adding a sample but I can't tell which sample it is or why?Code:private func addSamples(toWorkout workout: HKWorkout, from startDate: Date, to endDate: Date, handler: @escaping (Bool, Error?) -> Void) { let vo2MaxSample = HKQuantitySample(type: HKQuantityType.quantityType(forIdentifier: HKQuantityTypeIdentifier.vo2Max)!, quantity: vo2MaxQuantity(), start: startDate, end: endDate) var samples = [HKQuantitySample]() for distanceWalkingRunningSample in distanceWalkingRunningSamples { samples.append(distanceWalkingRunningSample) } for energySample in energySamples { samples.append(energySample) } samples.append(vo2MaxSample) samples.append(contentsOf: heartRateValues) // Add samples to workout healthStore.add(samples, to: workout) { (success: Bool, error: Error?) in if error != nil { print("Adding workout subsamples failed with error: \(String(describing: error))") handler(false, error) } if success { print("Success, samples have been added, workout Saved.") //WorkoutStartDate = \(workout.startDate) WorkoutEndDate = \(workout.endDate) handler(true, nil) } else { print("Adding workout subsamples failed no error reported") handler(false, nil) } } }Trace:Exception Type: EXC_CRASH (SIGABRT) Exception Codes: 0x0000000000000000, 0x0000000000000000 Exception Note: EXC_CORPSE_NOTIFY Triggered by Thread: 0 Application Specific Information: abort() called Filtered syslog: None found Last Exception Backtrace: 0 CoreFoundation 0x1bdf75e8 __exceptionPreprocess + 124 1 libobjc.A.dylib 0x1b15717c objc_exception_throw + 33 2 CoreFoundation 0x1bdf752c +[NSException raise:format:] + 103 3 HealthKit 0x273dbdde -[HKObject _validateForCreation] + 111 4 HealthKit 0x273dbc48 +[HKObject _newDataObjectWithMetadata:device:config:] + 219 5 HealthKit 0x273dbb30 +[HKSample _newSampleWithType:startDate:endDate:device:metadata:config:] + 159 6 HealthKit 0x273e9ba8 +[HKWorkout _workoutWithActivityType:startDate:endDate:workoutEvents:duration:totalActiveEnergyBurned:totalBasalEnergyBurned:totalDistance:totalSwimmingStrokeCount:totalFlightsClimbed:goalType:goal:device:metadata:config:] + 431 7 HealthKit 0x274a9342 +[HKWorkout workoutWithActivityType:startDate:endDate:workoutEvents:totalEnergyBurned:totalDistance:device:metadata:] + 109 8 HealthKit 0x274a9160 +[HKWorkout workoutWithActivityType:startDate:endDate:workoutEvents:totalEnergyBurned:totalDistance:metadata:] + 87Thread 0 name: Dispatch queue: com.apple.main-threadThread 0 Crashed:0 libsystem_kernel.dylib 0x1b9e443c __pthread_kill + 81 libsystem_pthread.dylib 0x1baec270 pthread_kill$VARIANT$mp + 3342 libsystem_c.dylib 0x1b96d28e abort + 1063 libc++abi.dylib 0x1b136cfe __cxa_bad_cast + 04 libc++abi.dylib 0x1b136e8a default_unexpected_handler+ 16010 () + 05 libobjc.A.dylib 0x1b1573e0 _objc_terminate+ 29664 () + 1026 libc++abi.dylib 0x1b1493fc std::__terminate(void (*)+ 91132 ()) + 67 libc++abi.dylib 0x1b148ed6 __cxxabiv1::exception_cleanup_func+ 89814 (_Unwind_Reason_Code, _Unwind_Exception*) + 08 libobjc.A.dylib 0x1b157274 _objc_exception_destructor+ 29300 (void*) + 09 CoreFoundation 0x1bdf7530 -[NSException initWithCoder:] + 010 HealthKit 0x273dbde2 -[HKObject _validateForCreation] + 11611 HealthKit 0x273dbc4c +[HKObject _newDataObjectWithMetadata:device:config:] + 22412 HealthKit 0x273dbb34 +[HKSample _newSampleWithType:startDate:endDate:device:metadata:config:] + 16413 HealthKit 0x273e9bac +[HKWorkout _workoutWithActivityType:startDate:endDate:workoutEvents:duration:totalActiveEnergyBurned:totalBasalEnergyBurned:totalDistance:totalSwimmingStrokeCount:totalFlightsClimbed:goalType:goal:device:metadata:config:] + 43614 HealthKit 0x274a9346 +[HKWorkout workoutWithActivityType:startDate:endDate:workoutEvents:totalEnergyBurned:totalDistance:device:metadata:] + 11415 HealthKit 0x274a9164 +[HKWorkout workoutWithActivityType:startDate:endDate:workoutEvents:totalEnergyBurned:totalDistance:metadata:] + 92
Posted Last updated
.
Post not yet marked as solved
0 Replies
680 Views
I've had a long running problem of receiving this error from HealthKit when attempting to "modify" an HKWorkout by (1) copying an existing workout (2) updating the metadata (3) deleting the "old" workout (selectedWorkout in my code below) then (4) saving the new modified workout. 98% of the time this code works flawlessly, however 2% of the time I will get this error and I end up with duplicate workouts. Am I doing anything wrong?As an aside I really wish HealthKit would let us modify data so that this dance wasn't necessary.class func updateMetadataDeleteOldAndSaveNewWorkout(selectedWorkout: HKWorkout, handler: @escaping (Bool,WorkoutManagerError? ) -> Void) { //configure metadata // Create a new workout with the old workout's fields and new edited metadata object let newWorkout = HKWorkout(activityType: selectedWorkout.workoutActivityType, start: selectedWorkout.startDate, end: selectedWorkout.endDate, duration: selectedWorkout.duration, totalEnergyBurned: selectedWorkout.totalEnergyBurned, totalDistance: selectedWorkout.totalDistance, metadata: metadata) // Delete the old workout HealthStoreSingleton.sharedInstance.healthStore.delete(selectedWorkout, withCompletion: { (success, error) in DispatchQueue.main.async { if let unwrappedError = error { handler(false, WorkoutManagerError.deleteError(unwrappedError.localizedDescription)) return } } // When delete was successful save the new workout HealthStoreSingleton.sharedInstance.healthStore.save(newWorkout) { success, error in DispatchQueue.main.async { if let unwrappedError = error { handler(false, WorkoutManagerError.saveError(unwrappedError.localizedDescription)) return } if success { handler(true, nil) return } else { handler(false, WorkoutManagerError.saveError("\(String(describing: error))")) return } } } })
Posted Last updated
.
Post not yet marked as solved
1 Replies
670 Views
I have a weird issue I cannot isolate the cause of. It appears its occuring only in the UK. I'm getting sporadic reports of users who purchase my auto-renewing subscription and there is no expire date on their receipt (I'm assuming, I cannot actually see their receipt). After the purchase is made, I parse the receipt and save the expire date into UserDefaults. Then whenever you open the app I check the expire date against today and if its later, you can access my content.I've created a GIST with my relevant code that parses the receipt and saves the expireDate that is kicked off in my appDelegate's handle purchase:extension AppDelegate: SKPaymentTransactionObserver { func paymentQueue(_ queue: SKPaymentQueue, updatedTransactions transactions: [SKPaymentTransaction]) { for transaction in transactions { switch transaction.transactionState { case .purchasing: handlePurchasingState(for: transaction, in: queue) case .purchased: handlePurchasedState(for: transaction, in: queue) case .restored: handleRestoredState(for: transaction, in: queue) case .failed: handleFailedState(for: transaction, in: queue) case .deferred: handleDeferredState(for: transaction, in: queue) } } } func handlePurchasingState(for transaction: SKPaymentTransaction, in queue: SKPaymentQueue) { print("User is attempting to purchase product id: \(transaction.payment.productIdentifier)") } func handlePurchasedState(for transaction: SKPaymentTransaction, in queue: SKPaymentQueue) { print("User purchased product id: \(transaction.payment.productIdentifier)") SubscriptionService.shared.uploadReceipt { (success, error) in if success { DispatchQueue.main.async { queue.finishTransaction(transaction) NotificationCenter.default.post(name: SubscriptionService.purchaseSuccessfulNotification, object: nil) } } else { if let unwrappedError = error { let errorDict:[String: Error] = [uploadReceiptErrorKey: unwrappedError] NotificationCenter.default.post(name: SubscriptionService.uploadReceiptErrorNotification, object: nil, userInfo: errorDict) } else { NotificationCenter.default.post(name: SubscriptionService.uploadReceiptErrorNotification, object: nil) } } } } func handleRestoredState(for transaction: SKPaymentTransaction, in queue: SKPaymentQueue) { print("Purchase restored for product id: \(transaction.payment.productIdentifier)") SubscriptionService.shared.uploadReceipt { (success, error) in if success { DispatchQueue.main.async { queue.finishTransaction(transaction) NotificationCenter.default.post(name: SubscriptionService.restoreSuccessfulNotification, object: nil) } } else { if let unwrappedError = error { let errorDict:[String: Error] = [uploadReceiptErrorKey: unwrappedError] NotificationCenter.default.post(name: SubscriptionService.uploadReceiptErrorNotification, object: nil, userInfo: errorDict) } else { NotificationCenter.default.post(name: SubscriptionService.uploadReceiptErrorNotification, object: nil) } } } } func handleFailedState(for transaction: SKPaymentTransaction, in queue: SKPaymentQueue) { if let unwrappedError = transaction.error { let errorDict:[String: Error] = [purchaseFailedErrorKey: unwrappedError] NotificationCenter.default.post(name: SubscriptionService.purchaseFailedNotification, object: nil, userInfo: errorDict) } else { NotificationCenter.default.post(name: SubscriptionService.purchaseFailedNotification, object: nil) } queue.finishTransaction(transaction) //The RW tutorial did NOT finish the transaction, but iOS Slack said I should print("Purchase failed for product id: \(transaction.payment.productIdentifier) on \(String(describing: transaction.transactionDate)) and this transaction is currently listed as \(transaction.transactionState) because of this error \(String(describing: transaction.error))") } func handleDeferredState(for transaction: SKPaymentTransaction, in queue: SKPaymentQueue) { NotificationCenter.default.post(name: SubscriptionService.purchaseDeferredNotification, object: nil) print("Purchase deferred for product id: \(transaction.payment.productIdentifier)") } }
Posted Last updated
.
Post not yet marked as solved
3 Replies
4.0k Views
I cannot get an Apple Watch to pair with an iPhone in the simulator even when adding a new device and pairing a watch according to these instructions. I can see from the red disconnected icon at the top of the watch that it's not paired even though Xcode thinks it is? When I tap on the Apple Watch App on the iPhone it says device not paired and to do it in Xcode.Nothing I've tried has been able to fix including a full reinstall of Xcode (however I just trashed the application and not every individual folder). I've also tried deleting derived data as well as everything inside ~/Library/Developer/Xcode/watchOS DeviceSupport and /Library/Developer/CoreSimulator/Profiles/RuntimesI also installed the Xcode 10.3 beta yesterday, but then deleted it. Could this have something to do with it?
Posted Last updated
.
Post not yet marked as solved
0 Replies
386 Views
My understanding is that during a running workout the system will automatically pause a workout when it detects inactivity https://developer.apple.com/documentation/healthkit/hkworkouteventtype/1649810-motionpaused can a third party app disable the workout from being paused so that samples are stil gathered during the period of perceived inactivity?
Posted Last updated
.