2012-10-03 12 views
5

मेरे पास दो इकाइयां हैं। Employee इकाईकोर डेटा: एकाधिक इकाइयों या रिश्ते से परिणाम प्राप्त करें

@interface Employee : NSManagedObject 

@property (nonatomic, retain) NSString * dept; 
@property (nonatomic, retain) NSString * email; 
@property (nonatomic, retain) NSString * name; 
@property (nonatomic, retain) Department *deptEmp; 

@end 

और Department इकाई

@interface Department : NSManagedObject 

@property (nonatomic, retain) NSString * location; 
@property (nonatomic, retain) NSString * name; 
@property (nonatomic, retain) Employee *deptEmp1; 

मैं निम्नलिखित विधेय

NSMutableString *queryString = [NSMutableString stringWithFormat:@"(name = 'Apple') AND (deptEmp1.location like 'Cupertino')"]; 

NSEntityDescription *entityDescription = [NSEntityDescription entityForName:@"Employee" inManagedObjectContext:self.managedObjectContext]; 

के साथ दोनों से जानकारी लाने और अनुरोध लायें कोशिश कर रहा हूँ

NSFetchRequest *request = [[NSFetchRequest alloc] init]; 
[request setEntity:entityDescription]; 
[request setResultType:NSDictionaryResultType]; // NSFetchRequestResultType - NSDictionaryResultType 
[request setRelationshipKeyPathsForPrefetching:[NSArray arrayWithObjects:@"Department",nil]]; 
[request setIncludesSubentities:YES]; 
है

स्थापना विधेय

if(![queryString isEqualToString:@""]) 
{ 
     [request setPredicate:[NSPredicate predicateWithFormat:queryString]]; 
} 

NSError *error = nil; 
NSArray *returnArray = nil; 

ला रहा है परिणाम

@synchronized(self) 
{ 
    returnArray = [self.managedObjectContext executeFetchRequest:request error:&error]; 
} 

लेकिन यहाँ मैं कभी नहीं परिणाम मिलता है।

+0

तुम क्या आप प्राप्त करना चाहते हैं समझा सकते हैं? धन्यवाद। –

+0

मैं नाम प्राप्त करना चाहता हूं, पहली इकाई से हटा सकता हूं और दूसरी स्थिति से पूर्वानुमान स्थिति के साथ स्थान प्राप्त करना चाहता हूं। – kamleshwar

उत्तर

3

मुझे यकीन है कि आप क्या हासिल करना चाहते नहीं कर रहा हूँ, लेकिन अगर आप एक Employee जो एक विशिष्ट विभाग के नाम पर और एक विशिष्ट स्थान में काम करता है प्राप्त करना चाहते हैं, मैं एक निम्न कोड का उपयोग करेंगे:

NSMutableString *queryString = [NSMutableString stringWithFormat:@"deptEmp1.name == %@ AND deptEmp1.location == %@", @"Apple", @"Cupertino"]; 
NSEntityDescription *entityDescription = [NSEntityDescription entityForName:@"Employee" inManagedObjectContext:self.managedObjectContext]; 

NSFetchRequest *request = [[NSFetchRequest alloc] init]; 
[request setEntity:entityDescription]; 
[request setRelationshipKeyPathsForPrefetching:[NSArray arrayWithObjects:@"Department",nil]]; 
[request setIncludesSubentities:YES]; 

NSArray* returnArray = [self.managedObjectContext executeFetchRequest:request error:&error]; 
if([returnArray count] > 0) { 

    Employee* emp = [returnArray objectAtIndex:0]; 
    NSLog(@"%@ %@ %@", emp.name, emp.dept, emp.deptEmp.location); 
} 

कुछ नोट्स

आप अनुरोध पर लॉक का उपयोग क्यों करते हैं? क्या आपने एक व्यस्त रिलायंस सेट किया है? शायद आपको Department और Employee के बीच एक से अधिक रिश्तों को स्थापित करने की आवश्यकता है?

कोशिश करें और मुझे बताएं।

संपादित

इस एक कोशिश करें। मैंने आपके प्रश्न में क्वेरी स्ट्रिंग को नहीं देखा।

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"deptEmp1.name == %@ AND deptEmp1.location == %@", @"Apple", @"Cupertino"]; 
NSEntityDescription *entityDescription = [NSEntityDescription entityForName:@"Employee" inManagedObjectContext:self.managedObjectContext]; 

NSFetchRequest *request = [[NSFetchRequest alloc] init]; 
[request setEntity:entityDescription]; 
[request setPredicate:predicate]; 
[request setRelationshipKeyPathsForPrefetching:[NSArray arrayWithObjects:@"Department",nil]]; 
[request setIncludesSubentities:YES]; 

NSArray* returnArray = [self.managedObjectContext executeFetchRequest:request error:&error]; 
if([returnArray count] > 0) { 

    Employee* emp = [returnArray objectAtIndex:0]; 
    NSLog(@"%@ %@ %@", emp.name, emp.dept, emp.deptEmp.location); 
} 

संपादित 2

NSEntityDescription *entityDescription = [NSEntityDescription entityForName:@"Employee" inManagedObjectContext:self.managedObjectContext]; 

NSFetchRequest *request = [[NSFetchRequest alloc] init]; 
[request setEntity:entityDescription]; 
[request setRelationshipKeyPathsForPrefetching:[NSArray arrayWithObjects:@"Department",nil]]; 

NSArray* returnArray = [self.managedObjectContext executeFetchRequest:request error:&error]; 
for(Employee* emp in returnArray) { 

    NSLog(@"%@", emp); 
    NSLog(@"%@", emp.deptEmp); 
} 
+0

आपके उत्तर के लिए धन्यवाद, क्वेरी स्ट्रिंग में मैंने deptEmp1 को deptEmp में बदल दिया है और आपके नोट के अनुसार सेटिंग्स, यह बिना किसी त्रुटि के चलाता है। लेकिन कुछ भी वापस नहीं, यहां तक ​​कि मेरे पास डीबी में भी रिकॉर्ड हैं। – kamleshwar

+1

@ कमलेश्वर मैंने एक संपादन जोड़ा। कोशिश करो। –

+1

क्षमा करें, प्रिय लेकिन यह काम नहीं करता है। क्या कोई नमूना कोड नहीं है जिसका मैं पालन कर सकता हूं ?, जैसा कि मैंने कोशिश की थी 2-3 दिनों से कोई समाधान नहीं मिला। मुझे यकीन नहीं है कि समस्या कोड या मॉडल में है या नहीं। आपकी मदद के लिए धन्यवाद – kamleshwar

0

आप एक रिवर्स संबंध स्थापित करने के लिए की जरूरत है। आपका विभाग उद्देश्य यह है कि तरह देखने की जरूरत है:

@interface Department : NSManagedObject 

@property (nonatomic, retain) NSString * location; 
@property (nonatomic, retain) NSString * name; 
@property (nonatomic, retain) NSSet *deptEmp1; 

कि जैसा सब डिपामेंट कर्मचारी का एक संग्रह है और एक एक डिपामेंट पर एक कर्मचारी काम है।

और अपने विधेय आप "deptEmp.name" से "deptEmp1.name" समायोजित करने की आवश्यकता पर

संबंधित मुद्दे