Core Data load document store without document for QuickLook plugin

  • I have a Core Data project that uses a NSPersistentDocument subclass,
    which works fine.  But I'm trying to write a QuickLook plugin for it,
    and want to open the data store directly.  When I do, it fails when
    trying to load the store.  Specifically, the -
    addPersistentStoreWithType::: call below fails:

        persistentStoreCoordinator = [[NSPersistentStoreCoordinator
    alloc] initWithManagedObjectModel:[self managedObjectModel]];

        NSPersistentStore *store = [persistentStoreCoordinator
    addPersistentStoreWithType:NSBinaryStoreType configuration:nil URL:url
    options:nil error:&error];

    It gives the error "The model configuration used to open the store is
    incompatible with the one that was used to create the store."  I'm
    using the same xcdatamodel file as for the main app.  Does
    NSPersistentDocument pass something for the configuration argument?

    Alternatively, would it be feasible to write another
    NSPersistentDocument subclass and use that to load the document
    manually in the QuickLook plugin?

    --

    David Sinclair, Dejal Systems, LLC - <dev...>
    Dejal blog - http://www.dejal.com/blog/
    Cocoa code - http://www.dejal.com/developer/
    Now on Twitter - http://twitter.com/dejal/
  • On Dec 21, 2007, at 11:32 AM, David Sinclair wrote:

    > It gives the error "The model configuration used to open the store
    > is incompatible with the one that was used to create the store."
    > I'm using the same xcdatamodel file as for the main app.
    >
    How did you get the model in the document-based app?
    In the app, are you linking against any frameworks that contain
    managed object models that might be merged in with the app's model and
    which would not be present in the importer?

    mmalc
  • On Dec 21, 2007, at 11:46:33, mmalc crawford wrote:

    > On Dec 21, 2007, at 11:32 AM, David Sinclair wrote:
    >
    >> It gives the error "The model configuration used to open the store
    >> is incompatible with the one that was used to create the store."
    >> I'm using the same xcdatamodel file as for the main app.
    >>
    > How did you get the model in the document-based app?

    I'm not sure I understand that question.  The model's .xcdatamodel
    file is part of the app, and the NSPersistentDocument loads it
    automatically.  I explicitly load it in the QuickLook plugin via:

          managedObjectModel = [[NSManagedObjectModel
    mergedModelFromBundles:nil] retain];

    In the document-based app, if I override -
    configurePersistentStoreCoordinatorForURL:ofType:modelConfiguration:storeOptions:error
    :, both modelConfiguration and storeOptions are nil.

    > In the app, are you linking against any frameworks that contain
    > managed object models that might be merged in with the app's model
    > and which would not be present in the importer?

    Not sure.  No non-Apple frameworks.  In the app, I link against
    Carbon, Cocoa, CoreFoundation, CoreServices, Foundation, AppKit,
    ExceptionHandling, SystemConfiguration, AddressBook, CoreData, and
    WebKit.  The plugin wouldn't use several of those.

    In case it helps, here's output from qlmanage -p, with some debug
    logging from my plugin:

    2007-12-21 12:08:40.239 qlmanage[1392:807] Testing quicklookd preview
    with files:
    (
        "Once upon a time4.narrative"
    )
    2007-12-21 12:08:40.246 qlmanage[1392:807] [QL] Succesfully activated
    built-in quicklookd sandbox profile
    2007-12-21 12:08:40.341 qlmanage[1392:5603] [QL] Preview test for Once
    upon a time4.narrative -- /Users/dejal/Development/Leopard/Narrator/
    Test documents/. Content type UTI: com.dejal.narrator.document
    2007-12-21 12:08:40.344 qlmanage[1392:6203] [QL] Registering
    <QLGenerator Built-in> for public.image
    <snip>
    2007-12-21 12:08:40.403 qlmanage[1392:6203] [QL] Registering
    <QLGenerator /Users/dejal/Development/Leopard/Narrator/build/Release/
    Narrator.app/Contents/Library/QuickLook/Narrator.qlgenerator> for
    com.dejal.narrator.document
    2007-12-21 12:08:40.405 qlmanage[1392:5f03] [QL] Loading Plug-in for
    <QLGenerator /Users/dejal/Development/Leopard/Narrator/build/Release/
    Narrator.app/Contents/Library/QuickLook/Narrator.qlgenerator>
    2007-12-21 12:08:40.432 qlmanage[1392:5f03] [QL] Previewing /Users/
    dejal/Development/Leopard/Narrator/Test documents/Once upon a
    time4.narrative. Content type UTI: com.dejal.narrator.document.
    Generator used: <QLGenerator /Users/dejal/Development/Leopard/Narrator/
    build/Release/Narrator.app/Contents/Library/QuickLook/
    Narrator.qlgenerator>
    2007-12-21 12:08:40.436 qlmanage[1392:5f03] model:
    (<NSManagedObjectModel: 0x17f110>) isEditable 1, entities {
    }, fetch request templates {
    }; configs: (
    )
    2007-12-21 12:08:40.457 qlmanage[1392:5f03] coordinator:
    <NSPersistentStoreCoordinator: 0x17ee30>; store = (null); error =
    Error Domain=NSCocoaErrorDomain Code=134020 UserInfo=0x1856f0 "The
    model configuration used to open the store is incompatible with the
    one that was used to create the store."
    2007-12-21 12:08:40.462 qlmanage[1392:5f03] url: file://localhost/Users/dejal/Development/Leopard/Narrator/Test%20documents/Once%20upon%20a%20time4.narrative
    ; context: <NSManagedObjectContext: 0x181540>
    2007-12-21 12:08:40.463 qlmanage[1392:5f03] [QL ERROR] Preview request
    generator for <QLPreviewRequest file://localhost/Users/dejal/Development/Leopard/Narrator/Test%20documents/Once%20upon%20a%20time4.narrative
    > raised 'executeFetchRequest:error: A fetch request must have an
    entity.'
    2007-12-21 12:08:40.469 qlmanage[1392:6307] [QL] Thumbnailing Once
    %20upon%20a%20time4.narrative -- /Users/dejal/Development/Leopard/
    Narrator/Test documents/ (512 x 512). Content type UTI:
    com.dejal.narrator.document. Generator used: <QLGenerator /Users/dejal/
    Development/Leopard/Narrator/build/Release/Narrator.app/Contents/
    Library/QuickLook/Narrator.qlgenerator>
    2007-12-21 12:08:40.550 qlmanage[1392:6307] [QL] Thumbnailing for Once
    %20upon%20a%20time4.narrative -- file://localhost/Users/dejal/Development/Leopard/Narrator/Test%20documents/
      done (No image created).

    --

    David Sinclair, Dejal Systems, LLC - <dev...>
    Dejal blog - http://www.dejal.com/blog/
    Cocoa code - http://www.dejal.com/developer/
    Now on Twitter - http://twitter.com/dejal/
  • On Dec 21, 2007, at 12:16 PM, David Sinclair wrote:

    > Not sure.  No non-Apple frameworks.  In the app, I link against
    > Carbon, Cocoa, CoreFoundation, CoreServices, Foundation, AppKit,
    > ExceptionHandling, SystemConfiguration, AddressBook, CoreData, and
    > WebKit.  The plugin wouldn't use several of those.
    >
    Which version of Mac OS X are you targeting?

    As a first step, override managedObjectModel:

    - (id)managedObjectModel
    {
        static id sharedModel = nil;
        if (sharedModel == nil)
        {
    NSString *modelPath = [[NSBundle mainBundle]
    pathForResource:@"MyDocument" ofType:@"mom"];
    NSURL *modelURL = [NSURL fileURLWithPath:modelPath];

            sharedModel = [[[NSManagedObjectModel alloc]
    initWithContentsOfURL:modelURL] autorelease];
        }
        return sharedModel;
    }
    (written in Mail).

    mmalc
  • On Dec 21, 2007, at 12:26:18, mmalc crawford wrote:

    >
    > On Dec 21, 2007, at 12:16 PM, David Sinclair wrote:
    >
    >> Not sure.  No non-Apple frameworks.  In the app, I link against
    >> Carbon, Cocoa, CoreFoundation, CoreServices, Foundation, AppKit,
    >> ExceptionHandling, SystemConfiguration, AddressBook, CoreData, and
    >> WebKit.  The plugin wouldn't use several of those.
    >>
    > Which version of Mac OS X are you targeting?

    Leopard.

    > As a first step, override managedObjectModel:
    >
    > - (id)managedObjectModel
    > {
    > static id sharedModel = nil;
    > if (sharedModel == nil)
    > {
    > NSString *modelPath = [[NSBundle mainBundle]
    > pathForResource:@"MyDocument" ofType:@"mom"];
    > NSURL *modelURL = [NSURL fileURLWithPath:modelPath];
    >
    > sharedModel = [[[NSManagedObjectModel alloc]
    > initWithContentsOfURL:modelURL] autorelease];
    > }
    > return sharedModel;
    > }
    > (written in Mail).

    After substituting [NSBundle bundleForClass:[self class]] for
    [NSBundle mainBundle], that worked.

    So based on that clue, I changed my code to:

        managedObjectModel = [[NSManagedObjectModel
    mergedModelFromBundles:[NSArray arrayWithObject:[NSBundle
    bundleForClass:[self class]]]] retain];

    And that also worked.  The key was that the -mainBundle was /usr/bin
    (presumably the QuickLook engine), not the plugin, so using the
    correct bundle fixed it.

    Thank you!

    --

    David Sinclair, Dejal Systems, LLC - <dev...>
    Dejal blog - http://www.dejal.com/blog/
    Cocoa code - http://www.dejal.com/developer/
    Now on Twitter - http://twitter.com/dejal/
previous month december 2007 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