3

I am trying to store a queue of UILocalNotification to solve the limit problem. I used this approach and it does archive and unarchive my object but only the first one.

How do 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. Restart and load. I have only 3.

Add 1,2,3. Restart and load. I have 3, 1, 2.

If it matters. This is a Phonegap/Cordova CDVPlugin.

Community
  • 1
  • 1
PiTheNumber
  • 22,828
  • 17
  • 107
  • 180

2 Answers2

1

After

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

You need to call

[[NSUserDefaults standardUserDefaults] synchronize]

To save the user defaults.

PiTheNumber
  • 22,828
  • 17
  • 107
  • 180
Kasper Welner
  • 545
  • 3
  • 11
  • After I added `@synchronized(self)` around the whole function and called `synchronise` it works as wanted. Thank you and everybody! – PiTheNumber Feb 06 '13 at 14:57
  • @synchronized (self) {} makes the code inside the brackets thread-safe. Calling "synchronize" on NSUserDefaults is just saving. They might as well have called the function "save" instead of "synchronize". So the two has nothing to do with each other. – Kasper Welner Feb 06 '13 at 15:36
0

Try this, without the NSKeyedArchiver:

[[NSUserDefaults standardUserDefaults] setObject: self.queue] forKey:@"LocalNotificationQueue"];
Oscar Gomez
  • 18,436
  • 13
  • 85
  • 118
  • 1
    I might be blind but... how would this help? – Daij-Djan Feb 06 '13 at 14:24
  • NSUserDefaults can only store NSData, NSString, NSNumber, NSDate, NSArray, or NSDictionary, but not an array of UILocalNotification. – Martin R Feb 06 '13 at 14:30
  • This does not work. But it looks like `@synchronize(self)` does help. While he still loses one of two added items sometimes... – PiTheNumber Feb 06 '13 at 14:30
  • @Daij-Djan I think the way the NSKeyedArchiver is archiving the NSArray may have something to do with the issue. – Oscar Gomez Feb 06 '13 at 14:31
  • @MartinR is right, NSUserDefaults can only store those types of data, I would suggest trying an array of perhaps a JSON NSString or something you can use to recreate your UILocalNotification after reading back from it. – Oscar Gomez Feb 06 '13 at 14:33
  • 1
    @OscarGomez thats what the archiver was for! it makes data, no? – Daij-Djan Feb 06 '13 at 14:43