Clear View Data When Tab is Changed SwiftUI - swift

I am showing daily step information in one of the tabs. Unfortunately, when I select the steps tab again it adds one more of the same data below the previous one.
I have tried to solve it via toggle a boolean. But it did not help either.
import SwiftUI
import HealthKit
struct StepView: View {
private var healthStore: HealthStore?
#State private var presentClipboardView = true
#State private var steps: [Step] = [Step]()
init() {
healthStore = HealthStore()
}
private func updateUIFromStatistics(_ statisticsCollection: HKStatisticsCollection) {
let now = Date()
let startOfDay = Calendar.current.startOfDay(for: now)
statisticsCollection.enumerateStatistics(from: startOfDay, to: now) { (statistics, stop) in
let count = statistics.sumQuantity()?.doubleValue(for: .count())
let step = Step(count: Int(count ?? 0), date: statistics.startDate, wc: Double(count ?? 0 / 1000 ))
steps.append(step)
}
}
var body: some View {
VStack {
ForEach(steps, id: \.id) { step in
VStack {
HStack{
Text("WC")
Text("\(step.wc)")
}
HStack {
Text("\(step.count)")
Text("Total Steps")
}
Text(step.date, style: .date)
.opacity(0.5)
Spacer()
}
}
.navigationBarBackButtonHidden(true)
}
.onAppear() {
if let healthStore = healthStore {
healthStore.requestAuthorization { (success) in
if success {
healthStore.calculateSteps { (statisticsCollection) in
if let statisticsCollection = statisticsCollection {
updateUIFromStatistics(statisticsCollection)
}
}
}
}
}
}
.onDisappear() {
self.presentClipboardView.toggle()
}
}
}
Step Model
struct Step: Identifiable {
let id = UUID()
let count: Int?
let date: Date
let wc: Double
}
HealthStore file
class HealthStore {
var healthStore: HKHealthStore?
var query: HKStatisticsCollectionQuery?
init() {
if HKHealthStore.isHealthDataAvailable() {
healthStore = HKHealthStore()
}
}
func calculateSteps(completion: #escaping (HKStatisticsCollection?) -> Void ) {
let stepType = HKQuantityType.quantityType(forIdentifier: HKQuantityTypeIdentifier.stepCount)!
let now = Date()
let startOfDay = Calendar.current.startOfDay(for: now)
let daily = DateComponents(day:1)
let predicate = HKQuery.predicateForSamples(withStart: startOfDay, end: Date(), options: .strictStartDate)
query = HKStatisticsCollectionQuery(quantityType: stepType, quantitySamplePredicate: predicate, options: .cumulativeSum, anchorDate: startOfDay, intervalComponents: daily)
query!.initialResultsHandler = { query, statisticCollection, error in
completion(statisticCollection)
}
if let healthStore = healthStore, let query = self.query {
healthStore.execute(query)
}
}
func requestAuthorization(completion: #escaping (Bool) -> Void) {
let stepType = HKQuantityType.quantityType(forIdentifier: HKQuantityTypeIdentifier.stepCount)!
guard let healthStore = self.healthStore else { return completion (false) }
healthStore.requestAuthorization(toShare: [], read: [stepType]) { (success, error) in
completion(success)
}
}
}
Those are all related files according to my problem.
Thanks in advance.

The steps is annotated with #State which means it will be persisted even when the view is redrawn.
And you never reset it. You only append new steps. Try clearing steps in updateUIFromStatistics:
private func updateUIFromStatistics(_ statisticsCollection: HKStatisticsCollection) {
steps = [] // remove previous values
let now = Date()
let startOfDay = Calendar.current.startOfDay(for: now)
statisticsCollection.enumerateStatistics(from: startOfDay, to: now) { (statistics, stop) in
let count = statistics.sumQuantity()?.doubleValue(for: .count())
let step = Step(count: Int(count ?? 0), date: statistics.startDate, wc: Double(count ?? 0 / 1000 ))
steps.append(step)
}
}

Related

SwiftUI, Core Data, and PhotoKit: Views not updating when state changes (state management hell)

I'm learning Swift/SwiftUI by building a photo organizer app. It displays a user's photo library in a grid like the built-in photos app, and there's a detail view where you can do things like favorite a photo or add it to the trash.
My app loads all the data and displays it fine, but the UI doesn't update when things change. I've debugged enough to confirm that my edits are applied to the underlying PHAssets and Core Data assets. It feels like the problem is that my views aren't re-rendering.
I used Dave DeLong's approach to create an abstraction layer that separates Core Data from SwiftUI. I have a singleton environment object called DataStore that handles all interaction with Core Data and the PHPhotoLibrary. When the app runs, the DataStore is created. It makes an AssetFetcher that grabs all assets from the photo library (and implements PHPhotoLibraryChangeObserver). DataStore iterates over the assets to create an index in Core Data. My views' viewmodels query core data for the index items and display them using the #Query property wrapper from Dave's post.
App.swift
#main
struct LbPhotos2App: App {
var body: some Scene {
WindowGroup {
ContentView()
.environment(\.dataStore, DataStore.shared)
}
}
}
PhotoGridView.swift (this is what contentview presents)
struct PhotoGridView: View {
#Environment(\.dataStore) private var dataStore : DataStore
#Query(.all) var indexAssets: QueryResults<IndexAsset>
#StateObject var vm = PhotoGridViewModel()
func updateVm() {
vm.createIndex(indexAssets)
}
var body: some View {
GeometryReader { geo in
VStack {
HStack {
Text("\(indexAssets.count) assets")
Spacer()
TrashView()
}.padding(EdgeInsets(top: 4, leading: 16, bottom: 4, trailing: 16))
ScrollView {
ForEach(vm.sortedKeys, id: \.self) { key in
let indexAssets = vm.index[key]
let date = indexAssets?.first?.creationDate
GridSectionView(titleDate:date, indexAssets:indexAssets!, geoSize: geo.size)
}
}.onTapGesture {
updateVm()
}
}.onAppear {
updateVm()
}
.navigationDestination(for: IndexAsset.self) { indexAsset in
AssetDetailView(indexAsset: indexAsset)
}
}
}
}
PhotoGridViewModel.swift
class PhotoGridViewModel: ObservableObject {
#Published var index: [String:[IndexAsset]] = [:]
var indexAssets: QueryResults<IndexAsset>?
func createIndex() {
guard let assets = self.indexAssets else {return}
self.createIndex(assets)
}
func createIndex(_ queryResults: QueryResults<IndexAsset>) {
indexAssets = queryResults
if queryResults.count > 0 {
var lastDate = Date.distantFuture
for i in 0..<queryResults.count {
let item = queryResults[i]
let isSameDay = isSameDay(firstDate: lastDate, secondDate: item.creationDate!)
if isSameDay {
self.index[item.creationDateKey!]?.append(item)
} else {
self.index[item.creationDateKey!] = [item]
}
lastDate = item.creationDate!
}
}
self.objectWillChange.send()
}
var sortedKeys: [String] {
return index.keys.sorted().reversed()
}
private func isSameDay(firstDate:Date, secondDate:Date) -> Bool {
return Calendar.current.isDate(
firstDate,
equalTo: secondDate,
toGranularity: .day
)
}
}
Here's where I actually display the asset in GridSectionView.swift
LazyVGrid(columns: gridLayout, spacing: 2) {
let size = geoSize.width/4
ForEach(indexAssets, id:\.self) { indexAsset in
NavigationLink(
value: indexAsset,
label: {
AssetCellView(indexAsset: indexAsset, geoSize:geoSize)
}
).frame(width: size, height: size)
.buttonStyle(.borderless)
}
}
AssetCellView.swift
struct AssetCellView: View {
#StateObject var vm : AssetCellViewModel
var indexAsset : IndexAsset
var geoSize : CGSize
init(indexAsset: IndexAsset, geoSize: CGSize) {
self.indexAsset = indexAsset
self.geoSize = geoSize
_vm = StateObject(wrappedValue: AssetCellViewModel(indexAsset: indexAsset, geoSize: geoSize))
}
var body: some View {
ZStack(alignment: .bottomTrailing) {
if (vm.indexAsset != nil && vm.image != nil) {
vm.image?
.resizable()
.aspectRatio(contentMode: .fit)
.border(.blue, width: vm.indexAsset!.isSelected ? 4 : 0)
}
if (vm.indexAsset != nil && vm.indexAsset!.isFavorite) {
Image(systemName:"heart.fill")
.resizable()
.frame(width: 20, height: 20)
.foregroundStyle(.ultraThickMaterial)
.shadow(color: .black, radius: 12)
.offset(x:-8, y:-8)
}
}
}
}
AssetCellViewModel.swift
class AssetCellViewModel: ObservableObject{
#Environment(\.dataStore) private var dataStore
#Published var image : Image?
var indexAsset : IndexAsset?
var geoSize : CGSize
init(indexAsset: IndexAsset? = nil, geoSize:CGSize) {
self.indexAsset = indexAsset
self.geoSize = geoSize
self.requestImage(targetSize: CGSize(width: geoSize.width/4, height: geoSize.width/4))
}
func setIndexAsset(_ indexAsset:IndexAsset, targetSize: CGSize) {
self.indexAsset = indexAsset
self.requestImage(targetSize: targetSize)
}
func requestImage(targetSize: CGSize? = nil) {
if (self.indexAsset != nil) {
dataStore.fetchImageForLocalIdentifier(
id: indexAsset!.localIdentifier!,
targetSize: targetSize,
completionHandler: { image in
withAnimation(Animation.easeInOut (duration:0.15)) {
self.image = image
}
}
)
}
}
}
some of DataStore.swift
public class DataStore : ObservableObject {
static let shared = DataStore()
let persistenceController = PersistenceController.shared
#ObservedObject var assetFetcher = AssetFetcher()
let dateFormatter = DateFormatter()
var imageManager = PHCachingImageManager()
let id = UUID().uuidString
init() {
print("🔶 init dataStore: \(self.id)")
dateFormatter.dateFormat = "yyyy-MM-dd"
assetFetcher.iterateResults{ asset in
do {
try self.registerAsset(
localIdentifier: asset.localIdentifier,
creationDate: asset.creationDate!,
isFavorite: asset.isFavorite
)
} catch {
print("Error registering asset \(asset)")
}
}
}
func registerAsset(localIdentifier:String, creationDate:Date, isFavorite:Bool) throws {
let alreadyExists = indexAssetEntityWithLocalIdentifier(localIdentifier)
if alreadyExists != nil {
// print("🔶 Asset already registered: \(localIdentifier)")
// print(alreadyExists![0])
return
}
let iae = IndexAssetEntity(context: self.viewContext)
iae.localIdentifier = localIdentifier
iae.creationDate = creationDate
iae.creationDateKey = dateFormatter.string(from: creationDate)
iae.isFavorite = isFavorite
iae.isSelected = false
iae.isTrashed = false
self.viewContext.insert(iae)
try self.viewContext.save()
print("🔶 Registered asset: \(localIdentifier)")
}
And AssetFetcher.swift
class AssetFetcher:NSObject, PHPhotoLibraryChangeObserver, ObservableObject {
#Published var fetchResults : PHFetchResult<PHAsset>? = nil
let id = UUID().uuidString
override init() {
super.init()
print("🔶 init assetfetcher: \(id)")
self.startFetchingAllPhotos()
}
deinit {
PHPhotoLibrary.shared().unregisterChangeObserver(self)
}
func startFetchingAllPhotos() {
getPermissionIfNecessary(completionHandler: {result in
print(result)
})
let fetchOptions = PHFetchOptions()
var datecomponents = DateComponents()
datecomponents.month = -3
//TODO: request assets dynamically
let threeMonthsAgo = Calendar.current.date(byAdding: datecomponents, to:Date())
fetchOptions.predicate = NSPredicate(format: "creationDate > %# AND creationDate < %#", threeMonthsAgo! as NSDate, Date() as NSDate)
fetchOptions.sortDescriptors = [NSSortDescriptor(key: "creationDate", ascending: false)]
fetchOptions.wantsIncrementalChangeDetails = true
// fetchOptions.fetchLimit = 1000
let results = PHAsset.fetchAssets(with: .image, options: fetchOptions)
PHPhotoLibrary.shared().register(self)
print("🔶 \(PHPhotoLibrary.shared())")
self.fetchResults = results
}
func iterateResults(_ callback:(_ asset: PHAsset) -> Void) {
print("iterateResults")
guard let unwrapped = self.fetchResults else {
return
}
for i in 0..<unwrapped.count {
callback(unwrapped.object(at: i))
}
}
func photoLibraryDidChange(_ changeInstance: PHChange) {
print("🔶 photoLibraryDidChange")
DispatchQueue.main.async {
if let changeResults = changeInstance.changeDetails(for: self.fetchResults!) {
self.fetchResults = changeResults.fetchResultAfterChanges
// self.dataStore.photoLibraryDidChange(changeInstance)
// self.updateImages()
self.objectWillChange.send()
}
}
}
}

Check Same Dates Inside ForEach Swift

How can I check if item 1 date is equal to item 2 date inside the foreach. Can I check the same dates inside the predicate like below without using 2 foreach loops?
struct TimedView: View {
static let taskDateFormat: DateFormatter = {
let formatter = DateFormatter()
formatter.dateStyle = .long
return formatter
}()
#State var releaseDate = Date()
#Environment(\.managedObjectContext) private var viewContext
#FetchRequest var reminder: FetchedResults<CDReminder>
init(){
var calendar = Calendar.current
calendar.timeZone = NSTimeZone.local
let dateFrom = calendar.startOfDay(for: Date()) // eg. 2016-10-10 00:00:00
let dateTo = calendar.date(byAdding: .day, value: 1, to: dateFrom)
let predicate = NSPredicate(format : "date >= %# AND date == %#", dateFrom as CVarArg)
self._reminder = FetchRequest(
entity: CDReminder.entity(),
sortDescriptors: [NSSortDescriptor(keyPath: \CDReminder.date, ascending: false)],
predicate: predicate)
}
var body: some View {
NavigationView {
VStack{
HStack{
Text("Zamanlanmis")
.font(.system(size: 40, weight: .bold, design: .rounded))
.foregroundColor(.red)
Spacer()
}
.padding(.leading)
List{
ForEach(reminder, id: \.self) { item in
DatedReminderCell(reminder: item, isSelected: false, onComplete: {})
.foregroundColor(.black)
}
}
}
}
}
Now my output is coming like this
Date objects are only equal if they represent the exact same instant, down to the fraction of a second.
If you want to see if a date falls on a give month/day/year, you will need to use a Calendar object to generate beginning_of_day and end_of_day dates and see if each date fall between them.
This thread includes several examples of that. Most of the code is in Objective-C but it should be pretty easy to translate the approach to Swift, if not the exact code.
To solve this complex problem I created a helper file called ScheduledViewHelper. Inside this helper, I have created a new array that has two elements(reminder and date). Inside this helper first I fetched all the reminders from core data then I compared all their date data and when I find a new date I append the date element inside the array. Finally, I sorted those dates and return [ReminderList] to be able to get this array from my ScheduledView.
My ScheduledViewHelper is
public struct ReminderList: Hashable {
let date: String
let reminder: [CDReminder]
}
open class ScheduledViewHelper: NSObject {
public static let shared = ScheduledViewHelper()
private let viewContext = PersistenceController.shared.container.viewContext
private override init() {
super.init()
}
private func getReminders() -> [CDReminder] {
let request: NSFetchRequest<CDReminder> = CDReminder.fetchRequest()
do {
let items = try viewContext.fetch(request) as [CDReminder]
return items
} catch {
print("Error occured: ", error)
}
return []
}
public func getScheduledData() -> [ReminderList] {
let calendar = Calendar.current
var list: [ReminderList] = []
var dateList: [String] = []
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "dd/MM/yyyy"
let reminders = getReminders()
reminders.forEach { (reminder) in
let date = dateFormatter.string(from: reminder.date!)
if !dateList.contains(date) && date >= dateFormatter.string(from: Date()) {
dateList.append(date)
}
}
dateList.forEach { (date) in
let dateFrom = calendar.startOfDay(for: dateFormatter.date(from: date)!) // eg. 2016-10-10 00:00:00
let dateTo = calendar.date(byAdding: .day, value: 1, to: dateFrom)
let predicate = NSPredicate(format: "date <= %# AND date >= %#", dateTo! as NSDate, dateFrom as NSDate)
let request: NSFetchRequest<CDReminder> = CDReminder.fetchRequest()
request.predicate = predicate
do {
let items = try viewContext.fetch(request) as [CDReminder]
let newItem = ReminderList(date: date, reminder: items)
list.append(newItem)
} catch {
print("Error occured: ", error)
}
}
return list.sorted {
$0.date < $1.date
}
}
private func saveContext() {
do {
try viewContext.save()
} catch {
print("Error occured: ", error)
}
}
}
I added to my ScheduledView those lines:
created a new var before init() statement
#State var remindersList : [ReminderList] = []
just under NavigationView I initialized remindersList with onAppear
.onAppear {
remindersList = ScheduledViewHelper.shared.getScheduledData()
}
Reorganized my List like this
List{
ForEach(remindersList, id: .self) { item in
VStack(alignment: .leading) {
Text(item.date)
ForEach(item.reminder, id: .self) { reminder in
DatedReminderCell(reminder: reminder, isSelected: false, onComplete: {})
}
}
.foregroundColor(.black)
}
}

SwiftUI publishing API data every 60 seconds with a Timer

I’ve been chasing my own tail for days.. Maybe the architecture is all wrong. I just can't get it all to work at the same time. Any help would be greatly appreciated.
I have a LoginView which takes an email and password, and validates to the server.
import SwiftUI
struct LoginView: View {
#EnvironmentObject var userAuth: UserAuth
#State private var email: String = ""
#State private var password: String = ""
var body: some View {
TextField("Email Address", text: $email)
SecureField("Password", text: $password)
Button(action: {
guard let url = URL(string: "https://www.SomeLoginApi.com/login") else { return }
let body: [String: String] = ["emailAddress": email, "password": password]
let finalBody = try! JSONSerialization.data(withJSONObject: body)
var request = URLRequest(url: url)
request.httpMethod = "POST"
request.httpBody = finalBody
request.setValue("application/json", forHTTPHeaderField: "Content-Type")
URLSession.shared.dataTask(with: request) { (data, _, _) in
guard let data = data else { return }
let loginResponse = try! JSONDecoder().decode(ServerResponse.self, from: data)
if loginResponse.message == "authorized" {
DispatchQueue.main.async {
self.userAuth.isLoggedIn = true
self.userAuth.userId = loginResponse.userId
AppData().getData(userId: userAuth.userId)
}
} else {
var isLoggedin = false
}
}
.resume()
}) {
Text("LOGIN")
}
.disabled(email.isEmpty || password.isEmpty)
}
If validated, the above code does the following:
Sets isLoggedIn to true which changes the view to MainView via the following StartingView:
import SwiftUI
struct StartingView: View {
#EnvironmentObject var userAuth: UserAuth
var body: some View {
if !userAuth.isLoggedIn {
LoginView()
} else {
MainView()
}
}
}
Sends a 2nd API call AppData().getData(userId: userAuth.userId) to the server for data.
Here is the AppData class that the above API is pointing to.
import Foundation
import SwiftUI
import Combine
import CoreImage
import CoreImage.CIFilterBuiltins
class AppData : ObservableObject {
#Published var userData: AppDataModel = AppDataModel(data1: "", data2: "", data3: "", data4: "", data5: "", data6: "", data7: "", bool1: false, data8: "", data9: "", bool2: true, bool3: true, bool4: true, bool5: true, bool6: true, data10: "", data11: "", data12: "", data13: "", array1:[], array2: [], array3: [], array4: [], array5: [], array6: [])
#Published var time = ""
#Published var greet = ""
#Published var bgImage = Image.init("")
init() {
var greetingTimer: Timer?
greetingTimer = Timer.scheduledTimer(timeInterval: 60.0, target: self, selector: #selector(getData), userInfo: nil, repeats: true)
}
#objc func getData(userId: String) {
let bgImgArr = ["appBackAnimals1", "appBackAnimals2", "appBackAnimals3", "appBackAnimals4", "appBackAnimals5", "appBackAnimals6", "appBackAnimals7", "appBackAnimals8", "appBackAnimals9", "appBackAnimals10", "appBackAnimals11", "appBackAnimals12", "appBackAnimals13"]
let bgImg = bgImgArr.randomElement()!
guard let inputImage = UIImage(named: bgImg) else { return }
let beginImage = CIImage(image: inputImage)
let context = CIContext()
let currentFilter = CIFilter.vignette()
currentFilter.inputImage = beginImage
currentFilter.intensity = 6
// get a CIImage from our filter or exit if that fails
guard let outputImage = currentFilter.outputImage else { return }
// attempt to get a CGImage from our CIImage
if let cgimg = context.createCGImage(outputImage, from: outputImage.extent) {
// convert that to a UIImage
let uiImage = UIImage(cgImage: cgimg)
// and convert that to a SwiftUI image
self.bgImage = Image(uiImage: uiImage)
}
let today = Date()
let formatter = DateFormatter()
formatter.dateFormat = "EEEE h:mma"
formatter.amSymbol = "am"
formatter.pmSymbol = "pm"
let calendar = Calendar.current
let hour = calendar.component(.hour, from: today)
time = formatter.string(from: today)
if hour >= 5 && hour <= 11 {
greet = "morning"
} else if hour >= 12 && hour <= 17 {
greet = "afternoon"
} else if hour >= 18 && hour <= 20 {
greet = "evening"
} else if hour >= 21 && hour <= 24 {
greet = "night"
} else if hour >= 0 && hour <= 4 {
greet = "night"
}
guard let url = URL(string: "https://www.SomeDataApi.com/data") else { return }
let body: [String: String] = ["userId": userId]
let finalBody = try! JSONSerialization.data(withJSONObject: body)
var request = URLRequest(url: url)
request.httpMethod = "POST"
request.httpBody = finalBody
request.setValue("application/json", forHTTPHeaderField: "Content-Type")
URLSession.shared.dataTask(with: request) { (data, _, _) in
guard let data = data else { return }
let apiData = try! JSONDecoder().decode(AppDataModel.self, from: data)
if apiData.message == "data" {
DispatchQueue.main.async {
self.userData = apiData
}
}
}
.resume()
if userData.appTopLine == "" {
userData.appTopLine = "Good " + greet + " " + userData.appName
} else {
userData.appTopLine = "not working"
}
if userData.appBottomLine == "" {
userData.appBottomLine = "It's " + time
}
}
}
And here is MainView where I want to display the data
import SwiftUI
struct MainView: View {
#ObservedObject var profileData = AppData()
#EnvironmentObject var userAuth: UserAuth
var body: some View {
ZStack {
profileData.bgImage
HStack {
VStack(alignment: .leading) {
Text(profileData.userData.appTopLine)
Text(profileData.userData.appBottomLine)
}
}
}
}
}
Issues that I am experiencing:
I am able to print(apiData) and see the data, however #Published var userData and self.userData = apiData are not making the data available on MainView via #ObservedObject var profileData = AppData()
getData() is not getting triggered every 60 seconds with the Timer because I am not able to figure out how to pass the (userId: userAuth.userId) parameter in there.
I appreciate any direction at all. If this is not set-up in an ideal way, please tell me, I want to do this correctly.
Thank you!
The instance of AppData().getData(userId: userAuth.userId) in LoginView is not the same as #ObservedObject var profileData = AppData().
The ObservedObject never sees what the LoginView one is doing.
You have to share the instance by either using the SwiftUI wrappers like you have with UserAuth or a singleton (less recommended).
class Singleton {
static let sharedInstance = Singleton()
}
Also, what is AppDataModel? is it an ObservableObject? You can't chain them.
If so, these changes are userData.appTopLine = "not working" are not being observed. You won't see them.
#Published var timeElapsed = false
func delayText() {
// Delay of 7.5 seconds
DispatchQueue.main.asyncAfter(deadline: .now() + 7.5) {
self.timeElapsed = true
}
}
That is a timer, hope this can help. This works for me, hope it can for you.

Swift HealthKit HKStatisticsCollectionQuery statisticsUpdateHandler not always called

I have a test project where I get the total number of falls for a user for each day over the course of the week. The initialResultsHandler works perfectly every time, however the statisticsUpdateHandler doesn't always fire off. If you start the app, then go to the health app and insert falls manually, switch back to the test app you should see the total for today update. In reality this works for about the first 3-6 times. After that the statisticsUpdateHandler doesn't get called anymore.
What's also odd is that if you delete data and then go back to the test app, or add data from a time earlier than now, the statisticsUpdateHandler gets called. This leads me to think that it has something to do with the statisticsUpdateHandler end date.
Apples documentation is pretty clear however I’m afraid they might be leaving something out.
If this property is set to nil, the statistics collection query will automatically stop as soon as it has finished calculating the initial results. If this property is not nil, the query behaves similarly to the observer query. It continues to run, monitoring the HealthKit store. If any new, matching samples are saved to the store—or if any of the existing matching samples are deleted from the store—the query executes the update handler on a background queue.
Is there any reason that statisticsUpdateHandler might not be called? I have included a test project below.
struct Falls: Identifiable{
let id = UUID()
let date: Date
let value: Int
var formattedDate: String{
let formatter = DateFormatter()
formatter.setLocalizedDateFormatFromTemplate("MM/dd/yyyy")
return formatter.string(from: date)
}
}
struct ContentView: View {
#StateObject var manager = HealthKitManager()
var body: some View {
NavigationView{
List{
Text("Updates: \(manager.updates)")
ForEach(manager.falls){ falls in
HStack{
Text(falls.value.description)
Text(falls.formattedDate)
}
}
}
.overlay(
ProgressView()
.scaleEffect(1.5)
.opacity(manager.isLoading ? 1 : 0)
)
.navigationTitle("Falls")
}
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
class HealthKitManager: ObservableObject{
let healthStore = HKHealthStore()
let fallType = HKQuantityType.quantityType(forIdentifier: .numberOfTimesFallen)!
#Published var isLoading = false
#Published var falls = [Falls]()
#Published var updates = 0
init() {
let healthKitTypesToRead: Set<HKSampleType> = [fallType]
healthStore.requestAuthorization(toShare: nil, read: healthKitTypesToRead) { (success, error) in
if let error = error{
print("Error: \(error)")
} else if success{
self.startQuery()
}
}
}
func startQuery(){
let now = Date()
let cal = Calendar.current
let sevenDaysAgo = cal.date(byAdding: .day, value: -7, to: now)!
let startDate = cal.startOfDay(for: sevenDaysAgo)
let predicate = HKQuery.predicateForSamples(withStart: startDate, end: now, options: [.strictStartDate, .strictEndDate])
var interval = DateComponents()
interval.day = 1
// start from midnight
let anchorDate = cal.startOfDay(for: now)
let query = HKStatisticsCollectionQuery(
quantityType: fallType,
quantitySamplePredicate: predicate,
options: .cumulativeSum,
anchorDate: anchorDate,
intervalComponents: interval
)
query.initialResultsHandler = { query, collection, error in
guard let collection = collection else {
print("No collection")
DispatchQueue.main.async{
self.isLoading = false
}
return
}
collection.enumerateStatistics(from: startDate, to: Date()){ (result, stop) in
guard let sumQuantity = result.sumQuantity() else {
return
}
let totalFallsForADay = Int(sumQuantity.doubleValue(for: .count()))
let falls = Falls(date: result.startDate, value: totalFallsForADay)
print(falls.value, falls.formattedDate)
DispatchQueue.main.async{
self.falls.insert(falls, at: 0)
}
}
print("initialResultsHandler done")
DispatchQueue.main.async{
self.isLoading = false
}
}
query.statisticsUpdateHandler = { query, statistics, collection, error in
print("In statisticsUpdateHandler...")
guard let collection = collection else {
print("No collection")
DispatchQueue.main.async{
self.isLoading = false
}
return
}
DispatchQueue.main.async{
self.isLoading = true
self.updates += 1
self.falls.removeAll(keepingCapacity: true)
}
collection.enumerateStatistics(from: startDate, to: Date()){ (result, stop) in
guard let sumQuantity = result.sumQuantity() else {
return
}
let totalFallsForADay = Int(sumQuantity.doubleValue(for: .count()))
let falls = Falls(date: result.startDate, value: totalFallsForADay)
print(falls.value, falls.formattedDate)
print("\n\n")
DispatchQueue.main.async{
self.falls.insert(falls, at: 0)
}
}
print("statisticsUpdateHandler done")
DispatchQueue.main.async{
self.isLoading = false
}
}
isLoading = true
healthStore.execute(query)
}
}
I was so focused on the statisticsUpdateHandler and the start and end time that I didn't pay attention to the query itself. It turns out that the predicate was the issue. By giving it an end date, it was never looking for samples outside the the initial predicate end date.
Changing the predicate to this solved the issue:
let predicate = HKQuery.predicateForSamples(withStart: startDate, end: nil, options: [.strictStartDate])

Thread 1: Fatal error: Can't remove last element from an empty collection SwiftUI

That is the link of the project https://github.com/m3rtkoksal/WalkerCoin
I am trying to reach all the historical step counts but when I change startDate value from -7 to something bigger than -7 I am getting Fatal error: Can't remove first element from an empty collection: file Swift/RangeReplaceableCollection.swift, line 624
I allowed all the necessary permissions from info.plist and added Healthkit from signing&capabilities.
If I try only 7 days back it works but when I increase that value it crashes.
import SwiftUI
import HealthKit
struct StepView: View {
private var healthStore: HealthStore?
#State private var selectedDay = Step(count: 0, date: Date())
#State private var steps: [Step] = [Step]()
init() {
healthStore = HealthStore()
}
private func updateUIFromStatistics(_ statisticsCollection: HKStatisticsCollection) {
steps = []
let now = Date()
let offset = -7
let startDate = Calendar.current.date(byAdding: .day, value: offset, to: Date())!
statisticsCollection.enumerateStatistics(from: startDate, to: now) { (statistics, stop) in
let count = statistics.sumQuantity()?.doubleValue(for: .count())
let step = Step(count: Int(count ?? 0), date: statistics.startDate)
steps.append(step)
}
}
var body: some View {
ZStack(alignment: .leading) {
Image("stepsTabBG")
.resizable()
.ignoresSafeArea(.all)
VStack {
HStack {
ScrollView(.horizontal) {
HStack(spacing: 30) {
ForEach(steps, id: \.id) { day in
Text("\(Calendar.current.dateComponents([.day], from: day.date).day ?? 0 )")
.foregroundColor(self.selectedDay.date == day.date ? Color.red : Color.black)
.onTapGesture {
selectedDay = day
}
}
}
}
.frame(width: UIScreen.main.bounds.width / 2)
.padding(10)
Spacer()
}
CircularProgress(steps: selectedDay.count)
and this is my HealthStore
import HealthKit
extension Date {
static func mondayAt12AM() -> Date {
return Calendar(identifier: .iso8601).date(from: Calendar(identifier: .iso8601).dateComponents([.yearForWeekOfYear, .weekOfYear], from: Date()))!
}
}
class HealthStore {
var healthStore: HKHealthStore?
var query: HKStatisticsCollectionQuery?
init() {
if HKHealthStore.isHealthDataAvailable() {
healthStore = HKHealthStore()
}
}
func calculateSteps(completion: #escaping (HKStatisticsCollection?) -> Void) {
let stepType = HKQuantityType.quantityType(forIdentifier: HKQuantityTypeIdentifier.stepCount)!
let offset = -7
let startDate = Calendar.current.date(byAdding: .day, value: offset, to: Date())!
let anchorDate = Date.mondayAt12AM()
let daily = DateComponents(day: 1)
let predicate = HKQuery.predicateForSamples(withStart: startDate, end: Date(), options: .strictStartDate)
query = HKStatisticsCollectionQuery(quantityType: stepType, quantitySamplePredicate: predicate, options: .cumulativeSum, anchorDate: anchorDate, intervalComponents: daily)
query!.initialResultsHandler = { query, statisticsCollection, error in
completion(statisticsCollection)
}
if let healthStore = healthStore, let query = self.query {
healthStore.execute(query)
}
}
func requestAuthorization(completion: #escaping (Bool) -> Void) {
let stepType = HKQuantityType.quantityType(forIdentifier: HKQuantityTypeIdentifier.stepCount)!
guard let healthStore = self.healthStore else { return completion (false) }
healthStore.requestAuthorization(toShare: [], read: [stepType]) { (success, error) in
completion(success)
}
}
}
And that is the error description after the crash.
statisticsCollection.enumerateStatistics(from: startDate, to: now) { (statistics, stop) in
let count = statistics.sumQuantity()?.doubleValue(for: .count())
let step = Step(count: Int(count ?? 0), date: statistics.startDate)
DispatchQueue.main.async {
steps.append(step)
}
}
Try adding your append statement inside DispatchQueue.main.async. It worked for me. Hope it works for you as well.