• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Core Data adding new instance - creating relationship to existing instance
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Core Data adding new instance - creating relationship to existing instance


  • Subject: Core Data adding new instance - creating relationship to existing instance
  • From: Amy Heavey <email@hidden>
  • Date: Fri, 19 Sep 2008 18:47:49 +0100

I'm trying to add new instances of some entities, I've got it adding new objects, and I can create new relationships to new related objects, but I can't work out how to connect a new instance to an existing instance of an entity,

eg

I have items with relationship to upcs and categories. I can add the new item, and set all the attributes, and new relationships to the new upcs that are also created, but I cannot create the new relationship to an existing category. The Category Entity has an attribute called name that I want to match to an NSString

I am assuming that I would actually set the relationship in the usual manner:

NSManagedObject *newItem = [NSEntityDescription
			insertNewObjectForEntityForName:@"Item"
					 inManagedObjectContext:moc];



NSPredicate *catFind = [NSPredicate predicateWithFormat:@"name like % @",categoryString];

NSFetchRequest *fetch=[[NSFetchRequest alloc] init];
[fetch setEntity:[NSEntityDescription entityForName:@"Category" inManagedObjectContext:moc]];
[fetch setPredicate:catFind];
NSArray *category = [moc executeFetchRequest:fetch error:nil];


[newItem setValue:[category objectAtIndex:0] forKey:@"Category"];

The problem is actually selecting the category, I have tried all manner of things, and I thought I needed a fetchRequest, but it just throws an error:

-[NSCFArray objectAtIndex:]: index (0) beyond bounds (0)

because there is nothing in the array?

I'm aiming at 10.4 and using XCode 2.4.1

Can anyone help me?

Many Thanks

Amy


_______________________________________________

Cocoa-dev mailing list (email@hidden)

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:
This email sent to email@hidden


  • Follow-Ups:
    • Re: Core Data adding new instance - creating relationship to existing instance
      • From: Jamie Hardt <email@hidden>
  • Prev by Date: Re: How to hide disclosure triangle in NSOutlineView [10.5]?
  • Next by Date: Re: Drawing an NSImage in a CALayer
  • Previous by thread: NSFetchRequest
  • Next by thread: Re: Core Data adding new instance - creating relationship to existing instance
  • Index(es):
    • Date
    • Thread