Note that there are some explanatory texts on larger screens.

plurals
  1. POCore Data - Inserting new object, check if the same objects exists, handle return
    text
    copied!<p>I'm working with Core Data and got two entities (Account, Contact) where one account can have multiple contacts (one-to-many-relationship). </p> <p>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. </p> <p>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).</p> <pre><code>[ContactInfo findOrCreateContact:contactData inManagedObjectContext:context]; </code></pre> <p>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.</p> <pre><code>+ (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:&amp;error] lastObject]; if (!error &amp;&amp; !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; } </code></pre> <p>__</p> <p><strong>Question</strong> </p> <p>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?</p> <pre><code>if ( ... ) // What should I put in here? { // if phone number doesn't exist, do that. } else { // if phone number exist, do that. } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload