Object initialized in "init" cannot be accessed in "viewDidLoad" - ios

I have a strange issue: I load object data in "init" method. When I try to access it in the "viewDidLoad" my app crashes. Here is the code:
#interface UploadCenterViewController () {
NSMutableArray *videos;
}
#end
#implementation UploadCenterViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
if (![self loadVideos]) {
[self saveVideos];
}
}
return self;
}
-(void)saveVideos {
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSData *encodedData = [NSKeyedArchiver archivedDataWithRootObject:videos];
[defaults setObject:encodedData forKey:#"VIDEOS"];
[defaults synchronize];
}
-(bool)loadVideos {
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSData *encodedData = [defaults objectForKey:#"VIDEOS"];
if (encodedData) {
videos = (NSMutableArray *)[NSKeyedUnarchiver unarchiveObjectWithData:encodedData];
NSLog(#"array size: %d", [videos count]);
return true;
} else {
videos = [[NSMutableArray alloc] init];
return false;
}
}
- (void)viewDidLoad
{
[super viewDidLoad];
NSLog(#"%d", [videos count]);
}
When loading the data, the "videos" array contains an object. When accessing it in the "viewDidLoad" the entire app crashes.
Does anyone has an idea?

Declare videos as an #property and use it as self.videos everywhere. The crash is due to the fact that videos is getting released once you assign a value to it. The scope of videos is only inside that method and it can crash due to this. Since you want to use this outside that method, you need to retain it and you can use #property for this as mentioned below.
for eg:-
#interface UploadCenterViewController () {}
#property(nonatomic, strong) NSMutableArray *videos;
#end
-(void)saveVideos {
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSData *encodedData = [NSKeyedArchiver archivedDataWithRootObject:self.videos];
[defaults setObject:encodedData forKey:#"VIDEOS"];
[defaults synchronize];
}
-(bool)loadVideos {
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSData *encodedData = [defaults objectForKey:#"VIDEOS"];
if (encodedData) {
self.videos = (NSMutableArray *)[NSKeyedUnarchiver unarchiveObjectWithData:encodedData];
NSLog(#"array size: %d", [self.videos count]);
return true;
} else {
self.videos = [[NSMutableArray alloc] init];
return false;
}
}
- (void)viewDidLoad
{
[super viewDidLoad];
NSLog(#"%d", [self.videos count]);
}

You might want to retain the result of [NSKeyedUnarchiver unarchiveObjectWithData:encodedData].
Read the memory management rules.

Related

store NSobject data into Nsuserdefaults

In My Modal Object two values are there
I'm trying to store in NSUserDefaults. This is My Code
interface File:
#interface collectionModel : NSObject <NSCoding> {
}
#property(nonatomic,retain) NSString *name;
#property(nonatomic,retain) NSString *author;
Implementation File:
#implementation collectionModel
#synthesize name = _name;
#synthesize author = _author;
- (id)initWithCoder:(NSCoder *)aDecoder {
if (self = [super init]) {
self.name = [aDecoder decodeObjectForKey:#"name"];
self.author = [aDecoder decodeObjectForKey:#"author"];
}
return self;
}
- (void)encodeWithCoder:(NSCoder *)aCoder {
[aCoder encodeObject:_name forKey:#"name"];
[aCoder encodeObject:_author forKey:#"author"];
}
ViewController.M
#import "collectionModel.h"
parsedCollectionArr = [[NSMutableArray alloc] init];
for (NSDictionary *obj in collectionBalk) {
NSString * Name = [obj objectForKey:#"Name"];
NSString * author = [obj objectForKey:#"author"];
collectionModel *dataObj = [[collectionModel alloc] init];
dataObj.name = Name;
dataObj.author = author;
[parsedCollectionArr addObject:dataObj];
}
NSLog(#"parsedCollectionArr count ---->>>> %d",23);
Here I want to store this parsedCollectionArr in NSUserDefaults and retrive from it.can any one help me.
try this..
Create a class like "CollectionModelManager.h and .m"
//For Save CollectionModel
+ (void)saveCollectionModel:(CollectionModel *) collectionModel
{
NSData *encodedData = [NSKeyedArchiver archivedDataWithRootObject:collectionModel];
NSUserDefaults *userDef = [NSUserDefaults standardUserDefaults];
[userDef setObject:encodedData forKey:kCollectionModelKey]; //here kCollectionModelKey is a static string
[userDef synchronize];
}
//For Get CollectionModel
+ (CollectionModel *)getCollectionModel
{
NSUserDefaults *userDef = [NSUserDefaults standardUserDefaults];
CollectionModel *collectionModel = nil;
NSData *encodedData = [userDef objectForKey:kCollectionModelKey]; //Same static string use here kCollectionModelKey
if (encodedData != nil) {
collectionModel = [NSKeyedUnarchiver unarchiveObjectWithData:encodedData];
}
return collectionModel;
}

Objective C - Saving a variable inside NSUserDefaults

I have a class called "EntityType" that has a string "name" and I am using NSUserDefaults to save it so I tried this:
if (button.selected)
{
[button setSelected:YES];
[[NSUserDefaults standardUserDefaults]setObject:[NSNumber numberWithBool:YES] forKey:#"buttonSelected"];
}
I want to access the variable from another class and save it inside NSUserDefaults
You have missed sync call.Apply this to save it.
if (button.selected)
{
[button setSelected:YES];
[[NSUserDefaults standardUserDefaults]setObject:[NSNumber numberWithBool:YES] forKey:#"buttonSelected"];
[[NSUserDefaults standardUserDefaults] synchronize];
}
to get it back later
NSNumber* savedValue = [[NSUserDefaults standardUserDefaults]
objectForKey:#"buttonSelected"];
This is if you want to store only the value.
If you want to store your custom object look at this link
How to store custom objects in NSUserDefaults
You can get NSNumber object like,
NSNumber *num = [[NSUserDefaults standardUserDefaults] objectForKey:#"buttonSelected"];
from anywhere in the app.
On your EntityType class, implement the following two methods for encoding and decoding (something relevant to your own object):
- (void)encodeWithCoder:(NSCoder *)encoder {
//Encode properties, other class variables, etc
[encoder encodeObject:self.name forKey:#"name"];
}
- (id)initWithCoder:(NSCoder *)decoder {
if((self = [super init])) {
//decode properties, other class vars
self.name = [decoder decodeObjectForKey:#"name"];
}
return self;
}
Reading and writing from NSUserDefaults:
- (void)saveCustomObject:(MyObject *)object key:(NSString *)key {
NSData *encodedObject = [NSKeyedArchiver archivedDataWithRootObject:object];
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:encodedObject forKey:key];
[defaults synchronize];
}
- (MyObject *)loadCustomObjectWithKey:(NSString *)key {
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSData *encodedObject = [defaults objectForKey:key];
MyObject *object = [NSKeyedUnarchiver unarchiveObjectWithData:encodedObject];
return object;
}
Code borrowed from: save class in NSUserDefaults
**ViewController.h**
#import <UIKit/UIKit.h>
#interface ViewController : UIViewController
//here i took one userdefault property
#property(strong,nonatomic)NSUserDefaults *user;
#end
**ViewController.m**
#import "ViewController.h"
#interface ViewController ()
#end
#implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
//after the property declaration take standard user defaults
_user = [NSUserDefaults standardUserDefaults];
//here i took Bool and key you can take any thing but took my_val
[_user setBool:YES forKey:#"my_val"];
}
// this is button action
- (IBAction)submit:(id)sender
{
//for bool i am checking the condition given above
if ([_user boolForKey:#"my_val"])
{
**enter code here**
NSLog(#"values is set to yes");
}
else
{
**enter code here**
NSLog(#"Value is set to No");
}
}

NSCoding save NSMutableArray on shutdown and load on startup

I am trying to save a NSMutableArray when the app shuts down, and then load it on startup to a tableview.
Here's my code:
- (void)encodeWithCoder:(NSCoder *)encoder {
[encoder encodeObject:_savedText forKey:#"savedText"];
}
- (id)initWithCoder:(NSCoder *)decoder {
if((self = [super initWithCoder:decoder])) {
_savedText = [decoder decodeObjectForKey:#"savedText"];
}
return self;
}
I use this code to save the MutableArray:
- (void)applicationDidEnterBackground:(UIApplication *)application {
NSData *data = [NSKeyedArchiver archivedDataWithRootObject:_savedText];
[[NSUserDefaults standardUserDefaults] setObject:data forKey:#"savedText"];
[[NSUserDefaults standardUserDefaults] synchronize];
}
And this to load it:
- (void)viewDidLoad {
NSData *savedData = [[NSUserDefaults standardUserDefaults] objectForKey:#"savedText"];
NSMutableArray *array = [NSKeyedUnarchiver unarchiveObjectWithData:savedData];
array = [_savedText copy];
}
There are no errors, but the table view is empty on startup. Have I forgotten something?
You should read the data from the defaults and set to _savedText object. You are reading the data into var named array and then assigning array = [_savedText copy]. This will probably be nil. It should be the reverse.
- (void)viewDidLoad {
NSData *savedData = [[NSUserDefaults standardUserDefaults] objectForKey:#"savedText"];
NSMutableArray *array = [NSKeyedUnarchiver unarchiveObjectWithData:savedData];
_savedText = [array copy];
}

Saving data with NSUserDetaults and load after phone restart wont work

I managed to save an NSMutableArray in NSUserDefaults by first converting it to NSData - I dont deal with a lot of data and just want the data to be there after I switch off & on my phone - but the data does not show up in my table where I would display it. I write the NSUserDefaults back to my array upon loading. Maybe one of you has a hint...? Below the button action where I write to NSUserDefaults and the method viewDidLoad where I write NSUserDefaults to my original array (toDoitems)
- (IBAction)unwindToList:(UIStoryboardSegue *)segue
{
XYZAddToDoItemViewController *source = [segue sourceViewController];
XYZToDoItem *item = source.toDoItem;
if (item !=nil) {
[self.toDoitems addObject:item];
NSString *error;
NSData *data = [NSPropertyListSerialization dataFromPropertyList:self.toDoitems format:NSPropertyListBinaryFormat_v1_0 errorDescription:&error];
[[NSUserDefaults standardUserDefaults] setObject:data forKey:#"itemArray"];
[[NSUserDefaults standardUserDefaults] synchronize];
[self.tableView reloadData];
}
}
- (void)viewDidLoad
{
[super viewDidLoad];
self.toDoitems = [[NSMutableArray alloc] init];
self.toDoitems = [NSMutableArray arrayWithArray:[[NSUserDefaults standardUserDefaults] objectForKey:#"itemArray"]];
}
Heres one way to do this
Add encoder decoder functions to your XYZToDoItem class
Something like this if say you had 2 strings in this class string1 and string2 :
(i havent compiled this code but you get the idea)
-(void)encodeWithCoder:(NSCoder *)aCoder
{
[aCoder encodeObject:self.string1 forKey:#"string1"];
[aCoder encodeObject:self.string2 forKey:#"string2"];
}
-(id)initWithCoder:(NSCoder *)aDecoder
{
self = [super init];
if (self)
{
self.string1 = [aDecoder decodeObjectForKey:#"string1"];
self.string2 = [aDecoder decodeObjectForKey:#"string2"];
}
return self;
}
Then when you are ready to save do the following
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
NSData *data = [NSKeyedArchiver archivedDataWithRootObject:self.myDataArray];
[userDefaults setObject:data forKey:#"storageName"];
// To load the data from NSUserDefaults
NSData *myData = [[NSUserDefaults standardUserDefaults] objectForKey:#"storageName"];
NSArray *temp = (NSMutableArray*)[NSKeyedUnarchiver unarchiveObjectWithData:myData];
self.myDataArray = (NSMutableArray*)[temp mutableCopy];

iOS - encoding/decoding enums - crash on accessing after relaunch

What am I doing wrong? Am I encoding/decoding the enum type properly?
GameSettings interface:
typedef enum {
CatWhite = 0,
CatBlack = 1,
CatOrange = 2
} CatColor;
...
CatColor catColor;
...
#property CatColor catColor;
GameSettings implementation:
#synthesize catColor;
...
+ (GameSettings*)GetInstance
{
if (sharedSingleton == nil) {
sharedSingleton = [[super allocWithZone:NULL] init];
sharedSingleton.catColor = CatWhite;
}
return sharedSingleton;
}
-(void)encodeWithCoder:(NSCoder *)coder {
[coder encodeInt:self.catColor forKey:#"CatColor"];
}
-(id)initWithCoder:(NSCoder *)coder {
if((self = [super init])) {
self.catColor = [coder decodeIntForKey:#"CatColor"];
}
NSLog(#"initWithCoder: %d", self.catColor); //this logs the correct int
return self;
}
AppDidFinishLaunching:
- (void)applicationDidFinishLaunching:(UIApplication *)application {
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSData *data = [defaults objectForKey:#"GameSettings"];
GameSettings *gGameData = [NSKeyedUnarchiver unarchiveObjectWithData:data];
if(gGameData != NULL) {
GameSettings *settings = [GameSettings GetInstance];
settings.catColor = gGameData.catColor;
}else {
GameSettings *settings = [GameSettings GetInstance];
settings.catColor = CatWhite;
}
...
}
Save settings:
GameSettings* settings = [GameSettings GetInstance];
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSData *data = [NSKeyedArchiver archivedDataWithRootObject:settings];
[defaults setObject:data forKey:#"GameSettings"];
[defaults synchronize];
The crash (Program received signal: “EXC_BAD_ACCESS”) comes when I re-launch the app and try to access the game settings:
GameSettings* settings = [GameSettings GetInstance];
NSLog(#"settings: %d", settings); //EXC_BAD_ACCESS
NSLog(#"catColor: %d", settings.catColor); //EXC_BAD_ACCESS
Why can't I access the GameSettings singleton after a re-launch?
Are you sure you want to use applicationDidFinishLaunching there? When you resurrect the program, it may be invoking awakeFromNib instead of applicationDidFinishLaunching (source: http://www.cimgf.com/2008/03/26/cocoa-tutorial-awakefromnib-vs-applicationdidfinishlaunching/ ).
Finally figured this out. GameSettings was getting deallocated because NSKeyedUnarchiver was autoreleasing it:
GameSettings *gGameData = [NSKeyedUnarchiver unarchiveObjectWithData:data];
Fix:
GameSettings *gGameData = [[NSKeyedUnarchiver unarchiveObjectWithData:data] retain];

Resources