首页 \ 问答 \ 将Swift数组的Realm对象转换回Realm结果(Convert Swift Array of Realm Objects Back to Realm Results)

将Swift数组的Realm对象转换回Realm结果(Convert Swift Array of Realm Objects Back to Realm Results)

我有一个Entry类,其中包含FieldValue对象列表:

class Entry: Object{
  let fieldValues = List<FieldValue>()
  //...
}

class FieldValue: Object{
  dynamic var total = 0
  //...
}

我对查询中的一些条目进行了大量过滤,如下所示:

var entryObjects = realm.objects(Entry.self).filter(...)

我现在想要sum我过滤的entryObjects仍然存在的所有fieldValues entryObjects 。 因此,如果我遍历所有条目,我可以构建一个FieldValue对象数组,如下所示:

var fieldValues = [FieldValue]()
for entry in entryObjects{
  fieldValues += entry.fieldValues  
}

我现在有[FieldValue]类型的fieldValues 。 但是为了对其total属性进行sum ,它必须是Results<FieldValue>类型:

let total:Int = fieldValues.sum(ofProperty: "total") <-- /// Error! ///

如何将我的[FieldValue]数组转换回Results<FieldValue>以便我可以对它进行聚合操作?


I have an Entry class that has a list of FieldValue objects within it:

class Entry: Object{
  let fieldValues = List<FieldValue>()
  //...
}

class FieldValue: Object{
  dynamic var total = 0
  //...
}

I do a bunch of filtering on some entries in a query like this:

var entryObjects = realm.objects(Entry.self).filter(...)

I now want to sum the total values for all the fieldValues still present in my filtered entryObjects. So if I iterate through all my entries, I can build an array of FieldValue objects like this:

var fieldValues = [FieldValue]()
for entry in entryObjects{
  fieldValues += entry.fieldValues  
}

I now have fieldValues of type [FieldValue]. But in order to do a sum on its total property, it has to be of type Results<FieldValue>:

let total:Int = fieldValues.sum(ofProperty: "total") <-- /// Error! ///

How do I convert my [FieldValue] array back to Results<FieldValue> so I can do aggregate operations on it?


原文:https://stackoverflow.com/questions/40185315
更新时间:2023-10-31 12:10

最满意答案

升级到Java 8,其中SecretKeyRSAPrivateKey实现Destroyable 。 但是,快速测试表明,这不适用于AES密钥,也不适用于本地生成的RSA私钥。

以下代码确实有效,但它仅在第二个init(!)之后失败,因此请注意可以缓存密钥信息(AES需要子密钥派生,因此子密钥可能会延续)。 在使用后使用单独的(零)密钥重新初始化任何密码可能是个好主意。 此外,它不能防止VM本身复制数据,例如在垃圾收集后的内存压缩期间。

MyAESKey myAESKey = new MyAESKey(new byte[16]);
Cipher aes = Cipher.getInstance("AES");
aes.init(Cipher.ENCRYPT_MODE, myAESKey);
aes.doFinal("owlstead".getBytes());
myAESKey.destroy();
aes.doFinal("owlstead".getBytes());
aes.init(Cipher.ENCRYPT_MODE, myAESKey);
aes.doFinal("owlstead".getBytes());

MyAESKey实现SecretKeyDestroyable 。 不要忘记销毁MyAESKey的输入。 当然,您可以使用与Java 7及更低版本的MyDestroyable接口类似的方法。

我所知道的唯一其他方法是使用使用安全令牌(HSM / TPM /智能卡等)的提供商,其中密钥不会离开设备。 在这种情况下,密钥也可能不会被破坏,但它至少不可用。

使用本机代码(使用正确类型的内存)的提供程序也可能允许销毁关键数据。 但即使在VM之外,也许很难确保密钥数据不会留在RAM或(交换)磁盘中的任何位置。


Upgrade to Java 8 where SecretKey and RSAPrivateKey implements Destroyable. However, a quick test shows that this doesn't work for AES keys nor RSA private keys generated locally.

The following code does work, but it fails only after the second init (!) so beware that key information may be cached (AES requires sub-key derivation, so the sub keys may linger on). It may be a good idea to re-init any cipher with a separate (zero) key after use. Furthermore, it does not protect against copying of the data by the VM itself, e.g. during memory compaction after garbage collection.

MyAESKey myAESKey = new MyAESKey(new byte[16]);
Cipher aes = Cipher.getInstance("AES");
aes.init(Cipher.ENCRYPT_MODE, myAESKey);
aes.doFinal("owlstead".getBytes());
myAESKey.destroy();
aes.doFinal("owlstead".getBytes());
aes.init(Cipher.ENCRYPT_MODE, myAESKey);
aes.doFinal("owlstead".getBytes());

where MyAESKey implements both SecretKey and Destroyable. Don't forget to destroy the input to the MyAESKey as well though. You could of course use a similar approach with your own MyDestroyable interface for Java 7 and below.

The only other method that I know of is to use a provider that uses a security token (HSM / TPM / smart card etc.) where the key does not leave the device. In that case the key may not be destroyed either, but it is at least not available.

Providers that use native code (which uses the right kind of memory) may also allow for destruction of key data. But even outside the VM it may be hard to ensure that the key data is not left anywhere in RAM or (swap) disk.

相关问答

更多
  • 使用 : tinyMCE.activeEditor.setContent(''); 将指定的内容设置为编辑器实例,这将在使用不同的清理规则选项设置之前清理内容。 参考: TinyMce 希望能帮助到你。 Use : tinyMCE.activeEditor.setContent(''); Sets the specified content to the editor instance, this will cleanup the content before it gets set using the ...
  • about:blank 是一个空白的“URL”。 总是很清楚 您可以将页面的来源设置为,并将其清除。 about:blank is a "URL" that is blank. It's always clear You can set the page's source to that, and it will clear.
  • Robert和Maarten Bodewes的回复是其中一条路。 我的公钥是.asc文件和.skr中的私钥(密钥密钥环) 我以这种方式实现它,它对我有用: public static PublicKey getPublicKey( String filePath) throws PGPException, NoSuchProviderException, FileNotFoundException ...
  • span元素不能自我关闭,即使它们没有内容。 如果这样做,浏览器会将按钮放在 span元素内( 右键单击-> Inspect Element )。 同 它按预期工作。 span elements cannot be self closed, even if they have no content. If you do that, the browser will put the button inside the span element (Right ...
  • 您将对ArrayList的引用放入Hashtable 。 如果要将ArrayList中的值添加到表中,则必须迭代列表中的元素。 for(Object element : podmienkyOblast) { // TODO generate a key for your value here String key = "..."; podmienky.put(key, element); } You put a reference to your ArrayList into th ...
  • 像这样调用你的write()方法: .write((new String()).getBytes()); 这将使您的文件为空。 您也可以删除文件并创建一个新文件。 这也将导致具有相同文件名的空文件。 Call your write() method like this: .write((new String()).getBytes()); This will make your file empty. You can also delete your file and create a new one. ...
  • 这个小代码可能会帮助你解释一些事情: import java.awt.event.*; import javax.swing.*; public class FrameDisplayTest implements ActionListener { /* * Creating an object of JFrame instead of extending it * has no side effects. */ private JFrame frame; ...
  • 您可以通过调用removeRowsAtIndexes: 此处为文档 )从NSTableView逐个删除对象,然后在NSTableView上调用reloadData 。 如果要清除所有数据,则需要清除数据源(通常是为表提供数据的NSArray),然后调用reloadData 。 要清除数据源,如果它是NSDictionary或NSArray,请在NSDictionary或NSArray上调用removeAllObjects 。 以下是您可能会做的一个示例: [myDataSourceDictionary re ...
  • 我发现下面的代码可以解决问题; objExplorer.Refresh I found that the below code does the trick; objExplorer.Refresh
  • 升级到Java 8,其中SecretKey和RSAPrivateKey实现Destroyable 。 但是,快速测试表明,这不适用于AES密钥,也不适用于本地生成的RSA私钥。 以下代码确实有效,但它仅在第二个init(!)之后失败,因此请注意可以缓存密钥信息(AES需要子密钥派生,因此子密钥可能会延续)。 在使用后使用单独的(零)密钥重新初始化任何密码可能是个好主意。 此外,它不能防止VM本身复制数据,例如在垃圾收集后的内存压缩期间。 MyAESKey myAESKey = new MyAESKey(ne ...

相关文章

更多

最新问答

更多
  • 您如何使用git diff文件,并将其应用于同一存储库的副本的本地分支?(How do you take a git diff file, and apply it to a local branch that is a copy of the same repository?)
  • 将长浮点值剪切为2个小数点并复制到字符数组(Cut Long Float Value to 2 decimal points and copy to Character Array)
  • OctoberCMS侧边栏不呈现(OctoberCMS Sidebar not rendering)
  • 页面加载后对象是否有资格进行垃圾回收?(Are objects eligible for garbage collection after the page loads?)
  • codeigniter中的语言不能按预期工作(language in codeigniter doesn' t work as expected)
  • 在计算机拍照在哪里进入
  • 使用cin.get()从c ++中的输入流中丢弃不需要的字符(Using cin.get() to discard unwanted characters from the input stream in c++)
  • No for循环将在for循环中运行。(No for loop will run inside for loop. Testing for primes)
  • 单页应用程序:页面重新加载(Single Page Application: page reload)
  • 在循环中选择具有相似模式的列名称(Selecting Column Name With Similar Pattern in a Loop)
  • System.StackOverflow错误(System.StackOverflow error)
  • KnockoutJS未在嵌套模板上应用beforeRemove和afterAdd(KnockoutJS not applying beforeRemove and afterAdd on nested templates)
  • 散列包括方法和/或嵌套属性(Hash include methods and/or nested attributes)
  • android - 如何避免使用Samsung RFS文件系统延迟/冻结?(android - how to avoid lag/freezes with Samsung RFS filesystem?)
  • TensorFlow:基于索引列表创建新张量(TensorFlow: Create a new tensor based on list of indices)
  • 企业安全培训的各项内容
  • 错误:RPC失败;(error: RPC failed; curl transfer closed with outstanding read data remaining)
  • C#类名中允许哪些字符?(What characters are allowed in C# class name?)
  • NumPy:将int64值存储在np.array中并使用dtype float64并将其转换回整数是否安全?(NumPy: Is it safe to store an int64 value in an np.array with dtype float64 and later convert it back to integer?)
  • 注销后如何隐藏导航portlet?(How to hide navigation portlet after logout?)
  • 将多个行和可变行移动到列(moving multiple and variable rows to columns)
  • 提交表单时忽略基础href,而不使用Javascript(ignore base href when submitting form, without using Javascript)
  • 对setOnInfoWindowClickListener的意图(Intent on setOnInfoWindowClickListener)
  • Angular $资源不会改变方法(Angular $resource doesn't change method)
  • 在Angular 5中不是一个函数(is not a function in Angular 5)
  • 如何配置Composite C1以将.m和桌面作为同一站点提供服务(How to configure Composite C1 to serve .m and desktop as the same site)
  • 不适用:悬停在悬停时:在元素之前[复制](Don't apply :hover when hovering on :before element [duplicate])
  • 常见的python rpc和cli接口(Common python rpc and cli interface)
  • Mysql DB单个字段匹配多个其他字段(Mysql DB single field matching to multiple other fields)
  • 产品页面上的Magento Up出售对齐问题(Magento Up sell alignment issue on the products page)