Managed Object URI's not working?

  • Greeetings -

    I'm trying to store a list of Managed Object IDs separately from my
    data store.  Since IDs only become valid after a save, I catch them
    using the saved objects notification:

    - (void) managedObjectsSaved: (NSNotification *) theNotification {

    [[theNotification object] lock];
    @synchronized (objectIDs) {
      NSArray *theIDs = [[theNotification userInfo] valueForKey:
    NSInsertedObjectsKey];
      NSManagedObjectID *anID;
      for ( NSManagedObject *anItem in theIDs )  {
      anID = [anItem objectID];

      if ( [anID isTemporaryID] )
        NSLog( @"problem" );
      else
        [objectIDs addObject: [[anID URIRepresentation] absoluteString]];
      }

    Nothing gets logged, the Array fills up with objects that appear
    proper (<string>x-coredata://7F300951-22DD-4C55-8F1B-C533BA0B8336/
    ItemWrapper/p17</string>
    ), and it saves to a file nicely.  Everything looks good.

    Problems arise when i try to use these to use these to grab objects.
    The URL appears just as valid, but the Persistent Store Coordinator
    can never find a valid corresponding ID.

    for ( loopCounter = 0; loopCounter < theLimit; loopCounter++ ) {

      objectURL = [NSURL URLWithString: [theIDArray objectAtIndex:
    loopCounter]];
      theObjID = [persistentStoreCoordinator
    managedObjectIDForURIRepresentation: objectURL];
      if ( theObjID != nil )
      [theIDArray replaceObjectAtIndex: loopCounter withObject:
    [persistentStoreCoordinator managedObjectIDForURIRepresentation:
    objectURL]];
      else
      NSLog ( [objectURL description] ) ;
    }

    Any advice on what I'm doing wrong would be extremely welcome.

    Cheers,

    John
  • John,

    You can do something like the following to confirm that the object ID
    you have comes from a store that the persistent store coordinator
    knows about.

    {
    NSArray *knownStores = [persistentStoreCoordinator persistentStores];
    BOOL objectIDStoreIsKnown = [knownStores containsObject: [objectURL
    persistentStore]];
    NSAssert( objectIDStoreIsKnown, @"...or I'll know the reason why.");
    }

    - Bill

    > Problems arise when i try to use these to use these to grab objects.
    > The URL appears just as valid, but the Persistent Store Coordinator
    > can never find a valid corresponding ID.
    >
    >
    > for ( loopCounter = 0; loopCounter < theLimit; loopCounter++ ) {
    >
    > objectURL = [NSURL URLWithString: [theIDArray objectAtIndex:
    > loopCounter]];
    > theObjID = [persistentStoreCoordinator
    > managedObjectIDForURIRepresentation: objectURL];
    > if ( theObjID != nil )
    > [theIDArray replaceObjectAtIndex: loopCounter withObject:
    > [persistentStoreCoordinator managedObjectIDForURIRepresentation:
    > objectURL]];
    > else
    > NSLog ( [objectURL description] ) ;
    > }
  • Thanks, that helped me identify the problem - it seems that the store
    ID is changing in between when i store the IDs and when i retrieve
    them from a separate app.  When i check the store identifier, it's
    24A24C62-1B60-44C6-9602-0F3011B5C71C

    In contrast, the previously stored object URIs seem to have a
    completely different store ID, even though it's the same exact file.
    Printing description of objectURL:
    <CFURL 0x10339de20 [0x7fff7020b7c0]>{type = 15, string = x-coredata://
    868BBB38-4830-45FA-98DF-21357091C402/ItemWrapper/p7445, base = (null)}

    So, that at least gives me a better hint of what's going wrong.

    Cheers,

    John

    On Dec 2, 2007, at 12:21 PM, Bill Garrison wrote:

    > John,
    >
    > You can do something like the following to confirm that the object
    > ID you have comes from a store that the persistent store coordinator
    > knows about.
    >
    > {
    > NSArray *knownStores = [persistentStoreCoordinator persistentStores];
    > BOOL objectIDStoreIsKnown = [knownStores containsObject: [objectURL
    > persistentStore]];
    > NSAssert( objectIDStoreIsKnown, @"...or I'll know the reason why.");
    > }
    >
    > - Bill
    >
    >> Problems arise when i try to use these to use these to grab
    >> objects. The URL appears just as valid, but the Persistent Store
    >> Coordinator can never find a valid corresponding ID.
    >>
    >>
    >> for ( loopCounter = 0; loopCounter < theLimit; loopCounter++ ) {
    >>
    >> objectURL = [NSURL URLWithString: [theIDArray objectAtIndex:
    >> loopCounter]];
    >> theObjID = [persistentStoreCoordinator
    >> managedObjectIDForURIRepresentation: objectURL];
    >> if ( theObjID != nil )
    >> [theIDArray replaceObjectAtIndex: loopCounter withObject:
    >> [persistentStoreCoordinator managedObjectIDForURIRepresentation:
    >> objectURL]];
    >> else
    >> NSLog ( [objectURL description] ) ;
    >> }

  • As a further follow up, i can grab the new store ID and use it to
    replace the store ID in the string representation of the URI.  The
    resulting URI seems to work beautifully, although this doesn't seem to
    be a safe, long-term solution.

    JT

    On Dec 2, 2007, at 1:15 PM, John R. Timmer wrote:

    > Thanks, that helped me identify the problem - it seems that the
    > store ID is changing in between when i store the IDs and when i
    > retrieve them from a separate app.  When i check the store
    > identifier, it's
    > 24A24C62-1B60-44C6-9602-0F3011B5C71C
    >
    > In contrast, the previously stored object URIs seem to have a
    > completely different store ID, even though it's the same exact file.
    > Printing description of objectURL:
    > <CFURL 0x10339de20 [0x7fff7020b7c0]>{type = 15, string = x-
    > coredata://868BBB38-4830-45FA-98DF-21357091C402/ItemWrapper/p7445,
    > base = (null)}
    >
    > So, that at least gives me a better hint of what's going wrong.
    >
    > Cheers,
    >
    > John
    >
    >
    >
    > On Dec 2, 2007, at 12:21 PM, Bill Garrison wrote:
    >
    >> John,
    >>
    >> You can do something like the following to confirm that the object
    >> ID you have comes from a store that the persistent store
    >> coordinator knows about.
    >>
    >> {
    >> NSArray *knownStores = [persistentStoreCoordinator persistentStores];
    >> BOOL objectIDStoreIsKnown = [knownStores containsObject: [objectURL
    >> persistentStore]];
    >> NSAssert( objectIDStoreIsKnown, @"...or I'll know the reason why.");
    >> }
    >>
    >> - Bill
    >>
    >>> Problems arise when i try to use these to use these to grab
    >>> objects. The URL appears just as valid, but the Persistent Store
    >>> Coordinator can never find a valid corresponding ID.
    >>>
    >>>
    >>> for ( loopCounter = 0; loopCounter < theLimit; loopCounter++ ) {
    >>>
    >>> objectURL = [NSURL URLWithString: [theIDArray objectAtIndex:
    >>> loopCounter]];
    >>> theObjID = [persistentStoreCoordinator
    >>> managedObjectIDForURIRepresentation: objectURL];
    >>> if ( theObjID != nil )
    >>> [theIDArray replaceObjectAtIndex: loopCounter withObject:
    >>> [persistentStoreCoordinator managedObjectIDForURIRepresentation:
    >>> objectURL]];
    >>> else
    >>> NSLog ( [objectURL description] ) ;
    >>> }


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