Post

Replies

Boosts

Views

Activity

Comment on help me! (data sync between watch and phone)
Your data are more complex than I gathered from your original post, i.e. title:number. You're now describing a struct with various properties, which include an array of counts. If this is correct, then there is a way of sending that via transferUserInfo but UserDefaults would not be an appropriate way of storing the data on the watch. Is only one 'title', 'count1', 'count2', 'creation date' struct sent at a time, or an array of them? Is there a variable number of counts per title on a given creation date? The answers to these questions determine how best to store data on the watch (if really needed) and how to transmit to the phone. I can't do much more to help until tomorrow afternoon Eastern Australia time.
Topic: App & System Services SubTopic: General Tags:
Sep ’21
Comment on Date in Swift printed is wrong
No, comparison of Date types is done on the UTC date-time. If the date-time of your user's timezone is important in decision making, e.g. how many times did the user do X before 10am each day, then you'd need to use the Calendar functions https://developer.apple.com/documentation/foundation/calendar
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Sep ’21
Comment on Date in Swift printed is wrong
let currentHour = Calendar.current.component(.hour, from: Date()) if currentHour < 12 {print("now is morning")} This gets the hour, as an Integer, from the current local time, i.e. the Calendar function converts the UTC time to the user's current timezone and summertime (or not) settings. I suggest that you experiment with Date formatting and with Calendar in a Playground.
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Sep ’21
Comment on help me! (data sync between watch and phone)
My understanding is that you need to store Goal, with its title, count1, count2 and creation time, and that, as the app gets used, more and more Goals are stored. If you only ever store ONE goal on the watch, before it is sent to the phone, then you might be able to use UserDefaults to reload title, count1, count2 and creation time each time the watch app relaunches - before data are sent to the phone. However, "The defaults system allows an app to customize its behavior to match a user’s preferences. For example, you can allow users to specify their preferred units of measurement or media playback speed." - Apple Documentation. What you're suggesting are not preferences, but cumulative data. I wouldn't use UserDefaults this way. When I have a small amount of data that only gets added to by appending, I store it as a text file with comma separated values. For anything else I use SQLite and/or CloudKit. You will be using CoreData on the phone, so (as I've stated) you can use the same code on the watch to store your data there. CoreData, SQLite and CloudKit are all complex (to begin with) but data persistence is an essential skill that must be learned. :). Regards, Michaela
Topic: App & System Services SubTopic: General Tags:
Sep ’21
Comment on Date in Swift printed is wrong
Sorry for the delay: there seems to have been a problem with the forum software and I was getting an error when attempting to respond. You only format the dates at the point of displaying them to the user: for all other purposes, you must use the original dates for comparisons and computations - except where you want specific information about the local date-time e.g. whether it's morning or not, in which case you use the Calendar functions with the date (not the formatted string).
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Sep ’21
Comment on Use CoreDate with swift
PS I meant to get rid of the cost property in BucketItem, because I essentially replaced it with a computed value totalCost. In this simple app, the cost of a buckelist item is the sum of the associated cost items, therefore the original cost property on BucketItem is redundant - and potentially a consistency problem.
Sep ’21
Comment on help me! (data sync between watch and phone)
Are you using SwiftUI or UIKit? If SwiftUI, the sample code I provided earlier works: make sure you follow what I said for the phone app. The key aspects are: 1) the Phone Data Model must be an ObservableObject, 2) you must use @Published on the variable (count) that you want synced in your view, 3) you must update the @Published count in the data model from incoming watch data by using DispatchQueue.main.async 4) the SwiftUI view must declare the data as @ObservedObject var ........ Oh, and I'm a lady - so just call me Michaela - not Uncle.
Topic: App & System Services SubTopic: General Tags:
Oct ’21
Comment on Help with first watch app
Mmm, I haven't had that message before, but I'm aware that it's been a problem for others from time to time. I suggest checking if your session (instance of WCSesssion) returns .ispaired == true on the phone immediately after your session gets activated. If it returns false, then see if the iPhone and Watch really are paired. It might also be an idea to check all other aspects of the paired device information as per the documentation https://developer.apple.com/documentation/watchconnectivity/wcsession. What versions of Xcode, iOS and watchOS are you using? If needs be, I can try to replicate the problem here. Regards, Michaela
Topic: App & System Services SubTopic: General Tags:
Mar ’22