On 2009 Jun 21, at 09:55, Quincey Morris wrote:

First, you *seem* to be suggesting that uniquing is a process of winnowing away unwanted duplicate objects, but I'm pretty sure that Core Data doesn't create duplicate objects. Rather, it seems likely that it uses the object UID to find out if a needed object exists before ever creating it.

Oh, I agree with you on that. I thought it was rather strange to read an "-ing" suffix on "uniqueing", as though it was something that could "happen".

Second, "Fetch all Bars" isn't a scenario where spurious duplicate Bars could be created.

It looks like you're correct. I just tried to reproduce this in the Core Data Utility but found that it worked as expected -- setting the same Bar to two different Foos does not create an additional Bar [1].

Also, you could examine the UIDs of foo1, foo2 and bar before and after the save, as well as after the fetch. Unless you can see 2 different objects with the same UID, then it would seem that the unwanted Bar was actually created as a result of your code somewhere.

Good idea. If I don't reply back to this thread it means I found something stupid in my code.

Thanks, Quincey.

Jerry


[1]

#import <Foundation/Foundation.h>
#import <CoreData/CoreData.h>

// Note: This function returns a retained, not autoreleased, instance.
NSManagedObjectModel *getStaticManagedObjectModel() {
    static NSManagedObjectModel *mom = nil;

    if (mom != nil) {
        return mom;
    }

    mom = [[NSManagedObjectModel alloc] init];

NSEntityDescription *fooEntity = [[NSEntityDescription alloc] init];
    [fooEntity setName:@"Foo"];
    [fooEntity setManagedObjectClassName:@"Foo"];
NSEntityDescription *barEntity = [[NSEntityDescription alloc] init];
    [barEntity setName:@"Bar"];
    [barEntity setManagedObjectClassName:@"Bar"];

    // Create a to-one relationship
NSRelationshipDescription *fooToBar = [[NSRelationshipDescription alloc] init] ;
    [fooToBar setMaxCount:1] ;
    [fooToBar setName:@"bar"] ;

    // Create a to-many relationship
NSRelationshipDescription *barToFoo = [[NSRelationshipDescription alloc] init] ;
    [barToFoo setMaxCount:NSIntegerMax] ;
    [barToFoo setName:@"foos"] ;

    // Wire up the relationships
    [fooToBar setDestinationEntity:barEntity] ;
    [barToFoo setDestinationEntity:fooEntity] ;
    [fooToBar setInverseRelationship:barToFoo] ;
    [barToFoo setInverseRelationship:fooToBar] ;

    // Set relationships in entities
    [fooEntity setProperties:[NSArray arrayWithObject:fooToBar]] ;
    [barEntity setProperties:[NSArray arrayWithObject:barToFoo]] ;
    [fooToBar release] ;
    [barToFoo release] ;

    // Set entities in mom
    [mom setEntities:[NSArray arrayWithObjects:
                      fooEntity,
                      barEntity,
                      nil]];

    [fooEntity release] ;
    [barEntity release] ;

    return mom;
}

// Note: This function returns a retained, not autoreleased, instance.
NSManagedObjectContext *getStaticManagedObjectContext() {

    static NSManagedObjectContext *moc = nil;

    if (moc != nil) {
        return moc;
    }

    moc = [[NSManagedObjectContext alloc] init];

    NSPersistentStoreCoordinator *coordinator =
    [[NSPersistentStoreCoordinator alloc]
     initWithManagedObjectModel: getStaticManagedObjectModel()];
    [moc setPersistentStoreCoordinator: coordinator];
    [coordinator release] ;

    NSError *error;
    NSPersistentStore *newStore ;
newStore = [coordinator addPersistentStoreWithType:NSInMemoryStoreType
                                         configuration:nil
                                                   URL:nil
                                               options:nil
                                                 error:&error];

    if (newStore == nil) {
        NSLog(@"Store Configuration Failure\n%@",
              ([error localizedDescription] != nil) ?
              [error localizedDescription] : @"Unknown Error");
    }
    return moc;
}


int main (int argc, const char * argv[])
{
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];

    // Create Core Data stack
    NSManagedObjectModel *mom = getStaticManagedObjectModel() ;
    NSManagedObjectContext *moc = getStaticManagedObjectContext() ;

    // Create two Foos and one Bar
NSManagedObject* foo1 = [NSEntityDescription insertNewObjectForEntityForName:@"Foo" inManagedObjectContext:moc] ; NSManagedObject* foo2 = [NSEntityDescription insertNewObjectForEntityForName:@"Foo" inManagedObjectContext:moc] ; NSManagedObject* bar = [NSEntityDescription insertNewObjectForEntityForName:@"Bar" inManagedObjectContext:moc] ;

    // Set the single bar to be the Bar of each Foo
    [foo1 setValue:bar
           forKey:@"bar"] ;
    [foo2 setValue:bar
            forKey:@"bar"] ;

    NSFetchRequest* fetchRequest ;
    NSArray* fetches ;

    // Fetch and log all the Foos.
    fetchRequest= [[NSFetchRequest alloc] init];
    [fetchRequest setEntity:[NSEntityDescription entityForName:@"Foo"
                                        inManagedObjectContext:moc]];
    fetches = [moc executeFetchRequest:fetchRequest
                                 error:NULL] ;
    NSLog(@"Fetched %d Foos: %@", [fetches count], fetches) ;
    [fetchRequest release] ;

    // Fetch and log all the Bars.
    fetchRequest= [[NSFetchRequest alloc] init];
    [fetchRequest setEntity:[NSEntityDescription entityForName:@"Bar"
                                        inManagedObjectContext:moc]];
    fetches = [moc executeFetchRequest:fetchRequest
                                 error:NULL] ;
    NSLog(@"Fetched %d Bars: %@", [fetches count], fetches) ;
    [fetchRequest release] ;

    [moc release] ;
    [mom release] ;
    [pool release] ;

    return 0;
}
_______________________________________________

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

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:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Reply via email to