fatbobman / blogComments

1 stars 0 forks source link

Core Data with CloudKit(四)—— 调试、测试、迁移及其他 | 肘子的Swift记事本 #55

Open fatbobman opened 3 years ago

fatbobman commented 3 years ago

https://www.fatbobman.com/posts/coreDataWithCloudKit-4/

本文聊一下在开发Core Data with CloudKit项目中常见的一些问题,让大家少走弯路、避免踩坑。

XiaoXiaoZhiWei commented 2 years ago

用户iCloud存储空间不足导致数据丢失怎么办?有木有检测iCloud空间不足的方法?

fatbobman commented 2 years ago

目前好像没没有关于icloud容量不总的获取方式。 或许可以尝试使用纯Cloudkit存储的方式测试一下是否可以存储成功?例如创建一个单独的Record Type,app定期保存一个类似token,然后获取看看是否保存成功? 总之,系统提供的状态查询机制中没有提供这种功能。

2022年1月13日 下午4:10,小小志伟 @.***> 写道:

用户iCloud存储空间不足导致数据丢失怎么办?有木有检测iCloud空间不足的方法?

— Reply to this email directly, view it on GitHub https://github.com/fatbobman/blogComments/issues/55#issuecomment-1011895036, or unsubscribe https://github.com/notifications/unsubscribe-auth/ANIYIGIP2NYRMXR7QDPEHB3UV2CHNANCNFSM5B5CEFKQ. You are receiving this because you authored the thread.

XiaoXiaoZhiWei commented 2 years ago

谢谢 找了半天没看到类似的方法 

发自我的iPhone

------------------ 原始邮件 ------------------ 发件人: 东坡肘子 @.> 发送时间: 2022年1月13日 16:22 收件人: fatbobman/blogComments @.> 抄送: 小小志伟 @.>, Comment @.> 主题: Re: [fatbobman/blogComments] Core Data with CloudKit(四)—— 调试、测试、迁移及其他 | 肘子的Swift记事本 (#55)

目前好像没没有关于icloud容量不总的获取方式。 或许可以尝试使用纯Cloudkit存储的方式测试一下是否可以存储成功?例如创建一个单独的Record Type,app定期保存一个类似token,然后获取看看是否保存成功? 总之,系统提供的状态查询机制中没有提供这种功能。

> 2022年1月13日 下午4:10,小小志伟 @.***> 写道: >
>
> 用户iCloud存储空间不足导致数据丢失怎么办?有木有检测iCloud空间不足的方法? >
> — > Reply to this email directly, view it on GitHub <https://github.com/fatbobman/blogComments/issues/55#issuecomment-1011895036&gt;, or unsubscribe <https://github.com/notifications/unsubscribe-auth/ANIYIGIP2NYRMXR7QDPEHB3UV2CHNANCNFSM5B5CEFKQ&gt;. > You are receiving this because you authored the thread. >

— Reply to this email directly, view it on GitHub, or unsubscribe. You are receiving this because you commented.Message ID: @.***>

daxuan90 commented 2 years ago

你好,请问文章中的参数配置面板在 Xcode 哪里打开呢?

fatbobman commented 2 years ago

https://icloud.developer.apple.com https://icloud.developer.apple.com/ 你需要有 apple developer 账户。

2022年2月17日 09:05,Xuan @.***> 写道:

你好,请问文章中的参数配置面板在 Xcode 哪里打开呢?

— Reply to this email directly, view it on GitHub https://github.com/fatbobman/blogComments/issues/55#issuecomment-1042465520, or unsubscribe https://github.com/notifications/unsubscribe-auth/ANIYIGOM5QI7LDPDDPQR57LU3RCVPANCNFSM5B5CEFKQ. You are receiving this because you authored the thread.

daxuan90 commented 2 years ago

https://icloud.developer.apple.com https://icloud.developer.apple.com/ 你需要有 apple developer 账户。

谢谢解答,但我想说的是 「Run -> Arguments Passed On Launch」 这个 Xcode 配置面板的入口,不过我现在已经找到了。

image
davidleee commented 2 years ago

你好,请问在应用里实现一个开关控制网络同步是不是直接用不同的 cloudKitContainerOptions 重建整个 NSPersistentContainer 就可以了?有什么需要需要注意的地方吗?

fatbobman commented 2 years ago

你好,请问在应用里实现一个开关控制网络同步是不是直接用不同的 cloudKitContainerOptions 重建整个 NSPersistentContainer 就可以了?有什么需要需要注意的地方吗?

你好。 是的,使用 cloudKitContainerOptions 为 nil 的配置,即可不启用同步。 通过开关更改设置后,如果是在app下次冷启动时生效,没有什么需要特别调整的地方。如果想支持热切换则需要注意如下问题:

例如:

粘贴的图形-1
davidleee commented 2 years ago

@fatbobman

你好,请问在应用里实现一个开关控制网络同步是不是直接用不同的 cloudKitContainerOptions 重建整个 NSPersistentContainer 就可以了?有什么需要需要注意的地方吗?

你好。 是的,使用 cloudKitContainerOptions 为 nil 的配置,即可不启用同步。 通过开关更改设置后,如果是在app下次冷启动时生效,没有什么需要特别调整的地方。如果想支持热切换则需要注意如下问题:

  • 切换时,UI 或 代码中的其他部分不应正在使用数据
  • 如果使用 SwiftUI ,且使用了 上下文环境值的话,应先创建新的container,然后用新的上下文替换原有的,最后再将旧的container删除。
  • 如果想两个 container 共用一个 model 的话,需要使用如下的方法,使用container另外一个构造方法

例如:

粘贴的图形-1

感谢答疑!

Hank-Zhong commented 2 years ago

你好!在开发阶段不管是真机还是模拟器,都需要切换至后台再切换回来才能触发NSFetchedResultsController相关方法。请问下这是正常的吗?

fatbobman commented 2 years ago

你好!在开发阶段不管是真机还是模拟器,都需要切换至后台再切换回来才能触发NSFetchedResultsController相关方法。请问下这是正常的吗?

在真机上不应该发生这种情况。 现在的状况是,只有在从后台切换回前台后,才会开始导入数据(从网络同步过来)然后合并到视图上下文,还是切换后,才会将已经导入的数据(切换前就已经从网络上同步到本地)合并到视图上下文。你必须首先确定是这里的哪种情况才能进一步排查问题

Hank-Zhong commented 2 years ago

果然

如果还是找不到原因的话,请泡壶茶、听听歌、看看远方,过一会可能就好了。

昨天一整天不管怎么试都没用,今天打开发现没问题了... 感谢回复~!

fatbobman commented 1 year ago

你好。 是的,使用 cloudKitContainerOptions 为 nil 的配置,即可不启用同步。 通过开关更改设置后,如果是在app下次冷启动时生效,没有什么需要特别调整的地方。如果想支持热切换则需要注意如下问题:

例如:

2022年4月13日 10:26,David Lee @.***> 写道:

你好,请问在应用里实现一个开关控制网络同步是不是直接用不同的 cloudKitContainerOptions 重建整个 NSPersistentContainer 就可以了?有什么需要需要注意的地方吗?

— Reply to this email directly, view it on GitHub https://github.com/fatbobman/blogComments/issues/55#issuecomment-1097486149, or unsubscribe https://github.com/notifications/unsubscribe-auth/ANIYIGMSMF2HAEBW7KNV5KDVEYWGZANCNFSM5B5CEFKQ. You are receiving this because you authored the thread.

KittenYang commented 1 year ago

iPhone真机和模拟器可以同步,但是iPhone 和 Mac 客户端不能同步,请问是可能哪里出了问题?都是同一个 bundle id 的 app

fatbobman commented 1 year ago

iPhone真机和模拟器可以同步,但是iPhone 和 Mac 客户端不能同步,请问是可能哪里出了问题?都是同一个 bundle id 的 app

我没在 macOS 下使用过,不过以前有人问过这个问题,通过下面的介绍的方法解决了 https://stackoverflow.com/questions/74279246/swiftui-macos-app-not-syncing-with-icloud 相较于 iOS ,macOS 要麻烦一点

image image

答案里有更详细的说明。如果你调试成功了,也麻烦将完整的步骤和我交流一下。谢谢

KittenYang commented 1 year ago

@fatbobman 确实,我后来就是这么解决的,感谢肘子哥的文章~

2410495654 commented 9 months ago

把通知开启,不要仅在Background点一下。UNUserNotificationCenter.current().requestAuthorization

@Hank-Zhong 你好!在开发阶段不管是真机还是模拟器,都需要切换至后台再切换回来才能触发NSFetchedResultsController相关方法。请问下这是正常的吗?

fatbobman commented 9 months ago

如果后台信息正常开启了,不正常发自我的 iPhone在 2023年11月24日,10:20,2410495654 @.***> 写道: 你好!在开发阶段不管是真机还是模拟器,都需要切换至后台再切换回来才能触发NSFetchedResultsController相关方法。请问下这是正常的吗?把通知开启,不要仅在Background点一下。UNUserNotificationCenter.current().requestAuthorization

@Hank-Zhong 你好!在开发阶段不管是真机还是模拟器,都需要切换至后台再切换回来才能触发NSFetchedResultsController相关方法。请问下这是正常的吗?

—Reply to this email directly, view it on GitHub, or unsubscribe.You are receiving this because you were mentioned.Message ID: @.***>

rnmp commented 3 months ago

Truly excellent guide! Thank you for writing it.

I would add on the initialization errors that there are two possible complications to connecting even after a user successfully logs in:

  1. They may not have iCloud Drive enabled, you’ll still get a “not logged in” error so it may be a false negative.
  2. You may be logged in and have iCloud Drive enabled but you may still get a Quota exceeded error which refers to user running out of space in iCloud Drive.

Whoever is reading this: highly recommend using Console.app to monitor CloudKit errors associated with an app. It’s very noisy but a great tool, esp when first learning.

fatbobman commented 3 months ago

@rnmp Thanks for the great tips! Especially Console.app.