Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8005201
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T17:09:42+00:00 2026-06-04T17:09:42+00:00

Below is my find-or-create code: + (id)checkIfEntity:(NSString *)entityName withIDValue:(NSString *)entityIDValue forIDKey:(NSString *)entityIDKey existsInContext:(NSManagedObjectContext *)context

  • 0

Below is my find-or-create code:

+ (id)checkIfEntity:(NSString *)entityName 
        withIDValue:(NSString *)entityIDValue 
           forIDKey:(NSString *)entityIDKey 
    existsInContext:(NSManagedObjectContext *)context
{

    // Create fetch request
    NSFetchRequest *request = [[NSFetchRequest alloc] init];
    [request setReturnsObjectsAsFaults:NO];

    // Fetch messages data
    NSEntityDescription *description = [NSEntityDescription entityForName:entityName inManagedObjectContext:context];
    [request setEntity:description];

    // Only include objects that exist (i.e. entityIDKey and entityIDValue's must exist)
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"%@=%@", entityIDKey, entityIDValue];
    [request setPredicate:predicate];    

    // Execute request that returns array of dashboardEntrys
    NSArray *array = [context executeFetchRequest:request error:nil];

    if ( [array count] ) {

        NSLog(@"%@ = %@ DOES EXIST", entityIDKey, entityIDValue);
        return [array objectAtIndex:0];
    }

    NSLog(@"%@ = %@ DOES NOT EXIST", entityIDKey, entityIDValue);
    return [NSEntityDescription insertNewObjectForEntityForName:entityName inManagedObjectContext:context];
}

Please ignore the fact that I’m not employing best-practices by having two return statements – I will this before pushing my code to production.

It works by passing an NSManagedObject entity with entityName, and has an NSPredicate that checks if there’s an entityIDKey with value entityIDValue; entityIDKey = entityIDValue. I’ve also tried with using ==.

Regardless which comparison method I use, the if ( [array count] ) method is never hit, so the second NSLog statement always gets output, delineating that an object that I know exists in my Core Data store doesn’t actually exist. Therefore, I end up with many duplicate entries when I go to display the contents of my store.

Is my NSPredicate statement correct?

    `NSPredicate *predicate = [NSPredicate predicateWithFormat:@"%@=%@", entityIDKey, entityIDValue];`

where entityIDKey = @"userID" and entityIDvalue = @"1234567890"

Thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T17:09:44+00:00Added an answer on June 4, 2026 at 5:09 pm

    Yup, the problem was definitely the predicate.

    The predicate argument should have been:

    %K == %@

    not

    %@ = %@

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Please find the below code <?xml version=1.0 encoding=utf-8?> <mx:Application xmlns:mx=http://www.adobe.com/2006/mxml layout=absolute> <mx:Script> <![CDATA[ [Bindable]
I'm doing a homework project that requires this: Below you will find the code
I constantly find myself writing similar code like the example below: if (object[Object Name]
The code below doesn't seem to work or find anything on an array. I'm
I am trying to find out why the code below does not seem to
In code below: map<string,vector<int>> create(ifstream& in, const vector<string>& vec) { /*holds string and line
So in the example code below, I create a UserControl UserControldChild which is a
I used the code below to dynamically create a group of radio buttons: self.wPaymantType.qgbSomeSelectionGroup
Here's the error I'm getting with the code pasted below. Unable to create instance
With the below code I'm creating a fetch request. The problem I'm having is

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.