Data Protection and SwiftData Containers

SwiftData ModelContainer instances don't seem to have a value for setting the Data Protection class.

Is the best way to set that by setting the Data Protection in the app capabilities? Is that the only way?

I have a need for log data that would be "Complete unless open" and user data that would be "Complete", but how do I change one of the containers data protection class?

Answered by DTS Engineer in 822821022

SwiftData ModelContainer instances don't seem to have a value for setting the Data Protection class.

You are right. SwiftData doesn't provide the API as of today. I’d suggest that you file a feedback report to voice your need – If you do so, please share your report ID here for folks to track.

Is the best way to set that by setting the Data Protection in the app capabilities? Is that the only way?

Setting the Data Protection in the app capabilities, or in the other word, adding the com.apple.developer.default-data-protection entitlement to your app, is probably not a good way, because it adds some complexity that is not well documented.

If your intent is to have your SwiftData store use the .complete data protection class, I'd suggest that you put the SwiftData store in a certain folder and then set the URL’s .fileProtectionKey. For how to use a custom store URL, see init(_:schema:url:allowsSave:cloudKitDatabase:); for how to change the file protection class for a URL, see Encrypting Your App’s Files.

Best,
——
Ziqiao Chen
 Worldwide Developer Relations.

Accepted Answer

SwiftData ModelContainer instances don't seem to have a value for setting the Data Protection class.

You are right. SwiftData doesn't provide the API as of today. I’d suggest that you file a feedback report to voice your need – If you do so, please share your report ID here for folks to track.

Is the best way to set that by setting the Data Protection in the app capabilities? Is that the only way?

Setting the Data Protection in the app capabilities, or in the other word, adding the com.apple.developer.default-data-protection entitlement to your app, is probably not a good way, because it adds some complexity that is not well documented.

If your intent is to have your SwiftData store use the .complete data protection class, I'd suggest that you put the SwiftData store in a certain folder and then set the URL’s .fileProtectionKey. For how to use a custom store URL, see init(_:schema:url:allowsSave:cloudKitDatabase:); for how to change the file protection class for a URL, see Encrypting Your App’s Files.

Best,
——
Ziqiao Chen
 Worldwide Developer Relations.

@cold_geek, Something weird happened with your post of your bug number. I can’t fix that, so this post is its replacement. Namely…

FB16416218

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

Data Protection and SwiftData Containers
 
 
Q