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 11-21-2011, 09:53 AM   #1 (permalink)
Registered Member
 
Join Date: Oct 2010
Posts: 117
subharb is on a distinguished road
Default Can't overwrite custom Saved Object in NSuserDefaults

Hello guys,

Im willing to overwrite a custom object in NSUserDefault, it's a Matrix.
It works fine the first time I create the object and save it, but it never updates.

here's the code:
The modification to the Matrix Class
Code:
- (void)encodeWithCoder:(NSCoder *)encoder
{
	//Encode properties, other class variables, etc
    [encoder encodeObject:self.matriz forKey:@"matriz"];
}
- (id)initWithCoder:(NSCoder *)decoder
{
    self = [super init];
    if( self != nil )
    {
		//decode properties, other class vars
        self.matriz = [decoder decodeObjectForKey:@"matriz"];
    }
    return self;
}
The methods that I use in a ViewController to set up a Matrix Object named "horarios"
I want to create a new Matrix if it's the very first time I execute this ViewController or then load the previous one.
Code:
-(void)viewDidLoad(){
	if([self loadCustomObjectWithKey:@"horarios"] == nil){
		//Inicializar el horarios a 0, que es lo que vale al inicio
		self.horarios = [[Matriz alloc]init:(int)3 column:5];		
	}
	else{
		self.horarios = [self loadCustomObjectWithKey:@"horarios"];		
	}
This is the code that saves and loads the customObject from NSUserDefaults in the ViewController

Code:
-(void)saveCustomObject:(Matriz*)obj
{
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
	NSData *myEncodedObject = [NSKeyedArchiver archivedDataWithRootObject:obj];
	
	//[obj showValue];
	
    [defaults setObject:myEncodedObject forKey:@"horarios"];
}

-(Matriz*)loadCustomObjectWithKey:(NSString*)key{
	
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    NSData *myEncodedObject = [defaults objectForKey: key];
    Matriz* obj = (Matriz*)[NSKeyedUnarchiver unarchiveObjectWithData: myEncodedObject];
	
	return obj;
}
The code works fine the first time, when in NSUserDefaults there's no custom object, when I save it saves the current status of the matrix, but the next times, when there already an object if always loads the previous one, as if it could only save once the object.

I dont understand it, any ideas?
subharb is offline   Reply With Quote
Old 11-21-2011, 10:00 AM   #2 (permalink)
Reading the Documentation
 
baja_yu's Avatar
 
Join Date: Sep 2010
Location: 45.255019,19.844908
Posts: 5,414
baja_yu has a spectacular aura about
Default

You aren't synchronizing defaults after you call setObject.
baja_yu is offline   Reply With Quote
Old 11-21-2011, 10:09 AM   #3 (permalink)
Registered Member
 
Join Date: Oct 2010
Posts: 117
subharb is on a distinguished road
Default

Quote:
Originally Posted by baja_yu View Post
You aren't synchronizing defaults after you call setObject.
Thanks, but how can I do that?
subharb is offline   Reply With Quote
Old 11-21-2011, 10:18 AM   #4 (permalink)
Reading the Documentation
 
baja_yu's Avatar
 
Join Date: Sep 2010
Location: 45.255019,19.844908
Posts: 5,414
baja_yu has a spectacular aura about
Default

By calling 'synchronize'
baja_yu is offline   Reply With Quote
Old 11-21-2011, 10:36 AM   #5 (permalink)
Registered Member
 
Join Date: Oct 2010
Posts: 117
subharb is on a distinguished road
Default

Quote:
Originally Posted by baja_yu View Post
By calling 'synchronize'
Thanks, that is

Code:
[[NSUserDefaults standardUserDefaults] synchronize];
subharb is offline   Reply With Quote
Old 11-21-2011, 11:34 AM   #6 (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 subharb View Post
Hello guys,

Im willing to overwrite a custom object in NSUserDefault, it's a Matrix.
It works fine the first time I create the object and save it, but it never updates.

here's the code:
The modification to the Matrix Class
Code:
- (void)encodeWithCoder:(NSCoder *)encoder
{
	//Encode properties, other class variables, etc
    [encoder encodeObject:self.matriz forKey:@"matriz"];
}
- (id)initWithCoder:(NSCoder *)decoder
{
    self = [super init];
    if( self != nil )
    {
		//decode properties, other class vars
        self.matriz = [decoder decodeObjectForKey:@"matriz"];
    }
    return self;
}
The methods that I use in a ViewController to set up a Matrix Object named "horarios"
I want to create a new Matrix if it's the very first time I execute this ViewController or then load the previous one.
Code:
-(void)viewDidLoad(){
	if([self loadCustomObjectWithKey:@"horarios"] == nil){
		//Inicializar el horarios a 0, que es lo que vale al inicio
		self.horarios = [[Matriz alloc]init:(int)3 column:5];		
	}
	else{
		self.horarios = [self loadCustomObjectWithKey:@"horarios"];		
	}
This is the code that saves and loads the customObject from NSUserDefaults in the ViewController

Code:
-(void)saveCustomObject:(Matriz*)obj
{
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
	NSData *myEncodedObject = [NSKeyedArchiver archivedDataWithRootObject:obj];
	
	//[obj showValue];
	
    [defaults setObject:myEncodedObject forKey:@"horarios"];
}

-(Matriz*)loadCustomObjectWithKey:(NSString*)key{
	
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    NSData *myEncodedObject = [defaults objectForKey: key];
    Matriz* obj = (Matriz*)[NSKeyedUnarchiver unarchiveObjectWithData: myEncodedObject];
	
	return obj;
}
The code works fine the first time, when in NSUserDefaults there's no custom object, when I save it saves the current status of the matrix, but the next times, when there already an object if always loads the previous one, as if it could only save once the object.

I dont understand it, any ideas?

Your encodeWithCoder method that you posted is in your Matriz class? It's trying to encode an a property "matriz" of the object. What kind of object is a "matriz"? Unless it's an object type that conforms to NSCoding, that call will fail, and not encode anything. When you try to read back an object of your Matriz class, you are going to get a nil for your matrix property.
"matriz" property.
__________________
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 online now   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: 392
14 members and 378 guests
7twenty7, chiataytuday, Clouds, dedeys78, Duncan C, e2applets, EvilElf, iekei, ipodphone, jeroenkeij, leostc, mbadegree, Murphy, QuantumDoja
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,676
Threads: 94,125
Posts: 402,910
Top Poster: BrianSlick (7,990)
Welcome to our newest member, jleannex55
Powered by vBadvanced CMPS v3.1.0

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