Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

davidmirv

macrumors newbie
Original poster
Apr 9, 2013
6
0
Hi all,
Long time lurker around here wondering if I could pop in for some advice & help here.

I have a NSPopupbutton that has its content & selected index (to change it) bound to an NSArrayController which is in turn has its managedObjectContext bound for Core Data access..
This all works great HOWEVER
I was looking for a way to simply and quickly save the NSPopupButtons Selecton without storing this is core data , SO I bound the NSArrayController's 'Selection Indexes' to "Shared User Defaults Controller" and set a model key path and also set the value transformer to NSKeyedUnarchiveFromData, and just for testing sake because I've never usd Shared User Defaults Controller (But have used NSUserDefaults in pure code w/o IB) I bound a couple text field values aswell just to make sure it was working.

Have done alot of debugging around and have looked in the application plist to verify there is a value for the storage key which appears to be there just fine however it WILL NOT restore this selectionindex when the app is reopened,.

Is anyone aware of a way to do this successfully? Would be greatly appreciated..
 
Last edited:

ArtOfWarfare

macrumors G3
Nov 26, 2007
9,557
6,057
I don't really like bindings... The concept behind using tools like it to not write code is that you won't have to debug them, but that clearly isn't the case as they're pretty easy to get wrong.

Having said that, I suggest zipping the project and uploading it so we can look at the bindings you hooked up ourselves, rather than trying to figure out what you did from your description. (A picture, as the saying goes, is worth 1000 words. In our case the picture is your zipped project.)
 

davidmirv

macrumors newbie
Original poster
Apr 9, 2013
6
0
I don't really like bindings... The concept behind using tools like it to not write code is that you won't have to debug them, but that clearly isn't the case as they're pretty easy to get wrong.

Having said that, I suggest zipping the project and uploading it so we can look at the bindings you hooked up ourselves, rather than trying to figure out what you did from your description. (A picture, as the saying goes, is worth 1000 words. In our case the picture is your zipped project.)

Attached.. Have included the Core Data store from Library/Application Support/ since the initial data for the ArrayControllers/NSPopupButtons is fetched from a web service

Thanks!
David
 

Attachments

  • timetracker.zip
    680.2 KB · Views: 108

davidmirv

macrumors newbie
Original poster
Apr 9, 2013
6
0
Also FYI i have tried (This morning) doing this manually without bindings using NSUserDefaults NSKeyed(un)Archiver to store/retrieve the arrayControler SelectedIndexes.. for whatever reason their not restored that way either..
 

davidmirv

macrumors newbie
Original poster
Apr 9, 2013
6
0
after persuing posts all morning I have found that there is a timing issue with coredata->nsarraycontroller and using NSUserDefaults (http://www.cocoabuilder.com/archive/cocoa/292357-nsarraycontroller-kvo-core-data-question.html)
However I'm unable to find an execution point in my app when core data has loaded and populated the NSArrayController KVC observation doesn't work and the manual fetch as discusse in the apple documentation
Code:
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
    [_acClients addObserver:self forKeyPath:@"arrangedObjects" options:0 context:NULL];
    NSError *error;
    BOOL ok = [_acClients fetchWithRequest:nil merge:NO error:&error];
    if (!ok) {
        NSLog(@"Error While Fetching %@", error);
    }
}
doesn't work either ..My observer is never called and the fetchwithrequest always returns NO with a nil error object!

Completely lost here..
 

davidmirv

macrumors newbie
Original poster
Apr 9, 2013
6
0
OK So the bindings in this case ARE completely useless and my problem with the fetch and the KVC observation was that I had somehow lost the connection to my array controller from app delegate.. So once i fixed that things started happening..

FIX:
Code:
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
    [_acClients addObserver:self forKeyPath:@"arrangedObjects" options:0 context:NULL];
    NSError *error;
    BOOL ok = [_acClients fetchWithRequest:nil merge:NO error:&error];
    if (!ok) {
        NSLog(@"Error While Fetching %@", error);
    }
    NSMutableDictionary * options = [[NSMutableDictionary alloc] init];
    [options setObject:[NSNumber numberWithBool:YES]
                forKey:NSContinuouslyUpdatesValueBindingOption];
    [options setObject:NSKeyedUnarchiveFromDataTransformerName
                forKey:NSValueTransformerNameBindingOption];
    //Now that we have made sure the arrayController is populated bind the nsuserdefaults.. 
    [_acClients bind:@"selectionIndexes"
            toObject:[NSUserDefaultsController sharedUserDefaultsController]
         withKeyPath:@"values.aPopupSelection"
             options: options];
}
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.