Only the first NSMutableArray is stored in NSUserDefaults

I am trying to keep a UILocalNotification queue to solve a constraint issue. I used this approach and it archives and expands my object, but only the first one.

How can I archive all objects from my NSMutableArray?

Code

// init/unarchive queue
if (self.queue == nil)
{
    // try loading stored array
    NSUserDefaults *currentDefaults = [NSUserDefaults standardUserDefaults];
    NSData *dataRepresentingSavedArray = [currentDefaults objectForKey:@"LocalNotificationQueue"];
    if (dataRepresentingSavedArray != nil) {
        NSArray *oldSavedArray = [NSKeyedUnarchiver unarchiveObjectWithData:dataRepresentingSavedArray];
        if (oldSavedArray != nil) {
            self.queue = [[NSMutableArray alloc] initWithArray:oldSavedArray];
        }
        else
        {
            self.queue = [[NSMutableArray alloc] init];
        }
    }
    else
    {
        self.queue = [[NSMutableArray alloc] init];
    }
}
// add
[self.queue addObject:notif];

// store queue
[[NSUserDefaults standardUserDefaults] setObject:[NSKeyedArchiver archivedDataWithRootObject:self.queue] forKey:@"LocalNotificationQueue"];

      

If I add items 1,2,3. Reboot and download. I only have 3.

Add 1,2,3. Reboot and download. I have 3, 1, 2.

If it matters. This is CDVPlugin Phonegap / Cordova.

+3


source to share


2 answers


After

[[NSUserDefaults standardUserDefaults] setObject:[NSKeyedArchiver archivedDataWithRootObject:self.queue] forKey:@"LocalNotificationQueue"];

      

You need to call



[[NSUserDefaults standardUserDefaults] synchronize]

      

To keep the default user settings.

+1


source


Try this, without NSKeyedArchiver

:



[[NSUserDefaults standardUserDefaults] setObject: self.queue] forKey:@"LocalNotificationQueue"];

      

0


source







All Articles