biased_estimator
biased_estimator

Reputation: 733

NSUserDefaults not getting preference values from the preferences cache?

I'm working with NSDefaults and trying to read the list of finder favorites in the finder. This code returns that list in a command line utility:

#import <Foundation/Foundation.h>
int main(int argc, const char * argv[]) {
    @autoreleasepool {
        NSUserDefaults *userDefaults = [[NSUserDefaults alloc] init];
        [userDefaults addSuiteNamed:@"com.apple.sidebarlists"];

        NSDictionary *list = [userDefaults dictionaryForKey:@"favoriteitems"];
        NSLog(@"%@", list);  
        }
    return 0;
}

Unfortunately, the returned list seems to be stale.

For example, if I delete an entry from the finder favorites list via the finder and then rerun my little bit of code, the deleted entry remains in the list. This appears to be true until the system syncs the UserDefaults cache with the disk (I can force this by restarting the computer, for example).

Is there a way to force cocoa to read from the preferences cache and not the disk as it appears to be doing?

Upvotes: 0

Views: 122

Answers (2)

Jon Shier
Jon Shier

Reputation: 12770

You can force synchronization with the synchronize method. Whether that will fix your issue i don't know.

Upvotes: 0

Ken Thomases
Ken Thomases

Reputation: 90531

Don't do it this way. There's a supported API for this, although it's only documented in the headers. See the LSSharedFileList API in /System/Library/Frameworks/CoreServices.framework/Frameworks/LaunchServices.framework/Headers/LSSharedFileList.h. You probably want the list with the identifier kLSSharedFileListFavoriteItems.

Upvotes: 3

Related Questions