编程技术网

关注微信公众号,定时推送前沿、专业、深度的编程技术资料。

 找回密码
 立即注册

QQ登录

只需一步,快速开始

极客时间

如何为我的CloudKit数据建模:How to model my CloudKit data

Ryan Gooler 云计算 2022-5-10 12:02 9人围观

腾讯云服务器
如何为我的CloudKit数据建模的处理方法

在我的应用中,我决定将CloudKit用作我的同步后端.我的应用程序与项目无关,但为简单起见,可以这么说...

In my app I decided to use CloudKit as my sync-backend. My app is not about projects, but for simplicity let's say so...

所以...在我的应用程序中,用户将有多个项目.每个项目都包含与该项目关联的多个实体.例如任务,还有提醒等.

So... In my app users will have multiple projects. Each of those contains multiple entities associated with that project. For example tasks, but also reminders and so on.

所有这些数据将存储在用户专用数据库中.什么都不会在公共数据库中.

All this data will be stored in the users private database. Nothing will be in the public database.

现在用户可以拥有多个项目.

Now a user can have multiple projects.

我的第一个问题:每个项目都应该在自己的CKRecordZone中吗?我没有看到这样做的好处?!?有人可以向我解释拥有多个记录区的好处是什么?因此,目前所有项目都在一个区域中.

My first question: Should each project be in it's own CKRecordZone? I didn't see a benefit of doing so?!? Can somebody explain to me what the benefit of having multiple record zones is? So currently all the projects are in ONE zone.

接下来,我希望用户能够与其他人共享他的所有数据.当前的问题是,由于该项目当前是数据库中的根记录,因此我需要为每个项目创建一个共享,对吗?在我的应用中,单独邀请用户加入每个项目并没有什么意义,所以我想以某种方式对其进行存档.创建一个以项目为子项目的新根记录是否有意义,然后用户邀请某人加入该新根记录?

Next, I'd like the user to be able to share ALL his data with somebody else. The problem currently is that as the project is currently the root record in my database, I'd need to create a share for each of those projects, right?!? In my app it doesn't really make sense to invite users to each project separately, so I'd like to somehow archive that. Would it make sense, to create a new root-record that has the projects as childs and then the user would invite somebody to this new root-record?

最后一个问题...是否有类似Sack-Team之类的问题来询问有关CloudKit的问题?这似乎比在stackoverflow上开始一个新问题要容易,因为我的问题是我的应用程序特有的...

Final question... is there something like a Sack-Team or so to ask questions about CloudKit? Would seem to be easier than starting a new question here on stackoverflow as my questions are quite specific to my app...

问题解答

好的问题.这就是我的建议.

Good questions. Here's what I recommend.

首先,您只需要一个区域.但是要共享记录,它必须是一个 custom 区域(您不能使用 _defaultZone ).老实说,CloudKit中的区域很奇怪,我不确定它们为什么存在.苹果似乎正在将数据库分片的挑战传递给他们的开发人员.:)

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)") } } 

记录类型

我将创建这样的记录类型:

Record Types

I would create record types like this:

  • 项目(根记录)
  • 任务
  • 提醒
  • Project (root record)
  • Task
  • Reminder

CloudKit的优点之一就是您可以在记录之间创建关系.这意味着您可以自动共享根记录的子级,而不必为每个子级分别设置 CKShare .

One 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 zone "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) 

所有这些前提是您为 name Project Task 记录类型创建字段(类型: String ).然后在 Task 记录类型上,您将具有类型为 Reference project 字段.

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频道,但是如果您听到一个频道,请告诉我!:)

I hope that helps and will at least get you started. I'm not aware of a CloudKit Slack channel, but if you hear about one, let me know! :)

这篇关于如何为我的CloudKit数据建模的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程技术网(www.editcode.net)!

腾讯云服务器 阿里云服务器
关注微信
^