Intermittently Calling [NSUserDefaults synchronize] Returns Success but isn't shown during ObjectForKey

| | August 7, 2015

I have 2 applications sharing data via the NSUserDefaults, initWithSuite ID. Using iOS Dev Center.

One in 2 attempts the following code block apparently fails to write my dictionary object to the shared UserDefault area, I use the BOOL response from Synchronise to check the success. eg, (4 NSData objects, one, or more, or all of the items might fail to write to storage).

I use [[NSUUID uuid] UUIDString] as a key [EDIT: Been told in comments that this changes value?, Tried a Static String same result, 25% fail rate]

Any help will be greatly received.

Writer – Application 1

NSUserDefaults *myDefaults = [[NSUserDefaults alloc]
                          initWithSuiteName:FRGFileShareID];

NSString *uuid = [[NSUUID UUID] UUIDString];

[myDefaults setObject:**customObjectDict** forKey:uuid];

BOOL successSave = [myDefaults synchronize];
if (!successSave) {
     DLog(@"Failed To Save To Defaults");
}

Reader – Application 2

NSUserDefaults *myDefaults = [[NSUserDefaults alloc] initWithSuiteName:FRGFileShareID];

if ([myDefaults objectForKey:uuid]) {
    [mediaArray addObject:[myDefaults objectForKey:uuid]];
    [myDefaults removeObjectForKey:uuid];
}

If i log out [[myDefaults dictionaryRepresentation] allKeys] im lucky if any of my keys are there.

Again with this working 75% of the time, im not sure whats causing the intermittency but its killing the usability of my feature.

One Response to “Intermittently Calling [NSUserDefaults synchronize] Returns Success but isn't shown during ObjectForKey”

  1. You have not pass the value to NSUserDefault, change the code like this,

    [myDefaults setObject:uuid forKey:"myDict Object"];
    [myDefaults synchronize];
    

Leave a Reply