JDx
JDx

Reputation: 2655

Compress sqlite data, NSString or NSData?

Im using sqlite files to store data and a lot of the data I store is NSString format, if i store this data as NSData will it make the size of my sqlite files smaller? Is there anything else i can do to decrease sqlite file size?

Thanks.

Upvotes: 0

Views: 1063

Answers (3)

ImHuntingWabbits
ImHuntingWabbits

Reputation: 3827

As others have stated SQLite / CoreData can't really help you in this endeavor. You've already pointed out the correct solution, which is to use NSData to store raw bytes in the SQLite store and do the in-memory conversion yourself.

This will be exceptionally slow as compression is inherently expensive.

Here's what you need to do:

  1. Set your attribute type to NSData
  2. Check out libz and add it to your project in Xcode
  3. Choose a compression flavor from libz
  4. Implement custom properties to do the translation

    - (NSString *)getAttrString {
         NSData *bytes = self.attr
         //decompress
    }
    
    - (void)setAttrString:(NSString *)s {
        NSData *d = //compress
        self.attr = d;
    }
    

As others have mentioned there isn't really any benefit to compressing small strings, so you'll probably want to add a boolean to your model that indicates whether or not the data was actually compressed.

Upvotes: 2

Tom Harrington
Tom Harrington

Reputation: 70976

Prajwal provided some good general tips for optimizing Core Data storage. To answer your specific question though: No, converting from NSString to NSData will not save any space and may actually use more space. NSData does not do any compression. To convert to NSData you'd want something like

NSData *myData = [myString dataUsingEncoding:NSUTF8StringEncoding];

But that won't help and might make things worse. For example, if your string includes an "é", NSString represents it on one byte. But UTF-8 requires two bytes, so the NSData uses more space.

If you really want to compress the strings, there are open source implementations that will give you compressed NSData objects. Don't do that unless you have a lot of string data which you know will compress. It might save space, but (a) it will do so at the expense of speed, and (b) you won't be able to use those strings to look up objects in your data store.

If you still need to reduce the size of your data store, ask another question where you describe your data model. Different techniques apply depending on what your data model looks like.

Upvotes: 2

Prajwal Udupa
Prajwal Udupa

Reputation: 860

you can do this : save image link instead of image. If there is a large description then you can fetch it through a server. Remove multiple data. ie; duplicate data Remove the data that can be generated during runtime using the data in the database. Perform normalization to optimise the database which will avoid and clean up the mess if present.

Upvotes: 2

Related Questions