I’m working with Core Data and got two entities (Account, Contact) where one account can have multiple contacts (one-to-many-relationship).
When inserting a new object I want to check if the phone number of the new contact already belongs to an existing contact. If the number exists, there should be a notification for the user.
In “ContactAddViewController.m” the following method is called where contactData is a NSMutableDictionary with the values and keys for the new contact (entered in textfields).
[ContactInfo findOrCreateContact:contactData inManagedObjectContext:context];
In “AccountInfo.m” the request for the phone with the predicate and the value of my textfield is set. If the phone number doesn’t exist, the contact will be added to the entity “ContactInfo” – if it exists, the method returns 0.
+ (ContactInfo *)findOrCreateContact:(NSDictionary *)data inManagedObjectContext:(NSManagedObjectContext *)context
{
ContactInfo *contactInfo = nil;
NSFetchRequest *request = [[NSFetchRequest alloc] init];
request.entity = [NSEntityDescription entityForName:@"ContactInfo" inManagedObjectContext:context];
request.predicate = [NSPredicate predicateWithFormat:@"phone == %@", [data objectForKey:@"phone"]];
NSError *error = nil;
contactInfo = [[context executeFetchRequest:request error:&error] lastObject];
if (!error && !contactInfo)
{
// if phone number can't be found, create new contact
contactInfo = [NSEntityDescription insertNewObjectForEntityForName:@"ContactInfo" inManagedObjectContext:context];
contactInfo.firstName = [data objectForKey:@"firstName"];
contactInfo.lastName = [data objectForKey:@"lastName"];
contactInfo.phone = [data objectForKey:@"phone"];
contactInfo.updatedDate = [NSDate date];
contactInfo.cinfoTOainfo = [AccountInfo findOrCreateAccount:data inManagedObjectContext:context];
}
// Return object
return contactInfo;
}
__
Question
How can I set the return value of 0 to a NSString (or sth like that), so that I can build up an if-clause like the following?
if ( ... ) // What should I put in here?
{
// if phone number doesn't exist, do that.
}
else
{
// if phone number exist, do that.
}
You mean the return value from that method?
First the naming of the function state
find OR createso with a name like that you should return an object in both case, the one you have find or the one just created. That would avoid possible confusion.Second, Your return type is an object, so you should return
nilor an object.So at this point you have to decide if you want to comply with your naming or not, but let say you want.
You could have a method like this :
By passing a pointer to a BOOL you can get set it to YES if the ContactInfo was created and NO if you are returning an one that already existed, and you can still return nil if you wasn’t able to have a ContactInfo to return. Passing a BOOL as a pointer is inspired by
- (BOOL)save:(NSError **)errorwhere you are passing a pointer to an object an can retreive the NSError if needed.So in your if you could say
You could also decide to return a NSDictionary with the ContactInfo as one entry and a NSNumber for the BOOL as an other entry.