views:

30

answers:

1

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.

A: 

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.

Mike