Should iOS app's plist be compressed for optimization purposes?
The app I have under development is utilizing a lot of plist that开发者_JS百科's taking up a lot of space. I am considering zipping up the plist files. At runtime, the app will unzip them into NSData and deserialize them into NSDictionary using NSPropertyListSerialization for eventual use.
Is there anything risky about this approach? Or is it useless if Property List Output Encoding is set to binary.
If you are getting to the point where you're considering zipping up property lists to save on space, I think it's time to move to a different format. Property lists are good for storing a few defaults or some settings, but they're not a replacement for a database.
My recommendation would be to look at moving this data into a Core Data database. If you are trying to embed large digital files as elements in the property list, look at storing those as separate resources in your application bundle or in your application's /Documents directory.
Core Data will allow you to lazy-load items as you need them, saving on load times and memory, and its SQLite base will provide for fast read / write times.
If your project settings are properly set up (that is, the Property List Output Encoding you mentioned is set to binary in the active target's settings), all of the .plist files will be converted to binary form. Look inside the bundle of your build product and check what the sizes of files are.
Update:
Further investigation shows that if you add a directory containing your plist files as a Folder Reference, Xcode will not look inside it, it will simply copy its contents into the produced bundle. Therefore, you should convert plists yourself using plutil.
A possible way do automate the task is to add a Build Script phase to your target in Xcode and make it recursively copy all the files from the directory A
to the directory B
converting all the files that end in .plist. Then add B
to your target as a reference instead of A
and you're good to go.
You could also simply create a new directory for binary plists and add them to your target instead of ordinary XML plists.
Here's how the script might look. Supposing all of your plists are stored in a directory named dir
(or any of its subdirectories) and it is located at path /path/to/dir
. To convert them all into directory /path/to/B
one could write the following:
#!/bin/bash
find "/path/to/dir" -iname '*.plist' | xargs -L 1 -J % cp % "/path/to/B"
find "/path/to/B" -iname '*.plist' | xargs -L 1 plutil -convert binary1
精彩评论