• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: singleton managed object / obtaining mo context SOLVED
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: singleton managed object / obtaining mo context SOLVED


  • Subject: Re: singleton managed object / obtaining mo context SOLVED
  • From: Daniel Child <email@hidden>
  • Date: Tue, 09 Sep 2008 17:31:24 -0400

As a followup to my own question, it seems that the context is not created until awakeFromNib.
If I wait until awakeFromNib, I can access it via:


[[NSApp delegate] managedObjectContext];

OR

[[[NSApplication sharedApplication] delegate] managedObjectContext];

and those return the same value. Clearly you don't want to alloc/init a new instance of the delegate class.

Regarding the singleton issue, I realized you need to grab the singleton from the persistent store and count it. Thus,

NSError *error;
NSManagedObjectContext *moc = [[NSApp delegate] managedObjectContext];
NSUInteger numLibrarians = [EntityUtilities countObjectsOfType: @"Librarian" inContext: moc];


if (numLibrarians == 0) {
librarian = [NSEntityDescription insertNewObjectForEntityForName: @"Librarian"
inManagedObjectContext: moc]; // moc becomes nil here !!!
etc.


enables me to determine how many librarians I have (once I set up such a utility).

Thanks for looking.

On Sep 9, 2008, at 3:11 PM, Daniel Child wrote:

Hi All,

I am trying to implement a singleton class which is a subclass of NSManagedObject, but it is not behaving as expected. The librarian singleton is supposed to keep a list of the data sources, which are added periodically. I'm trying to cache the Librarian as an instance variable of my App Controller so that I can access it easily. The app controller tries to create/retrieve the instance during its own init as follows:

<in AppController>
- (id)init
{
  if (self = [super init])
  {
    // a bunch of variables initialized here
    theLibrarian = [Librarian getLibrarianInstance];
  }
  return self;
}

What's totally weird is that my managed object context disappears midway through the following lines of code.

<in Librarian.m>
static Librarian *librarian = nil;
@implementation Librarian
@dynamic dataSources;
@dynamic numSources;

+ (Librarian *) getLibrarianInstance {
NSError *error;
NSManagedObjectContext *moc = [[NSApp delegate] managedObjectContext]; // moc exists at this point
if (librarian == nil) {
librarian = [NSEntityDescription insertNewObjectForEntityForName: @"Librarian"
inManagedObjectContext: moc]; // moc becomes nil here !!!
[moc insertObject: librarian];
if (![moc save: &error]) {
[[NSApplication sharedApplication] presentError:error];
}
}
// [moc insertObject: librarian];
return librarian;
}


I have two basic questions. Where is the best place to initialize a singleton for a Core Data application?

And secondly, what is the best way to access the managed object context. In my init, if I call

moc = [[NSApp delegate] managedObjectContext]; // value is nil
moc = [[[NSApplication sharedApplication] delegate] managedObjectContext]; // value is nil
coreDataDelegate = [[CoreDataLibrarian_AppDelegate alloc] init];
moc = [coreDataDelegate managedObjectContext]; // value exists


Surely you aren't supposed to alloc/init the AppDelegate class everywhere in your app just to get the context. What is the best way for accessing it throughout your code? (I notice most examples write //assume context exists.....)

Thanks in advance.

Daniel





_______________________________________________

Cocoa-dev mailing list (email@hidden)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden


References: 
 >singleton managed object / obtaining mo context (From: Daniel Child <email@hidden>)

  • Prev by Date: Re: While we're on the subject of DMG's for software distribution...
  • Next by Date: Core Data and Garbage Collection
  • Previous by thread: singleton managed object / obtaining mo context
  • Next by thread: simple window close question
  • Index(es):
    • Date
    • Thread