iCloud(三)保存Core Data数据到iCloud

本文介绍了如何将Core Data数据保存到iCloud,强调了在使用iCloud时的注意事项,包括监听`NSPersistentStoreDidImportUbiquitousContentChangesNotification`通知以更新UI,以及避免在iCloud账户切换时的数据混乱。同时,提到了如何处理SQLite数据库的迁移和删除操作,确保在iCloud中正确同步Core Data数据。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

iCloud综述及背景:

请参照 iCloud(一)综述iCloud(二)保存key value数据到iCloud

什么时候该用Key-Value来存储?

You can set up Core Data to store its data on iCloud. If your app doesn’t have the concept of documents or files but has a large amount of data, this method is usually the best choice.

Each instance of your app, on each device attached to an iCloud account, maintains its own local Core Data store file. When data changes locally, Core Data writes change log files to your app’s default ubiquity container.

Early in your app’s life cycle, register for the NSPersistentStoreCoordinator notification NSPersistentStoreDidImportUbiquitousContentChangesNotification. When a Core Data store has imported changes from iCloud, it posts this notification. On receiving it, refresh the affected records and update the user interface.

使用Core Data来存储到iCloud的注意点:

  • Place your SQLite Core Data store within a <my_folder>.nosync subdirectory of one of your app’s ubiquity containers. This placement ensures that, if a user switches iCloud accounts, the system takes care of keeping each account’s data associated with the correct account.

    Note: If your app must be able to operate when a user is signed out of iCloud, place the store, instead, in your sandbox container. If you do this, however, you must explicitly manage data access.

  • Do not prepopulate the contents of a new SQLite store, such as to provide seed records the first time a user launches your app, by moving or copyin

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值