0

I have tried different ways available but could not get a proper solution. I have a singleton array to which arrays of key-value pairs are being saved in my HomeVC. This array is being saved in NSUserDefaults and retrieved in another Class RewardsVC. But once the app is killed and relaunched , all the data in singleton array gets erased. Any help on this? Here is the code:

if ([json objectForKey:JSON_KEY_REWARDS]) 
{
    if([[json objectForKey:JSON_KEY_REWARDS] objectForKey:JSON_KEY_REWARD_ARRAY]){
        NSMutableArray *newRewardArray=[[json objectForKey:JSON_KEY_REWARDS] objectForKey:JSON_KEY_REWARD_ARRAY];
        if([newRewardArray count] > 0){
            shouldUpdate = TRUE;
        }
        for (NSMutableDictionary *reward in newRewardArray)
        {
            NSMutableDictionary *mutableReward = reward ;
            [[Utility getSharedInstance].rewardsFuckingArray addObject:mutableReward];
        }
    }
    [[NSUserDefaults standardUserDefaults] setObject:[Utility getSharedInstance].rewardsFuckingArray forKey:@"yourKey"];
    [[NSUserDefaults standardUserDefaults] synchronize];
    }

Utility is my Singleton class and I did @property (strong, nonatomic) NSMutableArray *rewardsFuckingArray; +(Utility*) getSharedInstance;in Utility.h. In Utility.m:

+(Utility*) getSharedInstance
{
    static Utility* _shared = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        _shared = [[self alloc] init];
    });
    return _shared;
}

- (id)init {
    if (self = [super init]) {
        self.rewardsFuckingArray = [[NSMutableArray alloc] init];
    }

    return self;
}

I RewardsVC.m, I did this:

rewardsArray = [[[NSUserDefaults standardUserDefaults] arrayForKey:@"yourKey"] mutableCopy];
ThomasCle
  • 6,792
  • 7
  • 41
  • 81
Vamshi Krishna
  • 979
  • 9
  • 19

1 Answers1

0

You need to load the data from NSUserDefaults, when the app have been killed. You could do this when you construct your singleton instance of Utility, by implementing your -(id)init method inside your Utility.m file like this:

- (id)init
{
    if (self = [super init])
    {
        NSArray* rewards = [[NSUserDefaults standardUserDefaults] objectForKey:@"yourKey"];

        if (rewards != nil)
        {
             self.rewardsFuckingArray = [NSMutableArray arrayWithArray:rewards];
        }
        else
        {
             self.rewardsFuckingArray = [[NSMutableArray alloc] init];
        }
    }

    return self;
}
ThomasCle
  • 6,792
  • 7
  • 41
  • 81
  • That depends on how your code is structured. If the `Utility` class is responsible for the array, I would load it as soon as the `Utility` class is constructed (I take it that you have `[[Utility alloc] init]` in some way inside your `sharedInstance` method of `Utility`, right?) – ThomasCle Aug 04 '15 at 07:19
  • The problem by doing it in `RewardsVC` is that maybe you are not guaranteed that `RewardsVC` is constructed at the point where you are trying to read the data, and thereby the array is not loaded. But then again, I don't know how your code is structured. Maybe you could calrify the question a bit and show us how you are loading data? :-) – ThomasCle Aug 04 '15 at 07:25
  • Do you have any other object types than `NSDictionary` in your array? You need to implement `NSCoding` on all custom classes you need to save. If your dictionaries contains ANY custom class objects in the structure you will get that error, unless you implement `NSCoding`. See this question for more information: http://stackoverflow.com/questions/2315948/how-to-store-custom-objects-in-nsuserdefaults – ThomasCle Aug 04 '15 at 08:04
  • Problem solved Thomas. Converted Array into NSData for saving and retrieving it. Thanks. – Vamshi Krishna Aug 04 '15 at 08:09