将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 ofFieldValue
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
thetotal
values for all thefieldValues
still present in my filteredentryObjects
. So if I iterate through all my entries, I can build an array ofFieldValue
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 asum
on itstotal
property, it has to be of typeResults<FieldValue>
:let total:Int = fieldValues.sum(ofProperty: "total") <-- /// Error! ///
How do I convert my
[FieldValue]
array back toResults<FieldValue>
so I can do aggregate operations on it?
原文:https://stackoverflow.com/questions/40185315
最满意答案
升级到Java 8,其中
SecretKey
和RSAPrivateKey
实现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
实现SecretKey
和Destroyable
。 不要忘记销毁MyAESKey
的输入。 当然,您可以使用与Java 7及更低版本的MyDestroyable
接口类似的方法。我所知道的唯一其他方法是使用使用安全令牌(HSM / TPM /智能卡等)的提供商,其中密钥不会离开设备。 在这种情况下,密钥也可能不会被破坏,但它至少不可用。
使用本机代码(使用正确类型的内存)的提供程序也可能允许销毁关键数据。 但即使在VM之外,也许很难确保密钥数据不会留在RAM或(交换)磁盘中的任何位置。
Upgrade to Java 8 where
SecretKey
andRSAPrivateKey
implementsDestroyable
. 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 bothSecretKey
andDestroyable
. Don't forget to destroy the input to theMyAESKey
as well though. You could of course use a similar approach with your ownMyDestroyable
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.
-
Java:如何从.asc文件创建java.security.Key?(Java : how to create java.security.Key from .asc file?)[2022-07-30]
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 ...
-
java me:如何清除文件(java me: How to clear a file)[2022-04-02]
像这样调用你的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; ...
-
清除NSTableView内容(Clear NSTableView Content)[2022-05-13]
您可以通过调用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 ...