Syncing Issue Between iOS and WatchOS: SwiftData Not Updating on Watch

I decided to start practicing how to communicate data between iOS/iPad and WatchOS. It seems like it’s not syncing correctly because when I add or remove a person, my watch doesn’t update. I have two watches: one connected via Bluetooth and the other connected via cellular or Wi-Fi. When I create a new person on my iOS app, my WatchOS app doesn’t reflect any changes. I have four people in my iOS app, but the WatchOS app shows an empty database. I’ve tried researching everywhere online and even consulted AI, but I haven’t been able to solve the issue. What could I be doing wrong, or am I missing something?

iOS App – ContentView.swift

struct ContentView: View {
    @Environment(.modelContext) private var modelContext
    
    // Array to hold the people for display
    @Query(sort: Person.name, order: .forward) private var people: [Person]
    
    // Add a single random person
    func addRandomPerson() {
        let names = ["John", "Alice", "Bob", "Charlie", "David", "Emma", "Frank", "Grace", "Hannah", "Isaac"]
        
        // Select a random name
        let name = names.randomElement()!
        
        // Generate random data for birthday, height, and weight
        let randomBirthday = Date(timeIntervalSince1970: TimeInterval.random(in: 315532800..<(315532800 + (10 * 365 * 24 * 60 * 60)))) // Random year between 1980 and 1990
        let randomHeight = Double.random(in: 5.5...6.5) // Random height between 5.5 and 6.5 feet
        let randomWeight = Int.random(in: 170...210) // Random weight between 170 and 210 lbs
        
        // Create the new person
        let person = Person(name: name,
                            birthday: randomBirthday,
                            height: randomHeight,
                            weight: randomWeight)
        
        // Insert the person into the model context
        modelContext.insert(person)
    }
    
    // Delete a person from the list
    func deletePerson(at offsets: IndexSet) {
        for index in offsets {
            let person = people[index]
            modelContext.delete(person)
        }
    }
    
    var body: some View {
        VStack {
            Button("Add Random Person") {
                addRandomPerson() // Add a single random person
            }
            .padding()

            List {
                ForEach(people) { person in
                    VStack(alignment: .leading) {
                        Text(person.name)
                            .font(.headline)
                        Text("Birthday: (person.birthday.formatted(.dateTime.month().day().year()))")
                            .font(.subheadline)
                        Text("Height: (person.height, specifier: "%.1f")")
                        Text("Weight: (person.weight)")
                    }
                    .onTapGesture {
                        // Handle edit functionality if needed
                    }
                }
                .onDelete(perform: deletePerson)
            }
        }
        .padding()
    }
}

iOS App – SwiftData.swift

@Model
class Person {
    var name: String
    var birthday: Date
    var height: Double
    var weight: Int

    init(name: String, birthday: Date, height: Double, weight: Int) {
        self.name = name
        self.birthday = birthday
        self.height = height
        self.weight = weight
    }
}

iOS App – Startup App

@main
struct Watch_ConnectionApp: App {
    var body: some Scene {
        WindowGroup {
            ContentView()
        }
        .modelContainer(for: Person.self)
    }
}

Watch App – ContentView.swift

struct PersonListView: View {
    @Query(sort: Person.name, order: .forward) private var people: [Person]
    
    public init() { }

    var body: some View {
        List(people) { person in
            VStack(alignment: .leading) {
                Text(person.name)
                    .font(.headline)
                Text("Birthday: (person.birthday.formatted(.dateTime.month().day().year()))")
                    .font(.subheadline)
                Text("Height: (person.height, specifier: "%.1f")")
                Text("Weight: (person.weight)")
            }
        }
        .navigationTitle("People")
    }
}

Watch App – Startup App

@Model
class Person {
    var name: String
    var birthday: Date
    var height: Double
    var weight: Int

    init(name: String, birthday: Date, height: Double, weight: Int) {
        self.name = name
        self.birthday = birthday
        self.height = height
        self.weight = weight
    }
}

@main
struct Watch_Connection_App_Watch_AppApp: App {
    var body: some Scene {
        WindowGroup {
            NavigationView {
                PersonListView()
            }
        }
        .modelContainer(for: Person.self) // Add SwiftData stack
    }
}

4

iCloud sync is not automatically enabled for SwiftData – You need to set it up.

Apple provides documentation, but in summary you need to:

  • Add the iCloud capability to your app in Xcode
  • Add the remote notification capability to your app in Xcode
  • Add some code to create a CloudKit schema
Recognized by Mobile Development Collective

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật