I am having trouble fetching results with a to-many relationship and would like to ask for advice.

I have two NSManagedObject subclasses: Owner and Book. There is a one- to-many relationship between Owner and Book (an Owner can be associated with many Books).

--------
@interface Owner :  NSManagedObject {
}

@property (nonatomic, retain) NSString * ownerId;
@property (nonatomic, retain) NSString * displayName;
@property (nonatomic, retain) NSSet* Book;

@interface Owner (CoreDataGeneratedAccessors)
- (void)addBookObject:(Book *)value;
- (void)removeBookObject:(Book *)value;
- (void)addBook:(NSSet *)value;
- (void)removeBook:(NSSet *)value;
--------

--------
@interface Book :  NSManagedObject  {
}

@property (nonatomic, retain) NSNumber * type;
@property (nonatomic, retain) NSString * name;
@property (nonatomic, retain) NSDate * creationDate;
@property (nonatomic, retain) Owner * Owner;
--------

I first create the Owner managed object instance:

Owner *ownerMO = (Owner *)[NSEntityDescription insertNewObjectForEntityForName:@"Owner" inManagedObjectContext:moc];
[ownerMO setOwnerId:[libraryOwner _id]];
[ownerMO setDisplayName:[libraryOwner _displayName]];
NSError *error;
[moc save:&error];

I then loop through my Books and add them:

for (LibraryBook *libraryBook in libraryBooks) {
Book *bookMO = (Book *)[NSEntityDescription insertNewObjectForEntityForName:@"Book" inManagedObjectContext:managedObjectContext];
        [bookMO setName:[libraryBook _name]];
        [bookMO setCreationDate:[libraryBook _creationDate]];
        [ownerMO addBucketObject:bookMO];
        NSError *error;
        [moc save:&error];
}

So far, so good (I think).

Let's say, in my example, I populate the store with one Owner:

Owner < 123456 | username >

and multiple Books, some of which have names that start with "myPrefix".

When I go to search the Owner entities for Books that start with "myPrefix", I get an error message when I try to look at the results:

NSArray *testFetchResults = [managedObjectContext fetchObjectsForEntityName:@"Owner" withPredicate:[NSString stringWithFormat:@"ANY Book.name like 'myPrefix*'"]];

NSLog(@"testFetchResults - \n%d...@\n%@", [testFetchResults count], [testFetchResults description], [(Book *)[(Owner *)[testFetchResults objectAtIndex:0] Book] description]);

Here's the error:

Relationship fault for (<NSRelationshipDescription: 0x3d1b2b0>), name Book, isOptional 1, isTransient 0, entity Owner, renamingIdentifier Book, validation predicates (
), warnings (
), versionHashModifier (null), destination entity Book, inverseRelationship Owner, minCount 0, maxCount 0 on Owner < 123456 | username >

Is the following NSPredicate incorrect for doing a to-many lookup:

@"ANY Book.name like 'myPrefix*'"     

Or did I go about adding new Book and Owner managed objects incorrectly, in that the relationship between Book objects and Owner was not established correctly?

Sorry if this is a noob Core Data question. Thanks for your advice.

Regards,
Alex
_______________________________________________

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