Go Back   MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old Jul 6, 2006, 06:45 AM   #1
MrFusion
macrumors 6502a
 
Join Date: Jun 2005
Location: West-Europe
relationship predicate

What is missing? I think I do everything according to the manual.
Say I have entity A en B with a relationship REL (to many) from A to B but without an inverse one. B has a bool value of selected.
The user can select any B for each A and when this happens I observe the
NSManagedObjectContextObjectsDidChangeNotification
and do some stuff I want to do. For this I need all the A's that has at least one selected B.

My solution to this is:
Code:
- (NSArray *) filterDatabase:(NSPredicate *) myPredicate forEntity:(NSString *)myEntity
{
	NSFetchRequest *request = [[[NSFetchRequest alloc] init] autorelease];
	NSEntityDescription *entity = [NSEntityDescription entityForName:myEntity
											  inManagedObjectContext:context];
	[request setEntity:entity];
	[request setPredicate:myPredicate];
	NSError *error = nil;
	NSArray *array = [context executeFetchRequest:request error:&error];
	if (error)
		NSLog(@"%@",error);
	return array;
}

================

	NSPredicate *selRec = [NSPredicate predicateWithFormat:@"ANY REL.selected == 1"];
	NSArray *result = [self filterDatabase:selRec
								 forEntity:@"A"];
	NSLog(@"r %i",[result count]);
		
	NSPredicate *selRec3 = [NSPredicate predicateWithFormat:@"selected == 1"];
	NSArray *result3 = [self filterDatabase:selRec3
								  forEntity:@"B"];
	NSLog(@"r3 %i",[result3 count]);
Every time something is selected, result3 is updated (as I can see with the NSLog), but the first 'result' doesn't give the right answer.

So, besides it being spaghetticode, what's wrong with it?

Thanks
MrFusion is offline   0 Reply With Quote
Old Jul 6, 2006, 04:53 PM   #2
MrFusion
Thread Starter
macrumors 6502a
 
Join Date: Jun 2005
Location: West-Europe
The code above was right it seems and gave the correct answers. What was displayed was incorrect. When I load the database all instances of B are displayed at first until the selection of instance A is changed.
arraycontroller 1 is bound to A, and arraycontroller 2 is bound to the selection of A for the relationship from A to B.
MrFusion is offline   0 Reply With Quote

Reply
MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Similar Threads
thread Thread Starter Forum Replies Last Post
Apple should make a method that returns an array's object that satisfies a predicate. moonman239 iPhone/iPad Programming 5 Nov 21, 2013 07:21 PM
Core Data fetch with Predicate cpuin Mac Programming 5 Sep 8, 2013 04:44 PM
[SOLVED] IB - Bindings Inspector - Search Predicate KieferThomas Mac Programming 2 May 13, 2013 11:05 AM
Core Data, predicate using Key Path Fuzej iPhone/iPad Programming 5 Jul 25, 2012 06:23 AM
Relationship issue ct2k7 Wasteland 26 Jun 3, 2012 05:16 PM

Forum Jump

All times are GMT -5. The time now is 09:54 PM.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps

Mobile Version | Fixed | Fluid | Fluid HD
Copyright 2002-2013, MacRumors.com, LLC