开发者

How to Determine if a Table Contains Any Records with Core Data?

I haven't seen any other questions quite like this on here, but I'm hoping someone has some insight. I'm just starting to learn Core Data.

Basically, I have two methods and I want to choose which one to call with an if/else statement based on whether or not the "Contacts" table contains any records. Is there a way using core data to check if there are any records in a table?

开发者_如何学C

The best way I've found so far is to set the fetchLimit to 1 and then check to see if anything returns.

[request setFetchLimit:1];

But I keep thinking there has to be a better/easier way. Anyone know or have a good reference I can look at?

Thanks a ton!


Yes, definitely there is a better method. Setup a fetch request as usual, but, instead of actually executing it, simply ask for the number of objects it would have returned if it had been passed to executeFetchRequest:error:

This can be done using

- (NSUInteger)countForFetchRequest:(NSFetchRequest *)request error:(NSError **)error;

Something like this:

- (int) numberOfContacts{

    NSFetchRequest *request = [[NSFetchRequest alloc] init];
    NSManagedObjectContext *managedObjectContext = yourManagedObjectContext;
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"Contacts" inManagedObjectContext:managedObjectContext];
    [request setEntity:entity];

    NSError *error = nil;
    NSUInteger count = [managedObjectContext countForFetchRequest:request error:&error];
    [request release];

    if (!error){
        return count;
    }
    else
        return -1;

}


It's not necessarily any better or easier, but you can look for a specific record and then create it if it doesn't exist like this:

NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Contact" 
                            inManagedObjectContext:[self managedObjectContext]];
[fetchRequest setEntity:entity];

NSError *error;
// Filter based on a predicate
[fetchRequest setPredicate:
                [NSPredicate predicateWithFormat:@"identifier == %@", @"1"]];
NSManagedObject *contact = [[managedObjectContext 
                   executeFetchRequest:fetchRequest error:&error] lastObject];

// If the contact was not found
if (!contact)
{
  // Create the contact
  contact = [NSEntityDescription insertNewObjectForEntityForName:@"Contact" 
                                  inManagedObjectContext:managedObjectContext];
  [contact setValue:[NSNumber numberWithInt:1] forKey:@"identifier"];
  [managedObjectContext save:nil];
}

Marcus Zarra posted some code that demonstrates this in a feed reader app. Marcus is the Core Data master.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜