简体   繁体   中英

How to verify that NSMutableArray of entity contains object with certain attribute

Based on the data model here: Photographer<------->>Photo

When the user goes to add a Photo , the user also specifies what Photographer took said picture. When the user decides to save the Photo , there is the possibility that the Photographer doesn't exist yet. After executing a NSFetchRequest to get a list of all instances of Photographer , how do I check if the NSMutableArray(photographerArray) contains an object that has the same fullName attribute as what the user is currently adding?

The naive way would be to simply get all the fullName of the array and check if it's in it.

BOOL photographerExist = [[listOfPhotographer valueForKey:@"fullName"] containsObject:enteredFullName];

However, it would be way more easier to put that directly in your fetch request. Just add a predicate to it.

NSPredicate *fullNamePredicate = [NSPredicate predicateWithFormat:@"fullName = %@", enteredFullName];
fetchRequest.predicate = fullNamePredicate;

If the result of the fetch is empty, then the photographer doesn't exist yet.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM