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 05-28-2011, 10:18 AM   #1 (permalink)
jcc
Registered Member
 
Join Date: Aug 2010
Posts: 53
jcc is on a distinguished road
Default NSCoder Memory Management

Hi,

I am using NSCoder to save various images, text, and drawings supplied by the user and saving it into NSUserDefaults like so....


[code]
- (void)encodeWithCoderNSCoder *)coder {

}


- (id)initWithCoderNSCoder *)coder {
self = [super initWithCoder:coder];
return self;
}


- (void) saveCurrentSetup {

NSUserDefaults * defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:[NSKeyedArchiver archivedDataWithRootObject:imageView] forKey:@"image_View"];
.....etc.

}
[code]

Now I am starting to second guess the idea of saving these objects to NSUserDefaults because it's quite a high memory load.

There are only certain views that actually use the saved objects. So I don't really need them taking up memory all the time. Is there a way to manage these different objects so that I can call them into memory only when they are needed?

Thanks.
jcc is offline   Reply With Quote
Old 05-28-2011, 10:37 AM   #2 (permalink)
Cocoa Junkie
 
Duncan C's Avatar
 
Join Date: Dec 2008
Location: Northern Virginia
Posts: 6,003
Duncan C has a spectacular aura about
Default

Quote:
Originally Posted by jcc View Post
Hi,

I am using NSCoder to save various images, text, and drawings supplied by the user and saving it into NSUserDefaults like so....


[code]
- (void)encodeWithCoderNSCoder *)coder {

}


- (id)initWithCoderNSCoder *)coder {
self = [super initWithCoder:coder];
return self;
}


- (void) saveCurrentSetup {

NSUserDefaults * defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:[NSKeyedArchiver archivedDataWithRootObject: imageView] forKey:@"image_View"];
.....etc.

}
[code]

Now I am starting to second guess the idea of saving these objects to NSUserDefaults because it's quite a high memory load.

There are only certain views that actually use the saved objects. So I don't really need them taking up memory all the time. Is there a way to manage these different objects so that I can call them into memory only when they are needed?

Thanks.
First:

Your class isn't really implementing the NSCoding protocol if your initWithCoder and encodeWithCoder methods simply call super. You need to write code in those methods that encodes/decodes the properties and iVars that you need in order to reconstruct your object.

Now, you have a method saveSetup that uses the NSKeyedArchiver class method archivedDataWithRootObject to convert an image view to a data object.

Why do you want to encode the image view? Do you want to be able to load the view from user defaults and add it as a subview of some view controller?

I would think it would make more sense to encode the image that's inside the image view, and then write (perhaps in your view controller's viewDidLoad method) that loads the image and installs it into an image view that was loaded by your nib file.

Saving images to user defaults should be ok. I don't think user defaults loads everything into memory at once. As far as I know, it only loads objects into memory when you ask for them, so your memory footprint should be reasonable.

As far as only loading your images when you need them, sure. That's a good idea. However, only you know when you need them, since you're the one writing your program.

If they are images that are displayed in views in view controllers, only load the images when the view controller's viewDidLoad method is called, and simply install the images in their image views. Then, if there is a low memory alert, the system will unload views that aren't in use and the images will be released from memory.
__________________
Regards,

Duncan C
WareTo

Check out our apps in the Apple App store


Check out this password generator app that shows various techniques including using a data container singleton object to share data between objects in your project.

See this tutorial on using UIView animations and layer animations:

See this thread on generating random, non-repeating text

Check out a very cool Macintosh Kaleidoscopes app called ScopeWorks that we released to the Mac App store.
Duncan C is offline   Reply With Quote
Old 05-29-2011, 08:13 PM   #3 (permalink)
Registered Member
 
Join Date: Feb 2010
Posts: 75
matt62king is on a distinguished road
Default

See my bog on NSCoding and NSCoder. Matt King's iOS Dev Blog: Saving Objects with NSCoder and NSCoding
__________________
My Twitter -- @matt62king
matt62king is offline   Reply With Quote
Reply

Bookmarks

Tags
nscoder, nsuserdefaults

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: 340
6 members and 334 guests
Dnnake, dre, iOS.Lover, jenniead38, Kirkout, Wikiboo
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,663
Threads: 94,120
Posts: 402,898
Top Poster: BrianSlick (7,990)
Welcome to our newest member, LezB44
Powered by vBadvanced CMPS v3.1.0

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