惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Microsoft Security Blog
Microsoft Security Blog
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
A
About on SuperTechFans
月光博客
月光博客
Jina AI
Jina AI
F
Fortinet All Blogs
博客园 - 聂微东
The Cloudflare Blog
美团技术团队
B
Blog RSS Feed
N
Netflix TechBlog - Medium
罗磊的独立博客
The GitHub Blog
The GitHub Blog
I
InfoQ
H
Hackread – Cybersecurity News, Data Breaches, AI and More
Apple Machine Learning Research
Apple Machine Learning Research
H
Help Net Security
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
T
The Blog of Author Tim Ferriss
MyScale Blog
MyScale Blog
博客园 - 三生石上(FineUI控件)
宝玉的分享
宝玉的分享
阮一峰的网络日志
阮一峰的网络日志
V
V2EX

胡方运的博客

机票还能这么买!随心飞低价旅行攻略 提前避坑:父母患病和医保问题 我和 Apple 开发的缘分 我的手机流量和宽带套餐性价比方案 iMessage 在国内好用吗? 为什么要写博客 macOS 升级 beta 系统后,如何打开原来版本的 XCode Xcode 安装依赖超时问题解决办法 短信转发-多设备多平台 Follow RSS 阅读 App 记录一次服务器重启应用恢复过程 macOS 重装踩坑记 我的 2021 我的 2020 年终总结 如何给网站添加免费的 https 证书 如何管理多个 git 账号 写给自己的JavaScript系列之一步步写个 JavaScript 解释器 JavaScript 文件的异步加载 async 和 defer 如何修改 git 已提交的用户名和邮箱
macOS App 开发 iCloud 不同步问题解决
感谢您的阅读。 🙏 关于转载请看这里 · 2025-03-12 · via 胡方运的博客

背景

时光鸡 - 纪念日·轮播提醒 我又做了 iOS 的第一个版本,想通过 CloudKit 实现两端的数据同步。但是同样的代码 iOS 正常,数据可以正常同步到 iCloud,macOS 的始终不行。

我在这个问题上调试了很久才解决掉,记录一下解决过程。

现象

  1. 同样的代码 iOS 开发环境和商店版本都正常可以同步 iCloud 数据。
  2. 同样的代码 macOS 版本开发环境可以正常同步数据,但是线上版本不行。

如果你也遇到了 macOS 开发环境 iCloud 能正常同步数据,但是 TestFlight 和线上版本却不行,可以看看我下面的处理过程。

我 iOS 版本数据同步是正常的,就排除了类似配置错误或者这个 CloudKit Database 不可用等可能性。

解决办法

参考这个案例 SwiftUI macOS app not syncing with iCloud

Apple 开发者论坛也有同样的问题备份 SwiftUI macOS应用程序无法与iCloud同步

Well, the answer was simple and yet hard to find. It seems when you convert a Core Data app to work with CloudKit you need to add CloudKit.framework to the Frameworks, Libraries, and Embedded Content section. without it, the app will work when running in debug mode (via Xcode) but once it’s in production, signed and notarized, it won’t. Even if you have the right entitlements, all pointing to production, etc.

翻译一下:

嗯,答案很简单,但很难找到。似乎当您将 Core Data 应用程序转换为与 CloudKit 一起使用时,您需要添加 CloudKit.frameworkFrameworks, Libraries, and Embedded Content 部分。如果没有它,应用程序将在调试模式下运行时(通过 Xcode)工作,但一旦它投入生产、签名和公证,它就不会工作。即使您拥有正确的权利,所有权利都指向生产等。

选择你的 Targets,第一个 General Tab 里面有个 Frameworks, Libraries, and Embedded Content,点击 ➕ 添加 CloudKit.framework,Embed 选择 Do Not Embed

报错的日志

error: CoreData+CloudKit: -[NSCloudKitMirroringDelegate _performSetupRequest:]_block_invoke(1242): <NSCloudKitMirroringDelegate: 0x60000377c000>: Failed to set up CloudKit integration for store: <NSSQLCore: 0x13dc04080> (URL: file:///Users/admin/Library/Containers/xxx)
<CKError 0x60000064adf0: "Service Unavailable" (6/NSCocoaErrorDomain:4099); "Error connecting to CloudKit daemon. This could happen for many reasons, for example a daemon exit, a device reboot, a race with the connection inactivity monitor, invalid entitlements, and more. Check the logs around this time to investigate the cause of this error."; Retry after 5.0 seconds>

error: CoreData+CloudKit: -[NSCloudKitMirroringDelegate recoverFromError:](2312): <NSCloudKitMirroringDelegate: 0x60000377c000> - Attempting recovery from error: <CKError 0x60000064adf0: "Service Unavailable" (6/NSCocoaErrorDomain:4099); "Error connecting to CloudKit daemon. This could happen for many reasons, for example a daemon exit, a device reboot, a race with the connection inactivity monitor, invalid entitlements, and more. Check the logs around this time to investigate the cause of this error."; Retry after 5.0 seconds>

Error retrieving daemon to get network transfer endpoint: Error Domain=NSCocoaErrorDomain Code=4099 "The connection to service named com.apple.cloudd was invalidated: failed at lookup with error 159 - Sandbox restriction." UserInfo={NSDebugDescription=The connection to service named com.apple.cloudd was invalidated: failed at lookup with error 159 - Sandbox restriction.}

Error getting network transfer endpoint: <CKError 0x600000600990: "Service Unavailable" (6/NSCocoaErrorDomain:4099); "Error connecting to CloudKit daemon. This could happen for many reasons, for example a daemon exit, a device reboot, a race with the connection inactivity monitor, invalid entitlements, and more. Check the logs around this time to investigate the cause of this error."; Retry after 5.0 seconds>

error: CoreData+CloudKit: -[NSCloudKitMirroringDelegate resetAfterError:andKeepContainer:](612): <NSCloudKitMirroringDelegate: 0x60000377c000> - resetting internal state after error: <CKError 0x60000064adf0: "Service Unavailable" (6/NSCocoaErrorDomain:4099); "Error connecting to CloudKit daemon. This could happen for many reasons, for example a daemon exit, a device reboot, a race with the connection inactivity monitor, invalid entitlements, and more. Check the logs around this time to investigate the cause of this error."; Retry after 5.0 seconds>

我的报错日志和 Apple 开发者论坛上提到的这个案例完全一致 SwiftData iCloud sync breaks after disabling and re-enabling iCloud

不过他的解决办法不是上面提到的 CloudKit.framework 而是 com.apple.developer.icloud-container-environment 这个属性写的 Development 导致上线后出现了问题,改成 Production 就好了。我感觉这个配置还挺好用的,我就直接固定成 Production 了,这样开发环境也直接连接线上数据库,反正都是连接我个人的 iCloud 不影响其他用户的数据。

该属性的 Apple 文档地址