CloudKit 使用教程
前言
CloudKit 属于 BaaS(Backend as a Service)一种形式,能够提供基本的增删改查功能,同时提供通知订阅服务。
介绍
CKRecord 记录的类型
使用

支持类型
增加
1 | CKRecord * recod ... |
查询
CONTAINS 列表
has a value type of STRING and cannot be queried using filter type LIST_CONTAINS”>1
2
3
4
5NSPredicate predicate = nil;
predicate = [NSPredicate predicateWithFormat:@"ANY favoriteColors = 'red'"];
predicate = [NSPredicate predicateWithFormat:@"favoriteColors CONTAINS 'red'"];
predicate = [NSPredicate predicateWithFormat:@"'red' IN favoriteColors"];
predicate = [NSPredicate predicateWithFormat:@"%K CONTAINS %@", @"favoriteColors", @"red"]; //%k 表示字段(存储数组的),%@ 表示包含在%k字段中的值
1 | - (void)fetchRecordWithID:(CKRecordID *)recordID completionHandler:(void (^)(CKRecord * _Nullable record, NSError * _Nullable error))completionHandler; |
删除
1 | - (void)deleteRecordWithID:(CKRecordID *)recordID completionHandler:(void (^)(CKRecordID * _Nullable recordID, NSError * _Nullable error))completionHandler; |
修改
所谓修改即:先查询,后保存。
订阅
订阅与通知相差不多,当数据发生变化时(增加、删除、更新),会收到通知。订阅的类型也是相同的,有增加、删除、更新、只收到一次订阅通知(发送后自动删除)。
通知:
1 | { |
1 | Printing description of cloudKitNotification: |
1 | (lldb) po response |
容器数据
默认容器1
CKContainer * defaultContainer = [CKContainer defaultContainer];
自定义容器1
CKContainer * customContainer = [CKContainer containerWithIdentifier:@"iCloud.com.devhitao.ios"];
小知识
关于 recordID.recordName
和 creatorUserRecordID.recordName
的区别:
对于 Users 表中的字段值来说,若当前用户获取自己的记录:1
2
3
4(lldb) po userRecord.recordID.recordName
_32e264944c0f480c52e68csdsd7653371 //用户唯一标识 或 这条记录的唯一标识
(lldb) po userRecord.creatorUserRecordID.recordName
__defaultOwner__
对于当前用户, 获取 Users 表中其他用户的记录返回值 creatorUserRecordID.recordName
和 recordID.recordName
相同。
对于当前用户,获取非自己创建的 Record(其他Record, 不包括 Users):1
2
3
4(lldb) po userRecord.recordID.recordName
_32e264944c0f480c52e68csdsd7653371 //这条记录的唯一标识
(lldb) po userRecord.creatorUserRecordID.recordName
_42323e264944c0f480c52e68csdsdsd76 //这条记录创建人的唯一标识
对于其他 Record,若当前创建人请求自己创建的 Record,userRecord.creatorUserRecordID.recordName
返回__defaultOwner__
,userRecord.recordID.recordName
为记录的唯一 id。
注:
CloudKit Dashboard 分为开发环境和生产环境,在 deployed 之前,可以在开发环境中删除 record types 和 Fields,发布之后不能够再修改已发布的 Record 和 字段 类型。
常量1
2
3
4CKCurrentUserDefaultName
__defaultOwner__
CKOwnerDefaultName
__defaultOwner__
FAQ
Q: 在 Xcode 中访问 Cloudkit 生产环境 中的数据?
A: 在 Example.entitlements 环境中,添加 com.apple.developer.icloud-container-environment
并设置为 Production
即可,已验证可行。(注:需要重新编译工程,清除缓存)
1 | <key>com.apple.developer.icloud-container-environment</key> |
参考来源:Use production CloudKit during development?
Q: 当使用 initWithRecordType:predicate: 方法检索 Users
记录集合时会遇到 "Permission Failure" (10/2007); server message = "Can't query system types";
,大概意思是不允许查询系统类型字段,同时苹果官方给出的信息是:
You cannot query for user records and executing a query where the record type is set to CKRecordTypeUserRecord results in an error. You must fetch user records directly using their ID.
您不能查询用户记录集,并且当执行查询 Record Type 为 CKRecordTypeUserRecord 结果集时会出现一个错误。你必须使用其ID直接获取用户记录。
Q: "Invalid Arguments" (12/2006); server message = "attempt to add owning ref to user rec";
将其改为 CKReference * userRefer = [[CKReference alloc] initWithRecord:aliasRecord action:CKReferenceActionNone];
Error when saving user record: “attempt to add owning ref to user rec”
Q: 每条记录大小问题
To ensure the speed of fetching and saving records, the data stored by a record must not exceed 1 MB. Assets do not count against this limit but all other data types do.
为了确保记录的抓取和保存速度,存储在记录中的数据一定不要超过 1 MB,Assets 类型不会被计算在内且不受此限制,但是上面其他类型都需要准守。
Q: 当出现Field 'recordName' is not marked queryable
时,意思是提醒我们索引不存在,需要在控制板中将此记录字段添加到可查询索引。
参考资料
因为在学习过程中遇到的问题比较多,所以参考的资料就多,大致分为官方资料和网络资料两种。
官方
- Documentation CloudKit
- Designing for CloudKit
- Predicate Programming Guide
- Migrating to CloudKit
- CloudKit
网络
文档信息
- 版权声明:自由转载-保持署名-非商用-非衍生 ( CC BY-NC-ND 4.0 )