English 中文(简体)
How can you use sort descriptors and predicates with core data transformable fields?
原标题:

I have an application which I wrote which used to work perfectly fine. There is new requirement that all data needs to be encrypted now, so I implemented encryption by making all my core data fields type transformable and writing custom transformers for each data type which encrypts/decrypts each element of data as it is written/read from the core data store.

The encryption works fine and I can see all the data. The problem is that sorting appears to be broken as does any even slightly complicated predicate (the ones that include subqueries).

I m guessing that the sorting is being done on the values before they are sent through the transformer (namely, sorting is being done on the encrypted values). Is there a way I can get around this? I suppose I can try using a sort descriptor and specify my own selector to do the comparison and explicitly decrypt the values first. I ll post here if that works.

The predicate situation is a bigger problem though. It s strange that it seems to mostly work but then fails when I do subqueries (which are like joins across two objects in a relationship). Is there a known problem when using transformable values and predicates or maybe I have a bug in my transformers?

Here is an example of a predicate that no longer works:

[NSPredicate predicateWithFormat:@"isdeleted == NO AND (SUBQUERY(appuserMessages, $am, $am.recAppUserID == %@ AND $am.isTrash == NO).@count > 0)", appuserid];

The predicate is performed on the Messages object which has a one-to-many relationship with AppuserMessages. This predicate is supposed to return all messages that are not isdeleted and have at least one appuserMessage where the recAppUserID is appuserid and isTrash is false. It used to work, but now returns nothing.

最佳回答

From more experimentation it seems that subquerys simply won t work when you have transformable data types.

Sort descriptors won t work either and it is understandable why. The sorting is left delegated to the database engine to do, so the sorting is done on the rows pre-transformation. In my case, that means they are sorted based on their (random) encrypted values instead of the decrypted ones.

The only solution seems to be to fetch the rows into an mutable array and then perform the sort on the array (using sortWithDescriptors).

As for the subqueries, I m assuming its a similar problem. Probably the subquery is done by the database engine pre-transformation and therefore always fails. Again, the solution is to do as complicated as a query as you can in the fetch and then the rest of the query (the subquery part) with a filter on a mutable array of the results.

问题回答

暂无回答




相关问题
Code sign Error

I have created a new iPhone application.I have two mach machines. I have created the certificate for running application in iPhone in one mac. Can I use the other mac for running the application in ...

ABPersonViewController Usage for displaying contact

Created a View based Project and added a contact to the AddressBook using ABAddressBookRef,ABRecordRef now i wanted to display the added contact ABPersonViewController is the method but how to use in ...

将音频Clips从Peter改为服务器

我不禁要问,那里是否有任何实例表明从Peit向服务器发送音响。 I m不关心电话或SIP风格的解决办法,只是一个简单的袖珍流程......

• 如何将搜查线重新定位?

我正试图把图像放在搜索条左边。 但是,问题始于这里,搜索条线不能重新布署。

热门标签