Go Back   MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old Apr 10, 2013, 12:35 AM   #1
davidmirv
macrumors newbie
 
Join Date: Apr 2013
NSArrayController and "Shared User Defaults Controller"

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 by davidmirv; Apr 10, 2013 at 06:20 PM.
davidmirv is offline   0 Reply With Quote
Old Apr 10, 2013, 10:51 AM   #2
ArtOfWarfare
macrumors 603
 
ArtOfWarfare's Avatar
 
Join Date: Nov 2007
Send a message via Skype™ to ArtOfWarfare
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.)
ArtOfWarfare is offline   0 Reply With Quote
Old Apr 10, 2013, 11:27 AM   #3
davidmirv
Thread Starter
macrumors newbie
 
Join Date: Apr 2013
Quote:
Originally Posted by ArtOfWarfare View Post
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
Attached Files
File Type: zip timetracker.zip (680.2 KB, 18 views)
davidmirv is offline   0 Reply With Quote
Old Apr 10, 2013, 11:30 AM   #4
davidmirv
Thread Starter
macrumors newbie
 
Join Date: Apr 2013
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 is offline   0 Reply With Quote
Old Apr 10, 2013, 05:57 PM   #5
davidmirv
Thread Starter
macrumors newbie
 
Join Date: Apr 2013
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/...-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 is offline   0 Reply With Quote
Old Apr 10, 2013, 06:27 PM   #6
davidmirv
Thread Starter
macrumors newbie
 
Join Date: Apr 2013
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];
}
davidmirv is offline   0 Reply With Quote

Reply
MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Similar Threads
thread Thread Starter Forum Replies Last Post
Resolved: 13" MacBook Pro Retina not display "switch user" after sleep mtbdudex MacBook Pro 1 Mar 1, 2014 02:46 PM
"Another device is using this computer's IP address""Shared" user's slowing down Wifi mnya OS X 2 Feb 15, 2014 02:30 PM
How is the "use as defaults" option in Finder supposed to work? bostich OS X 10.8 Mountain Lion 3 Jul 25, 2013 01:53 PM
Is it possible for people on the "network" to read the files in the Shared folder ? XPcentric Mac Basics and Help 1 Jan 25, 2013 03:23 PM
What happens when you enter an incorrect "defaults write" command? bitfidelity OS X 2 Jul 28, 2012 05:44 PM

Forum Jump

All times are GMT -5. The time now is 12:53 AM.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps

Mobile Version | Fixed | Fluid | Fluid HD
Copyright 2002-2013, MacRumors.com, LLC