Core Data and NSFetchRequest

  • I've been working through the Core Data tutorials and documentation on
    the Apple site, and they've been very useful.  However, I'm stuck at
    the moment on using NSFetchRequest.

    I've used XCode to design a managed object model and and using Cocoa
    Bindings I can very easily create lots of arrays of managed objects,
    and have managed objects referencing each other.

    However, I now want to access the objects created programmatically.
    Through the default "many items" interface and bindings, I have
    created a set of managed objects of type Task, which save and restore
    to/from the persistent store no problem.  What I now want to do is
    access those objects, and I think I can get a handle on them by
    'fetching' them.

    Reading through the NSPersistentDocumentTutorial, it shows how to use
    NSFetchRequest, including the following code snippet:

    NSEntityDescription *entity = [NSEntityDescription entityForName:@"Department"
                inManagedObjectContext:moc];
        [fetchRequest setEntity:entity];
        fetchResults = [moc executeFetchRequest:fetchRequest error:&fetchError];

    but this seems to assume you have previously inserted the object using
    something like:

    NSEntityDescription insertNewObjectForEntityForName:@"Department"
                    inManagedObjectContext:managedObjectContext

    When I invoke

      NSEntityDescription *entity = [NSEntityDescription entityForName:@"Task"
          inManagedObjectContext:moc];

    I get

    "+entityForName: could not locate an NSManagedObjectModel"

    Do I need to do something to load the managed object model into the
    document - even though it seems to already been in use as the bound
    interface is working fine.

    Thanks

    Marcus
  • On May 11, 2005, at 2:28 PM, Marcus Roberts wrote:
    > When I invoke
    > NSEntityDescription *entity = [NSEntityDescription
    > entityForName:@"Task"
    > inManagedObjectContext:moc];
    > I get
    > "+entityForName: could not locate an NSManagedObjectModel"
    >
    Either 'moc' is nil or you've created the managed object context
    without setting up its persistent store coordinator.

    mmalc
  • On 5/11/05, mmalcolm crawford <mmalc_lists...> wrote:

    >> I get
    >> "+entityForName: could not locate an NSManagedObjectModel"
    >>
    > Either 'moc' is nil or you've created the managed object context
    > without setting up its persistent store coordinator.

    Thank you!  'moc' was nil - I was trying to access the managed object
    context too early, as I reference it through the document, and I was
    calling it in the window controller constructor, before the contrller
    knew what its document was.  It just goes to show, check the simple
    things first, but I have learnt alot about the managed object model in
    the process anyway.  Thanks mmalc!

    Marcus
previous month may 2005 next month
MTWTFSS
            1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31          
Go to today