Basic data. Insert a new object. Check if the same objects exist.

I work with Core Data and get two objects (Account, Contact), where one account can have several contacts (one-to-many).

When inserting a new object, I want to check whether the phone number of the new contact belongs to the existing contact. If the number exists, there should be a notification to the user.

In "ContactAddViewController.m" the following method is called where contactData is an NSMutableDictionary with the values ​​and keys for the new contact (entered in the text fields).

[ContactInfo findOrCreateContact:contactData inManagedObjectContext:context]; 

In "AccountInfo.m" a request for a phone with a predicate and the value of my text field is set. If the phone number does not exist, the contact will be added to the ContactInfo object - 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 0 to an NSString (or sth like this) so that I can create an if statement 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. } 
+4
source share
1 answer

Do you mean the return value of this method?

 + (ContactInfo *)findOrCreateContact:(NSDictionary *)data inManagedObjectContext:(NSManagedObjectContext *)context 

First, the designation of the state of the find OR create function, so with this name you should return the object in both cases, the one you found or just created. This will avoid confusion.

Secondly, your return type is an object, so you must return nil or an object.

So, at this point, you need to decide whether you want to keep your name or not, but let me say what you want.
You may have a method like this:

 + (ContactInfo *)findOrCreateContact:(NSDictionary *)data inManagedObjectContext:(NSManagedObjectContext *)context contactWasCreated:(BOOL *)isNew 

By passing a pointer to BOOL, you can set it to YES if ContactInfo was created, and NO if you return one that already exists, and you can still return zero if you did not have ContactInfo return. Passing BOOL as a pointer is inspired by - - (BOOL)save:(NSError **)error , where you pass a pointer to an object, and you can get an NSError if necessary.
So if you could say

 if (contanctInfo && wasCreated) { // I've got a new one } else if (contactInfo) { // I just receive an old one } else { // something very bad just happened... } 

You can also decide to return an NSDictionary with ContactInfo as one record and NSNumber for BOOL as another record.

+4
source

Source: https://habr.com/ru/post/1386419/


All Articles