Geo*_*org 2 ios icloud swift cloudkit cloudkit-sharing
在我的应用程序中,我决定使用 CloudKit 作为我的同步后端。我的应用程序与项目无关,但为了简单起见,我们可以这么说......
所以......在我的应用程序中,用户将有多个项目。其中每个都包含与该项目关联的多个实体。比如任务,还有提醒等等。
所有这些数据将存储在用户的私人数据库中。公共数据库中不会有任何内容。
现在一个用户可以拥有多个项目。
我的第一个问题:每个项目都应该在它自己的 CKRecordZone 中吗?我没有看到这样做的好处?!?有人可以向我解释一下拥有多个记录区的好处是什么吗?所以目前所有项目都在一个区域。
接下来,我希望用户能够与其他人共享他的所有数据。目前的问题是,由于该项目当前是我数据库中的根记录,我需要为每个项目创建一个共享,对吗?!?在我的应用程序中,单独邀请用户加入每个项目实际上没有意义,因此我想以某种方式对其进行存档。创建一个将项目作为子项目的新根记录,然后用户邀请某人加入这个新根记录是否有意义?
最后一个问题...是否有类似 Sack-Team 之类的组织可以询问有关 CloudKit 的问题?似乎比在 stackoverflow 上开始一个新问题更容易,因为我的问题非常针对我的应用程序......
Good questions. Here's what I recommend.
First off, you only need one zone. But to share records from it, it must be a custom zone (you can't use the _defaultZone). Honestly, zones in CloudKit are weird and I'm not sure why they exist. Apple seems to be passing database sharding challenges on to their developers. :)
Create a custom zone like this:
let customZone = CKRecordZone(zoneName: "projectZone")
// Save the zone in the private database
let container = CKContainer(identifier: "...")
let privateDB = container.privateCloudDatabase
privateDB.save(customZone){ zone, error in
if let error = error{
print("Zone creation error: \(String(describing: error))")
}else{
print("Zone created: \(zone)")
}
}
Run Code Online (Sandbox Code Playgroud)
I would create record types like this:
Project (root record)TaskReminderOne of the nice things about CloudKit is that you can create relationships between records. This means you can automatically share the children of a root record without having to set up CKShares for each child individually.
Below is an example that walks through how you would set those fields on the records.
//Get a reference to the zone you created
let zoneID = CKRecordZoneID(zoneName: "projectZone", ownerName: CKCurrentUserDefaultName)
//Create a project record
let projectRecord = CKRecord(recordType: "Project", zoneID: zoneID)
projectRecord.setValue("My Cool Project", forKey: "name")
//Create a task record
let taskRecord = CKRecord(recordType: "Task", zoneID: zoneID)
taskRecord.setValue("My Task Name", forKey: "name")
//Create an association between the task and its parent project
let parentReference = CKReference(record: projectRecord, action: .deleteSelf)
taskRecord.setValue(parentReference, forKey: "project")
//When sharing, allow this task to be automatically shared if the parent project is shared
taskRecord.setParent(projectRecord)
Run Code Online (Sandbox Code Playgroud)
All of this presupposes that you create fields for your Project and Task record types of name (type: String). Then on the Task record type, you would have a project field of type Reference.
我希望这对您有所帮助,并且至少可以帮助您入门。我不知道 CloudKit Slack 频道,但如果您听说过,请告诉我!:)
| 归档时间: |
|
| 查看次数: |
1232 次 |
| 最近记录: |