Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 01-11-2012, 03:41 AM   #1 (permalink)
Registered Member
 
Join Date: Jan 2009
Posts: 321
melmoup is on a distinguished road
Default Core Data DB Size

Hi,
what is the maximum size the sqlite db associated with Core Data should reach?
I'm thinking of implementing some house keeping to avoid it grows too much.
Thanks
melmoup is offline   Reply With Quote
Old 01-11-2012, 04:00 AM   #2 (permalink)
Senior Member
iPhone Dev SDK Supporter
 
Join Date: Jan 2010
Location: Issaquah, WA
Age: 42
Posts: 1,244
dljeffery is on a distinguished road
Default

No limit that I'm aware of. But don't store things like images or other binary objects in your databases. Instead, store those somewhere in the documents folder in the app, and just keep paths to them in your database. For both database size considerations, and also for performance when querying (and loading rows from) the database.
__________________
Recall It! Tag your notes. Tag your photos. Tag your thoughts. Tag your life.

Recall It! for iPad

http://www.dljeffery.com
dljeffery is offline   Reply With Quote
Old 01-11-2012, 04:08 AM   #3 (permalink)
Registered Member
 
Join Date: Jan 2009
Posts: 321
melmoup is on a distinguished road
Default

Quote:
Originally Posted by dljeffery View Post
No limit that I'm aware of. But don't store things like images or other binary objects in your databases. Instead, store those somewhere in the documents folder in the app, and just keep paths to them in your database. For both database size considerations, and also for performance when querying (and loading rows from) the database.
Thanks for replying, I read that for images with size around Kb was ok.
I'm also wondering why not may talk about house keeping, ok the size is unlimited but if I'm using my app to cache images I guess at some point I don't want to fill all device storage.
Does it have to be implemented as a cache with last date access for a certain item, and clean if too old?

Besides, I'm trying to make sure the writing on storage is asyncronous, is ther a common pattern for writing on storage (or on Core Data)?
Thanks.
melmoup is offline   Reply With Quote
Old 01-11-2012, 03:30 PM   #4 (permalink)
Senior Member
iPhone Dev SDK Supporter
 
Join Date: Jan 2010
Location: Issaquah, WA
Age: 42
Posts: 1,244
dljeffery is on a distinguished road
Default

My personal rule of thumb is, it's never appropriate to store BLOBs in your database. I mean it's not like you can query against them, right? So all they do is increase the size of the database, the time it takes to retrieve a rowset from the database, and the size of the records in memory.

Of course, careful database design (moving BLOBs into their own table) can address two of these issues. But I think the better design decision would be to just keep the BLOBs on the filesystem instead (which the Core Data Programming Guide also recommends). Not only will your database scale much better, but deleting records will cause less fragmentation. It's easy to break out the BLOBs into filesystem objects. Just add an attribute to your entity of NSString type to hold the filename (likely just a bare name, or a partial subpath plus name, depending on your directory structure). And when you need to save a new image, generate a unique filename with CFUUIDCreate and CFUUIDCreateString.

As for managing your storage footprint, I suppose this depends on what your database (and app) are doing. There's no way to predict how much storage is too much. Are you just caching data that you don't mind throwing away at some point?
__________________
Recall It! Tag your notes. Tag your photos. Tag your thoughts. Tag your life.

Recall It! for iPad

http://www.dljeffery.com
dljeffery is offline   Reply With Quote
Old 01-11-2012, 03:44 PM   #5 (permalink)
Registered Member
 
Join Date: Jan 2009
Posts: 321
melmoup is on a distinguished road
Default

Quote:
Originally Posted by dljeffery View Post
My personal rule of thumb is, it's never appropriate to store BLOBs in your database. I mean it's not like you can query against them, right? So all they do is increase the size of the database, the time it takes to retrieve a rowset from the database, and the size of the records in memory.

Of course, careful database design (moving BLOBs into their own table) can address two of these issues. But I think the better design decision would be to just keep the BLOBs on the filesystem instead (which the Core Data Programming Guide also recommends). Not only will your database scale much better, but deleting records will cause less fragmentation. It's easy to break out the BLOBs into filesystem objects. Just add an attribute to your entity of NSString type to hold the filename (likely just a bare name, or a partial subpath plus name, depending on your directory structure). And when you need to save a new image, generate a unique filename with CFUUIDCreate and CFUUIDCreateString.

As for managing your storage footprint, I suppose this depends on what your database (and app) are doing. There's no way to predict how much storage is too much. Are you just caching data that you don't mind throwing away at some point?
Thanks for the detailed answer.
Yes I have blobs in their own table, it should work like a browser cache. So basically image url and binary data in a Transformable.
About the storage footprint, I just want to avoid it will grow too much, so delete less accessed images, probably same thing a browser would do in case of limited cache size, just wondering best practice there.
The big advantage to have blobs was to avoid having to sync the DB Table with the references and the filesystem, but I'm convinced it's not advised, even though I'm quite sure I read somewhere it was ok for small images (less than 1MB).

Maybe a little bit off-topic, but what if I want to save on db (and filesystem in case) from a separate Thread?
melmoup is offline   Reply With Quote
Old 01-11-2012, 03:52 PM   #6 (permalink)
Senior Member
iPhone Dev SDK Supporter
 
Join Date: Jan 2010
Location: Issaquah, WA
Age: 42
Posts: 1,244
dljeffery is on a distinguished road
Default

Syncing the record and the filesystem objects is actually easy; all you need to do is override prepareForDeletion in your NSManagedObject subclass, and inside there, delete your associated filesystem objects.

As for doing CoreData inserts from a separate thread, I've never done this. I believe, however, the answer is that it's totally fine as long as each thread has its own NSManagedObjectContext and also doesn't try to share NSManagedObjects from other threads.
__________________
Recall It! Tag your notes. Tag your photos. Tag your thoughts. Tag your life.

Recall It! for iPad

http://www.dljeffery.com
dljeffery is offline   Reply With Quote
Old 01-11-2012, 05:04 PM   #7 (permalink)
Emphasizing Fundamentals
 
BrianSlick's Avatar
 
Join Date: Jul 2009
Location: NoVA / DC Area
Age: 36
Posts: 7,990
BrianSlick has a spectacular aura about
Default

Quote:
Originally Posted by dljeffery View Post
all you need to do is override prepareForDeletion in your NSManagedObject subclass, and inside there, delete your associated filesystem objects.
Sonuva... I swear I looked at this before, and decided it could be triggered in a condition such that the item was being removed from a context, not necessarily the store. But rereading things now, I'm not sure why I arrived at that conclusion. I've been doing things The Hard Way.

So, thanks!
__________________
BriTer Ideas LLC - Professional iOS App Development. Available for hire.

SlickShopper 2 | Free NSLog utility | Leave a PayPal donation.

Are you a newbie? Things you should read:
Definitive Guide To Properties | UITableView Series | Guide To Troubleshooting | Model Object Overview

Do you sit at a desk all day? Walk instead! Follow along with my treadmill desk adventures.
BrianSlick is offline   Reply With Quote
Old 01-11-2012, 05:13 PM   #8 (permalink)
Senior Member
iPhone Dev SDK Supporter
 
Join Date: Jan 2010
Location: Issaquah, WA
Age: 42
Posts: 1,244
dljeffery is on a distinguished road
Default

Quote:
Originally Posted by BrianSlick View Post
Sonuva... I swear I looked at this before, and decided it could be triggered in a condition such that the item was being removed from a context, not necessarily the store. But rereading things now, I'm not sure why I arrived at that conclusion. I've been doing things The Hard Way.

So, thanks!
LOL... isn't that the way it goes, sometimes?

I've felt completely comfortable with Core Data for quite a while now, but I wouldn't be surprised to find that there are things about it I'm also doing the hard way.

And to my recollection, I've never posted a dev question on these forums, but I sure have picked up a lot of good info here over the last few years, just from reading the threads!
__________________
Recall It! Tag your notes. Tag your photos. Tag your thoughts. Tag your life.

Recall It! for iPad

http://www.dljeffery.com
dljeffery is offline   Reply With Quote
Old 01-13-2012, 08:36 AM   #9 (permalink)
Registered Member
 
Join Date: Jan 2009
Posts: 321
melmoup is on a distinguished road
Default

Maybe I'm off-topic but I wanted to ask best approach for using Core Data as a cache considering multi-threading.
For instance I will have a page with images from the web, so I'm retrieving them by their URLs, using NSOperation I can do that asynchronously but I want to check first on my DB and write on it (or disk) the retrieved image for future use.
What can be the best way to do that without blocking the UI (like when scrolling)?
I read something about multi thread access on DB but still not sure best way to do that for reading/writing in conjunction with async network download.
Thanks for any tips on that.
melmoup is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 390
11 members and 379 guests
7twenty7, Atatator, buggen, guusleijsten, j.b.rajesh@gmail.com, morterbaher, QuantumDoja, sacha1996, Sami Gh, tim0504, VinceYuan
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,674
Threads: 94,122
Posts: 402,907
Top Poster: BrianSlick (7,990)
Welcome to our newest member, Atatator
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 05:37 AM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0