阅读背景:

CloudKit,添加项目到列表的连接或有效方式

来源:互联网 

I'm using CloudKit to manage a list of messages (record type Message with a field title and body). All messages are public and I want to maintain which Message the user has read using the mobile app.

我正在使用CloudKit管理消息列表(用字段标题和正文记录类型消息)。所有消息都是公共的,我想维护用户使用移动应用程序读取的消息。

The app can have thousands of users and messages. And I use swift3.

这个应用程序可以有上千个用户和消息。我使用swift3。

I've think of different way to do it but they seems quite poor in term of performance:

我想了不同的方法,但他们的表现似乎很差:

  • add a field 'readers' to Message which is a list of string corresponding of user Id. The problem is that if I want to add a new user ID I must load all the list. This is problematic for a mobile app in case of a lot of users have read the message. Can I lazy fetch a list field and add a value to it without downloading all the list (like in classic Orm)?
  • 向消息中添加一个“读取器”字段,该字段是用户Id对应的字符串列表。对于一个移动应用程序来说,如果有很多用户读过这条消息,这就有问题了。我是否可以延迟获取列表字段并在不下载所有列表的情况下添加值(如在classic Orm中)?
  • add another record type 'Reader' which has two fields: a user ID and a message ID. I can't find a way to join Message and Reader in a predicate to download only Message that the user hasn't read. Is that possible?
  • 添加另一种记录类型“Reader”,它有两个字段:一个用户ID和一个消息ID。这有可能吗?
  • As suggested by Matthew: add a record type ReadArticle in the private database that stores only a CKReference to a message. The problem is that we need to download all message ID before sending them in a NOT predicate.
  • 如Matthew所建议的:在私有数据库中添加一个记录类型ReadArticle,它只存储一条消息的CKReference。问题是,在以非谓词发送消息之前,我们需要下载所有消息ID。

I don't know how to solve this problem with a database like CloudKit.

我不知道如何使用CloudKit这样的数据库来解决这个问题。

Any advice ?

任何建议吗?

1 个解决方案

#1


1  

Readers field

This approach is not ideal for the exact reasons you pointed out, and it additionally is unsafe as a String could theoretically contain anything.

这种方法并不理想,因为您指出的确切原因,而且它是不安全的,因为字符串理论上可以包含任何东西。

Reader object

Even if there was a way to get this to work you'd be storing user ids in the public database. That's probably not a privacy-conscious thing to do.

即使有方法可以让它工作,你也要在公共数据库中存储用户id。这可能并不是一件有隐私意识的事情。

ReadMessage Record in Private Database

Suppose you had a Record Type called ReadArticle. This object would contain exactly one field, a CKReference to a Message record.

假设您有一个名为ReadArticle的记录类型。该对象将只包含一个字段,即消息记录的CKReference。

Then, when someone reads a Message, you take the recordId of that Message, create a CKReference from it, and place that reference in the "message" field of a new CKRecord object of type ReadMessage. Then, you save it to the user's private database. Because it's a CKReference, it won't actually take up hardly any space in iCloud because it's just a pointer, and because you'd use the user's private database there's no need to explicitly identify the user.

然后,当某人读取一条消息时,您获取该消息的记录,从中创建一个CKReference,并将该引用放在一个新的CKRecord对象的“Message”字段中,该对象是ReadMessage类型。然后,将其保存到用户的私有数据库中。因为它是一个CKReference,它实际上不会占用iCloud的任何空间,因为它只是一个指针,而且因为你要使用用户的私有数据库,所以不需要显式地识别用户。

Then when you want all unread messages, fetch the ReadMessage record, and create a NOT predicate to receive all Message records where the record id is not any of the read ones. CloudKit definitely supports NOT predicates, but if it happens to not support NOT predicates specifying record ids, than you could use some other unique field on message instead.

然后,当您想要所有未读的消息时,获取ReadMessage记录,并创建一个NOT谓词来接收所有消息记录,其中记录id不是任何读取的记录。CloudKit肯定不支持谓词,但是如果它不支持指定记录id的非谓词,那么您可以在消息上使用其他惟一字段。


分享到: