CoreData: ValueTransformer functions are not called





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







3















I'm storing some key/value-pairs as Strings in CoreData which - in a newer version - should now be encrypted. To not alone relying on Apples DataProtection I now want to encrypt the data before storing with RNCryptor and with the help of the ValueTransformer class.



However, the transform-functions are not called, neither debug-outputs nor breakpoints are triggered. The strings are now stored as data objects, but can be read in plain text in binary representation - so they are obviously not encrypted.



DB Browser



Here is what I changed:




  • Added and activated a new migration/database scheme

  • Changed key and value column from Type String to Transformable

  • Set the Value Transformer to »EncryptedStringTransformer« and the Custom Class to »String«


Data model




  • and finally I added a file Encryption.swift with the following implementation:


import Foundation
import RNCryptor

class EncryptedStringTransformer : ValueTransformer {
let password = "SuperSecurePassword"

override class func allowsReverseTransformation() -> Bool{
return true
}

func transformedValue(value: String?) -> NSData? {

guard let data = value else {
return nil
}
let encryptData = Data(data.utf8)
let ciphertext = RNCryptor.encrypt(data: encryptData, withPassword: password)
return ciphertext as NSData
}

func reverseTransformedValue(value: NSData?) -> String? {
guard value != nil else {
return "nil"
}
do {
let originalData = try RNCryptor.decrypt(data: (value! as Data), withPassword: password)
return String(data: originalData, encoding: String.Encoding(rawValue: String.Encoding.utf8.rawValue))!

} catch {
print(error)
return "nil"
}
}
}


So, the app continues to work flawlessly, and all database objects can be stored and retrieved (with the difference that they are now stored as a Data object rather than a String). I'm checking the SQLite database directly with »DB Browser for SQLite«.



The expected behavior would be encrypted entries in CoreData. Can someone tell me what I am missing? Some tutorials I found don't do any additional implementations and the few articles here on StackOverflow aren't helping either with this issue.



I tried to change the output data from the transform-functions from Data to NSData, with no result. Am I missing something, so that the ValueTransformer is actually called? Any hint would be highly appreciated!










share|improve this question























  • Set a breakpoint in transformedValue to make sure the method is called and the encryption is happening. See also stackoverflow.com/questions/1598600/…. There is a suggestion that the transformer class must be registered before it will be called. I don't know if this is true or not, but it is worth checking out.

    – Mike Taverne
    Nov 24 '18 at 0:13


















3















I'm storing some key/value-pairs as Strings in CoreData which - in a newer version - should now be encrypted. To not alone relying on Apples DataProtection I now want to encrypt the data before storing with RNCryptor and with the help of the ValueTransformer class.



However, the transform-functions are not called, neither debug-outputs nor breakpoints are triggered. The strings are now stored as data objects, but can be read in plain text in binary representation - so they are obviously not encrypted.



DB Browser



Here is what I changed:




  • Added and activated a new migration/database scheme

  • Changed key and value column from Type String to Transformable

  • Set the Value Transformer to »EncryptedStringTransformer« and the Custom Class to »String«


Data model




  • and finally I added a file Encryption.swift with the following implementation:


import Foundation
import RNCryptor

class EncryptedStringTransformer : ValueTransformer {
let password = "SuperSecurePassword"

override class func allowsReverseTransformation() -> Bool{
return true
}

func transformedValue(value: String?) -> NSData? {

guard let data = value else {
return nil
}
let encryptData = Data(data.utf8)
let ciphertext = RNCryptor.encrypt(data: encryptData, withPassword: password)
return ciphertext as NSData
}

func reverseTransformedValue(value: NSData?) -> String? {
guard value != nil else {
return "nil"
}
do {
let originalData = try RNCryptor.decrypt(data: (value! as Data), withPassword: password)
return String(data: originalData, encoding: String.Encoding(rawValue: String.Encoding.utf8.rawValue))!

} catch {
print(error)
return "nil"
}
}
}


So, the app continues to work flawlessly, and all database objects can be stored and retrieved (with the difference that they are now stored as a Data object rather than a String). I'm checking the SQLite database directly with »DB Browser for SQLite«.



The expected behavior would be encrypted entries in CoreData. Can someone tell me what I am missing? Some tutorials I found don't do any additional implementations and the few articles here on StackOverflow aren't helping either with this issue.



I tried to change the output data from the transform-functions from Data to NSData, with no result. Am I missing something, so that the ValueTransformer is actually called? Any hint would be highly appreciated!










share|improve this question























  • Set a breakpoint in transformedValue to make sure the method is called and the encryption is happening. See also stackoverflow.com/questions/1598600/…. There is a suggestion that the transformer class must be registered before it will be called. I don't know if this is true or not, but it is worth checking out.

    – Mike Taverne
    Nov 24 '18 at 0:13














3












3








3


1






I'm storing some key/value-pairs as Strings in CoreData which - in a newer version - should now be encrypted. To not alone relying on Apples DataProtection I now want to encrypt the data before storing with RNCryptor and with the help of the ValueTransformer class.



However, the transform-functions are not called, neither debug-outputs nor breakpoints are triggered. The strings are now stored as data objects, but can be read in plain text in binary representation - so they are obviously not encrypted.



DB Browser



Here is what I changed:




  • Added and activated a new migration/database scheme

  • Changed key and value column from Type String to Transformable

  • Set the Value Transformer to »EncryptedStringTransformer« and the Custom Class to »String«


Data model




  • and finally I added a file Encryption.swift with the following implementation:


import Foundation
import RNCryptor

class EncryptedStringTransformer : ValueTransformer {
let password = "SuperSecurePassword"

override class func allowsReverseTransformation() -> Bool{
return true
}

func transformedValue(value: String?) -> NSData? {

guard let data = value else {
return nil
}
let encryptData = Data(data.utf8)
let ciphertext = RNCryptor.encrypt(data: encryptData, withPassword: password)
return ciphertext as NSData
}

func reverseTransformedValue(value: NSData?) -> String? {
guard value != nil else {
return "nil"
}
do {
let originalData = try RNCryptor.decrypt(data: (value! as Data), withPassword: password)
return String(data: originalData, encoding: String.Encoding(rawValue: String.Encoding.utf8.rawValue))!

} catch {
print(error)
return "nil"
}
}
}


So, the app continues to work flawlessly, and all database objects can be stored and retrieved (with the difference that they are now stored as a Data object rather than a String). I'm checking the SQLite database directly with »DB Browser for SQLite«.



The expected behavior would be encrypted entries in CoreData. Can someone tell me what I am missing? Some tutorials I found don't do any additional implementations and the few articles here on StackOverflow aren't helping either with this issue.



I tried to change the output data from the transform-functions from Data to NSData, with no result. Am I missing something, so that the ValueTransformer is actually called? Any hint would be highly appreciated!










share|improve this question














I'm storing some key/value-pairs as Strings in CoreData which - in a newer version - should now be encrypted. To not alone relying on Apples DataProtection I now want to encrypt the data before storing with RNCryptor and with the help of the ValueTransformer class.



However, the transform-functions are not called, neither debug-outputs nor breakpoints are triggered. The strings are now stored as data objects, but can be read in plain text in binary representation - so they are obviously not encrypted.



DB Browser



Here is what I changed:




  • Added and activated a new migration/database scheme

  • Changed key and value column from Type String to Transformable

  • Set the Value Transformer to »EncryptedStringTransformer« and the Custom Class to »String«


Data model




  • and finally I added a file Encryption.swift with the following implementation:


import Foundation
import RNCryptor

class EncryptedStringTransformer : ValueTransformer {
let password = "SuperSecurePassword"

override class func allowsReverseTransformation() -> Bool{
return true
}

func transformedValue(value: String?) -> NSData? {

guard let data = value else {
return nil
}
let encryptData = Data(data.utf8)
let ciphertext = RNCryptor.encrypt(data: encryptData, withPassword: password)
return ciphertext as NSData
}

func reverseTransformedValue(value: NSData?) -> String? {
guard value != nil else {
return "nil"
}
do {
let originalData = try RNCryptor.decrypt(data: (value! as Data), withPassword: password)
return String(data: originalData, encoding: String.Encoding(rawValue: String.Encoding.utf8.rawValue))!

} catch {
print(error)
return "nil"
}
}
}


So, the app continues to work flawlessly, and all database objects can be stored and retrieved (with the difference that they are now stored as a Data object rather than a String). I'm checking the SQLite database directly with »DB Browser for SQLite«.



The expected behavior would be encrypted entries in CoreData. Can someone tell me what I am missing? Some tutorials I found don't do any additional implementations and the few articles here on StackOverflow aren't helping either with this issue.



I tried to change the output data from the transform-functions from Data to NSData, with no result. Am I missing something, so that the ValueTransformer is actually called? Any hint would be highly appreciated!







swift encryption core-data rncryptor






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 23 '18 at 17:16









MarcMarc

4324




4324













  • Set a breakpoint in transformedValue to make sure the method is called and the encryption is happening. See also stackoverflow.com/questions/1598600/…. There is a suggestion that the transformer class must be registered before it will be called. I don't know if this is true or not, but it is worth checking out.

    – Mike Taverne
    Nov 24 '18 at 0:13



















  • Set a breakpoint in transformedValue to make sure the method is called and the encryption is happening. See also stackoverflow.com/questions/1598600/…. There is a suggestion that the transformer class must be registered before it will be called. I don't know if this is true or not, but it is worth checking out.

    – Mike Taverne
    Nov 24 '18 at 0:13

















Set a breakpoint in transformedValue to make sure the method is called and the encryption is happening. See also stackoverflow.com/questions/1598600/…. There is a suggestion that the transformer class must be registered before it will be called. I don't know if this is true or not, but it is worth checking out.

– Mike Taverne
Nov 24 '18 at 0:13





Set a breakpoint in transformedValue to make sure the method is called and the encryption is happening. See also stackoverflow.com/questions/1598600/…. There is a suggestion that the transformer class must be registered before it will be called. I don't know if this is true or not, but it is worth checking out.

– Mike Taverne
Nov 24 '18 at 0:13












1 Answer
1






active

oldest

votes


















1














You haven't overridden the correct methods of ValueTransformer. Your methods are:



func transformedValue(value: String?) -> NSData?
func reverseTransformedValue(value: NSData?) -> String?


The correct methods are:



func transformedValue(_ value: Any?) -> Any?
func reverseTransformedValue(_ value: Any?) -> Any?


The big hint that you're implementing the wrong methods is that you didn't need to add the override keyword.



BTW, this expression:



encoding: String.Encoding(rawValue: String.Encoding.utf8.rawValue))!


can be replaced with:



encoding: .uf8


It would also likely be better to replace your return "nil" with return nil; it's a String?, so it can be nil if things go wrong.






share|improve this answer


























  • Thanks rob and sorry for the late reply. Your hint with the missing override statement was very helpful and instructive! The utf8-part was not final but it's corrected now. However, the correct ValueTransformer-methods still won't kick in. breakpoints within those two functions aren't triggered and I'm completely clueless on which part I should have a look at… Do you have an additional hint what could be missing?

    – Marc
    Jan 4 at 12:34








  • 1





    I believe you still need to mark EncryptedStringTransformer as @objc, even though it's a subclass of ValueTransformer. A good way to explore that is to change the name of the transformer to something that you know doesn't exist, to see whether you get errors or not. Swift types do not always have exactly the name you expect (it's usually Module.ClassName, for example). You may want to use the @objc(NameForObjC) syntax to set the name precisely the way you want it.

    – Rob Napier
    Jan 4 at 14:12






  • 1





    Yes, setting the @objc(ClassName) explicitly did work! Now the ValueTransformer is called correctly. I got some _SwiftStringStorage/NSData type casting problems now on the inside, but I guess I will manage that… :) Thanks again, Rob. That's been a great help!

    – Marc
    Jan 4 at 15:07












Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53450724%2fcoredata-valuetransformer-functions-are-not-called%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














You haven't overridden the correct methods of ValueTransformer. Your methods are:



func transformedValue(value: String?) -> NSData?
func reverseTransformedValue(value: NSData?) -> String?


The correct methods are:



func transformedValue(_ value: Any?) -> Any?
func reverseTransformedValue(_ value: Any?) -> Any?


The big hint that you're implementing the wrong methods is that you didn't need to add the override keyword.



BTW, this expression:



encoding: String.Encoding(rawValue: String.Encoding.utf8.rawValue))!


can be replaced with:



encoding: .uf8


It would also likely be better to replace your return "nil" with return nil; it's a String?, so it can be nil if things go wrong.






share|improve this answer


























  • Thanks rob and sorry for the late reply. Your hint with the missing override statement was very helpful and instructive! The utf8-part was not final but it's corrected now. However, the correct ValueTransformer-methods still won't kick in. breakpoints within those two functions aren't triggered and I'm completely clueless on which part I should have a look at… Do you have an additional hint what could be missing?

    – Marc
    Jan 4 at 12:34








  • 1





    I believe you still need to mark EncryptedStringTransformer as @objc, even though it's a subclass of ValueTransformer. A good way to explore that is to change the name of the transformer to something that you know doesn't exist, to see whether you get errors or not. Swift types do not always have exactly the name you expect (it's usually Module.ClassName, for example). You may want to use the @objc(NameForObjC) syntax to set the name precisely the way you want it.

    – Rob Napier
    Jan 4 at 14:12






  • 1





    Yes, setting the @objc(ClassName) explicitly did work! Now the ValueTransformer is called correctly. I got some _SwiftStringStorage/NSData type casting problems now on the inside, but I guess I will manage that… :) Thanks again, Rob. That's been a great help!

    – Marc
    Jan 4 at 15:07
















1














You haven't overridden the correct methods of ValueTransformer. Your methods are:



func transformedValue(value: String?) -> NSData?
func reverseTransformedValue(value: NSData?) -> String?


The correct methods are:



func transformedValue(_ value: Any?) -> Any?
func reverseTransformedValue(_ value: Any?) -> Any?


The big hint that you're implementing the wrong methods is that you didn't need to add the override keyword.



BTW, this expression:



encoding: String.Encoding(rawValue: String.Encoding.utf8.rawValue))!


can be replaced with:



encoding: .uf8


It would also likely be better to replace your return "nil" with return nil; it's a String?, so it can be nil if things go wrong.






share|improve this answer


























  • Thanks rob and sorry for the late reply. Your hint with the missing override statement was very helpful and instructive! The utf8-part was not final but it's corrected now. However, the correct ValueTransformer-methods still won't kick in. breakpoints within those two functions aren't triggered and I'm completely clueless on which part I should have a look at… Do you have an additional hint what could be missing?

    – Marc
    Jan 4 at 12:34








  • 1





    I believe you still need to mark EncryptedStringTransformer as @objc, even though it's a subclass of ValueTransformer. A good way to explore that is to change the name of the transformer to something that you know doesn't exist, to see whether you get errors or not. Swift types do not always have exactly the name you expect (it's usually Module.ClassName, for example). You may want to use the @objc(NameForObjC) syntax to set the name precisely the way you want it.

    – Rob Napier
    Jan 4 at 14:12






  • 1





    Yes, setting the @objc(ClassName) explicitly did work! Now the ValueTransformer is called correctly. I got some _SwiftStringStorage/NSData type casting problems now on the inside, but I guess I will manage that… :) Thanks again, Rob. That's been a great help!

    – Marc
    Jan 4 at 15:07














1












1








1







You haven't overridden the correct methods of ValueTransformer. Your methods are:



func transformedValue(value: String?) -> NSData?
func reverseTransformedValue(value: NSData?) -> String?


The correct methods are:



func transformedValue(_ value: Any?) -> Any?
func reverseTransformedValue(_ value: Any?) -> Any?


The big hint that you're implementing the wrong methods is that you didn't need to add the override keyword.



BTW, this expression:



encoding: String.Encoding(rawValue: String.Encoding.utf8.rawValue))!


can be replaced with:



encoding: .uf8


It would also likely be better to replace your return "nil" with return nil; it's a String?, so it can be nil if things go wrong.






share|improve this answer















You haven't overridden the correct methods of ValueTransformer. Your methods are:



func transformedValue(value: String?) -> NSData?
func reverseTransformedValue(value: NSData?) -> String?


The correct methods are:



func transformedValue(_ value: Any?) -> Any?
func reverseTransformedValue(_ value: Any?) -> Any?


The big hint that you're implementing the wrong methods is that you didn't need to add the override keyword.



BTW, this expression:



encoding: String.Encoding(rawValue: String.Encoding.utf8.rawValue))!


can be replaced with:



encoding: .uf8


It would also likely be better to replace your return "nil" with return nil; it's a String?, so it can be nil if things go wrong.







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 28 '18 at 14:12

























answered Nov 28 '18 at 14:06









Rob NapierRob Napier

207k28306434




207k28306434













  • Thanks rob and sorry for the late reply. Your hint with the missing override statement was very helpful and instructive! The utf8-part was not final but it's corrected now. However, the correct ValueTransformer-methods still won't kick in. breakpoints within those two functions aren't triggered and I'm completely clueless on which part I should have a look at… Do you have an additional hint what could be missing?

    – Marc
    Jan 4 at 12:34








  • 1





    I believe you still need to mark EncryptedStringTransformer as @objc, even though it's a subclass of ValueTransformer. A good way to explore that is to change the name of the transformer to something that you know doesn't exist, to see whether you get errors or not. Swift types do not always have exactly the name you expect (it's usually Module.ClassName, for example). You may want to use the @objc(NameForObjC) syntax to set the name precisely the way you want it.

    – Rob Napier
    Jan 4 at 14:12






  • 1





    Yes, setting the @objc(ClassName) explicitly did work! Now the ValueTransformer is called correctly. I got some _SwiftStringStorage/NSData type casting problems now on the inside, but I guess I will manage that… :) Thanks again, Rob. That's been a great help!

    – Marc
    Jan 4 at 15:07



















  • Thanks rob and sorry for the late reply. Your hint with the missing override statement was very helpful and instructive! The utf8-part was not final but it's corrected now. However, the correct ValueTransformer-methods still won't kick in. breakpoints within those two functions aren't triggered and I'm completely clueless on which part I should have a look at… Do you have an additional hint what could be missing?

    – Marc
    Jan 4 at 12:34








  • 1





    I believe you still need to mark EncryptedStringTransformer as @objc, even though it's a subclass of ValueTransformer. A good way to explore that is to change the name of the transformer to something that you know doesn't exist, to see whether you get errors or not. Swift types do not always have exactly the name you expect (it's usually Module.ClassName, for example). You may want to use the @objc(NameForObjC) syntax to set the name precisely the way you want it.

    – Rob Napier
    Jan 4 at 14:12






  • 1





    Yes, setting the @objc(ClassName) explicitly did work! Now the ValueTransformer is called correctly. I got some _SwiftStringStorage/NSData type casting problems now on the inside, but I guess I will manage that… :) Thanks again, Rob. That's been a great help!

    – Marc
    Jan 4 at 15:07

















Thanks rob and sorry for the late reply. Your hint with the missing override statement was very helpful and instructive! The utf8-part was not final but it's corrected now. However, the correct ValueTransformer-methods still won't kick in. breakpoints within those two functions aren't triggered and I'm completely clueless on which part I should have a look at… Do you have an additional hint what could be missing?

– Marc
Jan 4 at 12:34







Thanks rob and sorry for the late reply. Your hint with the missing override statement was very helpful and instructive! The utf8-part was not final but it's corrected now. However, the correct ValueTransformer-methods still won't kick in. breakpoints within those two functions aren't triggered and I'm completely clueless on which part I should have a look at… Do you have an additional hint what could be missing?

– Marc
Jan 4 at 12:34






1




1





I believe you still need to mark EncryptedStringTransformer as @objc, even though it's a subclass of ValueTransformer. A good way to explore that is to change the name of the transformer to something that you know doesn't exist, to see whether you get errors or not. Swift types do not always have exactly the name you expect (it's usually Module.ClassName, for example). You may want to use the @objc(NameForObjC) syntax to set the name precisely the way you want it.

– Rob Napier
Jan 4 at 14:12





I believe you still need to mark EncryptedStringTransformer as @objc, even though it's a subclass of ValueTransformer. A good way to explore that is to change the name of the transformer to something that you know doesn't exist, to see whether you get errors or not. Swift types do not always have exactly the name you expect (it's usually Module.ClassName, for example). You may want to use the @objc(NameForObjC) syntax to set the name precisely the way you want it.

– Rob Napier
Jan 4 at 14:12




1




1





Yes, setting the @objc(ClassName) explicitly did work! Now the ValueTransformer is called correctly. I got some _SwiftStringStorage/NSData type casting problems now on the inside, but I guess I will manage that… :) Thanks again, Rob. That's been a great help!

– Marc
Jan 4 at 15:07





Yes, setting the @objc(ClassName) explicitly did work! Now the ValueTransformer is called correctly. I got some _SwiftStringStorage/NSData type casting problems now on the inside, but I guess I will manage that… :) Thanks again, Rob. That's been a great help!

– Marc
Jan 4 at 15:07




















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53450724%2fcoredata-valuetransformer-functions-are-not-called%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







這個網誌中的熱門文章

Xamarin.form Move up view when keyboard appear

Post-Redirect-Get with Spring WebFlux and Thymeleaf

Anylogic : not able to use stopDelay()