Posts

Post not yet marked as solved
0 Replies
203 Views
Hello! Today I worked for the first time with Apple Health Kit and successfully saved a workout in Health with the basic informations (activityType, start and end). My app is in basic function an interval timer where you can create your own workout. Now in the next step I want to add the calories burned in the workout. This information is stored in the attribute 'totalEnergyBurned'. Do I need to calculate this value myself or can I query this value directly if the user is wearing an Apple Watch? Or maybe the value is even automatically added to the workout if there is the corresponding record? (So far I have only tested the app in the simulator, which is why I can't answer this possibility). My current code: func saveToHealthKit(entryID: String){             if HKHealthStore.isHealthDataAvailable() {                 let healthStore = HKHealthStore()                 if(healthStore.authorizationStatus(for: HKObjectType.workoutType()) == .sharingAuthorized && healthStore.authorizationStatus(for: HKObjectType.quantityType(forIdentifier: HKQuantityTypeIdentifier.activeEnergyBurned)!) == .sharingAuthorized){                     let newWorkout = HKWorkout(activityType: HKWorkoutActivityType.highIntensityIntervalTraining, start: entry.date!, end: Date())                     healthStore.save(newWorkout) { success, error in                         guard success else {                             // Perform proper error handling here.                             return                         }                                                  // Add detail samples here.                     }                 }             }         }     } Thanks :)
Posted
by KLukas.
Last updated
.
Post not yet marked as solved
1 Replies
177 Views
I'm new to SwiftUI and I have run into an issue I can't explain. (I know its a lot of text but it would be great if you can help me) About the project: For the problem three views are important. In View A you can set some values and when you press a button you go to the View C where an object is created with the passed values form View A. In this example every things work, the values are presented in a Text View. There is also the option to save the values you set in View A and save them to use them later. These objects are saved and when you tap on them in View 0 (View is a view where every saved object is presented)you get to ViewB. There you can go to View C like in View A. Here the values should also be displayed but don't. Only when I press a button in View C do the values appear. Code: View A: NavigationLink(destination: TrainingView(trainingsListVM: übungListVM.transformToTrainingsListVM(daten: daten))){ 										Text("startString") 												.styleButton() 								} //goes to View C an passes the necessary values View B: NavigationLink(destination: TrainingView(trainingsListVM: workout.transformToTrainingsListViewModel())){ 								Text("startString") 										.styleButton() 						} View C: struct ViewC: View{ 		var trainingsListVM: TrainingsListViewModel 		@ObservedObject var settings = SettingsModel() 		//intervallTimer is the important Object 		@ObservedObject var intervallTimer = IntervallTimer(trainingsListVM: TrainingsListViewModel(daten: Daten()), settings: SettingsModel()) 	 		var momentaneÜbung: String{ //one of the values I want to display 				if !noExercise{ 						return intervallTimer.momentaneÜbung 						 				}else{ 						return NSLocalizedString("\(intervallTimer.inTraining ? "trainingString" : "pauseString")", tableName: "Localizable", comment: "") 				} 		} 		 		init(trainingsListVM: TrainingsListViewModel) { 				self.trainingsListVM = trainingsListVM 				intervallTimer = IntervallTimer(trainingsListVM: trainingsListVM, settings: settings) 		 //have to set intervalltimer here again because I need the passes object trainingsListVM which I get from View A or View B 		} 		var body: some View{ 				 Text(momentaneÜbung) //when I come from View A momentaneÜbung has a value; from View B it is empty on the screen 				 .onApperar{ 						 intervallTimer.setStart()//set the right values 						 print(momentaneÜbung) //although it is not shown on the screen, the correct value is sprinted here (when I come from View B) 					} 		} } IntervallTimer Object: class IntervalTimer: ObervableObject{ 		 var trainingsListVM: TrainingsListViewModel var settings: SettingsModel @Published var trainingsState: [TrainingsState] = [] @Published var übungenListe: Übungen init(trainingsListVM: TrainingsListViewModel, settings: SettingsModel) { 		self.settings = settings 		übungenListe = Übungen(trainingsListVM: trainingsListVM) 		 		self.trainingsListVM = trainingsListVM 		 		if settings.randomExercise == true{ 				self.übungenListe.übungen.shuffle() 		//this init is called when I go to View B but I use it in View C. Shouldn't it only be created when I go to View C 		} 		 } var momentaneÜbung = "" ...some other variables func setStart(){ 		trainingsState = [] 		trainingsState = setTrainingsState(übungenListe: übungenListe) 		 		momentaneÜbung = übungenListe.übungen[0].name //String array with all exercises 		 		trainingsState.removeFirst() } } Things I tested:When I call onAppear() in View C an print the value it is printed correctly. When I set a breakpoint in the init() of the object I want to create in View C it gets already called when I go to View B When I set a breakpoint in the init of intervallTimer and go to View C (coming from View B) this init is called 7 times. In every iteration I print momentaneÜbung. In the first five iterations momentaneÜbung is empty, in the sixth I get the right value but in the last one momentaneÜbung is empty again. Question: What is the reason for the different behaviour depending on which view I come from? I hope I have mentioned everything important. I am very happy to receive an answer. Thank you!
Posted
by KLukas.
Last updated
.
Post marked as solved
1 Replies
579 Views
Hi, in my current project I want to save a custom data type in core data but I don't know to do it. The Core Data Entity: extension WorkoutBuy { @nonobjc public class func fetchRequest() -> NSFetchRequest<WorkoutBuy> { &#9;&#9;return NSFetchRequest<WorkoutBuy>(entityName: "WorkoutBuy") } @NSManaged public var beschreibung: String? @NSManaged public var dauerInterval: Int64 @NSManaged public var dauerPause: Int64 @NSManaged public var dauerPauseDurchgang: Int64 @NSManaged public var durchgaenge: Int64 @NSManaged public var id: UUID? @NSManaged public var intensitaet: String? @NSManaged public var name: String? @NSManaged public var price: Double @NSManaged public var catagory: Catagory? !!!//here is my own data type !!! } extension WorkoutBuy : Identifiable { } My own data type: struct Catagory: Codable, Identifiable{ &#9; let id: Int &#9; let localizedString: String &#9; let engishName: String? } When I do it this way I get this error: Property cannot be declared public because its type uses an internal type And: Property cannot be marked @NSManaged because its type cannot be represented in Objective-C One thing I tried: When Catagory is a own core data entity the app is starting but when I add an object I got the following error like 10 times in the console: 2020-12-04 21:58:31.586858+0100 IntervallTraining[4869:1923447] [error] error: SQLCore dispatchRequest: exception handling request: <NSSQLSaveChangesRequestContext: 0x2812c9380> , threw while encoding a value. with userInfo of (null) CoreData: error: SQLCore dispatchRequest: exception handling request: <NSSQLSaveChangesRequestContext: 0x2812c9380> , threw while encoding a value. with userInfo of (null) Ein Core Data-Fehler ist aufgetreten. Does anyone have a solution? Thank you in advance. PS: I use the AppDelegate to create the persistentContainer
Posted
by KLukas.
Last updated
.