2009-09-17 19 views
10

में संपत्ति द्वारा ऑब्जेक्ट प्राप्त करें मेरे आईफोन प्रोजेक्ट में, मैं एक फ़ंक्शन लिखना चाहता हूं जो गीले जांचता है कि मेरे कोर डेटा प्रबंधित ऑब्जेक्ट कॉन्टेक्स्ट में किसी ऑब्जेक्ट की एक निश्चित संपत्ति के लिए दिए गए मान के साथ कोई ऑब्जेक्ट है, some_property कहें।कोर डेटा

यदि some_property == 12 के साथ पहले से ही कोई ऑब्जेक्ट है, तो मैं चाहता हूं कि फ़ंक्शन ऑब्जेक्ट को वापस कर दे, अन्यथा, मैं ऑब्जेक्ट बनाना चाहता हूं, या कम से कम nil लौटा दूंगा।

मैं यह कैसे करूँगा?

उत्तर

19

निम्न स्निपेट दिखाता है कि एक विशिष्ट predicate से मेल खाने वाली वस्तुओं को कैसे पुनर्प्राप्त करें। यदि ऐसी कोई वस्तु नहीं है, तो स्निपेट दिखाता है कि एक नई वस्तु कैसे बनाएं, इसे सहेजें और इसे वापस करें।

NSFetchRequest *request = [[NSFetchRequest alloc] init]; 
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"YourEntityName" inManagedObjectContext:managedObjectContext]; 
    [request setEntity:entity]; 
    // retrive the objects with a given value for a certain property 
    NSPredicate *predicate = [NSPredicate predicateWithFormat: @"property == %@", value]; 
    [request setPredicate:predicate]; 

    // Edit the sort key as appropriate. 
    NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"yourSortKey" ascending:YES]; 
    NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor, nil]; 
    [request setSortDescriptors:sortDescriptors]; 



    // Edit the section name key path and cache name if appropriate. 
    // nil for section name key path means "no sections". 
    NSFetchedResultsController *aFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:request managedObjectContext:managedObjectContext sectionNameKeyPath:nil cacheName:@"Root"]; 
    aFetchedResultsController.delegate = self; 

    NSError *error = nil; 
    NSArray *result = [managedObjectContext executeFetchRequest:request error:&error]; 

    [request release]; 
    [sortDescriptor release]; 
    [sortDescriptors release]; 


    if ((result != nil) && ([result count]) && (error == nil)){ 
     return [NSMutableArray arrayWithArray:result]; 
    } 
    else{ 
     YourEntityName *object = (YourEntityName *) [NSEntityDescription insertNewObjectForEntityForName:@"YourEntityName" inManagedObjectContext:self.managedObjectContext]; 
      // setup your object attributes, for instance set its name 
      object.name = @"name" 

      // save object 
      NSError *error; 
      if (![[self managedObjectContext] save:&error]) { 
      // Handle error 
      NSLog(@"Unresolved error %@, %@", error, [error userInfo]); 

      } 

      return object; 

    } 
+0

वाह, कि जल्दी थी! मुझे बस इसे आज़माएं ... – winsmith

+0

'एफ़ेटेड रीसेट कंट्रोलर' का बिंदु क्या है? क्या मुझे यह सोचने में गलती है कि आप इसे बनाते हैं, लेकिन फिर कभी भी ऐसा करने के लिए इसका इस्तेमाल नहीं करते? – ArtOfWarfare

+0

आप सही हैं, इस विशेष उदाहरण में NSFetchedResultsController का उपयोग नहीं किया जाता है, लेकिन यह वास्तविक अनुप्रयोग के संदर्भ में होना चाहिए (यह कई अन्य चीजों को सरल बनाता है, और एक अच्छा कैशिंग तंत्र प्रदान करता है)। –

2

यदि आप स्थानीय डेटा पर कुछ गुणों की जांच करना चाहते हैं तो यह बेहतर होगा यदि आप एकाधिक fetching नहीं करते हैं। प्री-पॉप्युलेटेड सरणी का उपयोग करके बस एक fetch अनुरोध करें और फिर परिणामों को फिर से भरें या फ़िल्टर करें।

इस कोर डेटा प्रोग्रामिंग गाइड से एक कोड का टुकड़ा "कार्यान्वयन खोजें या बनाएं कुशलतापूर्वक" है:

// get the names to parse in sorted order 
NSArray *employeeIDs = [[listOfIDsAsString componentsSeparatedByString:@"\n"] 
     sortedArrayUsingSelector: @selector(compare:)]; 

// create the fetch request to get all Employees matching the IDs 
NSFetchRequest *fetchRequest = [[[NSFetchRequest alloc] init] autorelease]; 
[fetchRequest setEntity: 
     [NSEntityDescription entityForName:@"Employee" inManagedObjectContext:aMOC]]; 
[fetchRequest setPredicate: [NSPredicate predicateWithFormat: @"(employeeID IN %@)", employeeIDs]]; 

// make sure the results are sorted as well 
[fetchRequest setSortDescriptors: [NSArray arrayWithObject: 
     [[[NSSortDescriptor alloc] initWithKey: @"employeeID" 
       ascending:YES] autorelease]]]; 
// Execute the fetch 
NSError *error; 
NSArray *employeesMatchingNames = [aMOC 
     executeFetchRequest:fetchRequest error:&error];