I made a decision to start out practising how one can talk knowledge between iOS/iPad and WatchOS. It looks like it’s not syncing accurately as a result of once I add or take away an individual, my watch doesn’t replace. I’ve two watches: one related through Bluetooth and the opposite related through mobile or Wi-Fi. After I create a brand new individual on my iOS app, my WatchOS app doesn’t mirror any adjustments. I’ve 4 individuals in my iOS app, however the WatchOS app reveals an empty database. I’ve tried researching in all places on-line and even consulted AI, however I haven’t been in a position to clear up the difficulty. What might I be doing mistaken, or am I lacking one thing?
iOS App – ContentView.swift
struct ContentView: View {
@Setting(.modelContext) non-public var modelContext
// Array to carry the individuals for show
@Question(kind: Individual.title, order: .ahead) non-public var individuals: [Person]
// Add a single random individual
func addRandomPerson() {
let names = ["John", "Alice", "Bob", "Charlie", "David", "Emma", "Frank", "Grace", "Hannah", "Isaac"]
// Choose a random title
let title = names.randomElement()!
// Generate random knowledge for birthday, top, and weight
let randomBirthday = Date(timeIntervalSince1970: TimeInterval.random(in: 315532800..<(315532800 + (10 * 365 * 24 * 60 * 60)))) // Random yr between 1980 and 1990
let randomHeight = Double.random(in: 5.5...6.5) // Random top between 5.5 and 6.5 toes
let randomWeight = Int.random(in: 170...210) // Random weight between 170 and 210 lbs
// Create the brand new individual
let individual = Individual(title: title,
birthday: randomBirthday,
top: randomHeight,
weight: randomWeight)
// Insert the individual into the mannequin context
modelContext.insert(individual)
}
// Delete an individual from the record
func deletePerson(at offsets: IndexSet) {
for index in offsets {
let individual = individuals[index]
modelContext.delete(individual)
}
}
var physique: some View {
VStack {
Button("Add Random Individual") {
addRandomPerson() // Add a single random individual
}
.padding()
Checklist {
ForEach(individuals) { individual in
VStack(alignment: .main) {
Textual content(individual.title)
.font(.headline)
Textual content("Birthday: (individual.birthday.formatted(.dateTime.month().day().yr()))")
.font(.subheadline)
Textual content("Peak: (individual.top, specifier: "%.1f")")
Textual content("Weight: (individual.weight)")
}
.onTapGesture {
// Deal with edit performance if wanted
}
}
.onDelete(carry out: deletePerson)
}
}
.padding()
}
}
iOS App – SwiftData.swift
@Mannequin
class Individual {
var title: String
var birthday: Date
var top: Double
var weight: Int
init(title: String, birthday: Date, top: Double, weight: Int) {
self.title = title
self.birthday = birthday
self.top = top
self.weight = weight
}
}
iOS App – Startup App
@predominant
struct Watch_ConnectionApp: App {
var physique: some Scene {
WindowGroup {
ContentView()
}
.modelContainer(for: Individual.self)
}
}
Watch App – ContentView.swift
struct PersonListView: View {
@Question(kind: Individual.title, order: .ahead) non-public var individuals: [Person]
public init() { }
var physique: some View {
Checklist(individuals) { individual in
VStack(alignment: .main) {
Textual content(individual.title)
.font(.headline)
Textual content("Birthday: (individual.birthday.formatted(.dateTime.month().day().yr()))")
.font(.subheadline)
Textual content("Peak: (individual.top, specifier: "%.1f")")
Textual content("Weight: (individual.weight)")
}
}
.navigationTitle("Folks")
}
}
Watch App – Startup App
@Mannequin
class Individual {
var title: String
var birthday: Date
var top: Double
var weight: Int
init(title: String, birthday: Date, top: Double, weight: Int) {
self.title = title
self.birthday = birthday
self.top = top
self.weight = weight
}
}
@predominant
struct Watch_Connection_App_Watch_AppApp: App {
var physique: some Scene {
WindowGroup {
NavigationView {
PersonListView()
}
}
.modelContainer(for: Individual.self) // Add SwiftData stack
}
}