iOS 和 WatchOS 之间的同步问题:SwiftData 未在 Watch 上更新

问题描述 投票:0回答:1

我决定开始练习如何在 iOS/iPad 和 WatchOS 之间通信数据。它似乎没有正确同步,因为当我添加或删除一个人时,我的手表不会更新。我有两只手表:一只通过蓝牙连接,另一只通过蜂窝网络或 Wi-Fi 连接。当我在 iOS 应用程序上创建新用户时,我的 WatchOS 应用程序不会反映任何更改。我的 iOS 应用程序中有四个人,但 WatchOS 应用程序显示一个空数据库。我尝试过在网上到处研究,甚至咨询过人工智能,但我一直没能解决这个问题。我可能做错了什么,或者我错过了什么?

iOS 应用程序 - 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 应用程序 - 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 应用程序 - 启动应用程序

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

观看应用程序 - 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")
    }
}

手表应用程序 - 启动应用程序

@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
    }
}
ios swift swiftui watchos swiftdata
1个回答
0
投票

iCloud 同步不会自动为 SwiftData 启用 - 您需要进行设置。

Apple 提供了文档,但总的来说,您需要:

  • 在 Xcode 中将 iCloud 功能添加到您的应用程序
  • 在 Xcode 中为您的应用程序添加远程通知功能
  • 添加一些代码来创建 CloudKit 架构
© www.soinside.com 2019 - 2024. All rights reserved.