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

  • Home
  • SEARCH
  • 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 8326193
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T00:41:20+00:00 2026-06-09T00:41:20+00:00

When a UIManagedDocument instance is opened (document state is normal?), how to check if

  • 0

When a UIManagedDocument instance is opened (document state is normal?), how to check if a UIManagedDocument is empty? By “empty” here means no entity in the document at all.

Or could I just check the emptiness when it is still closed?

if (self.photoDatabase.documentState == UIDocumentStateClosed) 
{ 
    [self.photoDatabase openWithCompletionHandler:^(BOOL success) {}];
    [self setupFetchedResultsController];  

    if ( // check if document is empty  )
        {  
            [self fetchFlickrDatatoDocument:self.photoDatabase]; 
        }
}
  • 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-09T00:41:22+00:00Added an answer on June 9, 2026 at 12:41 am

    Several answers here…

    First, the document is surely empty wen you create it. You can just query the file system to see if the file exists.

    If it remains empty, why did you create it in the first place?

    If you are in the habit of opening empty database files, then you can tell if it is “empty” in a number of ways, depending on what “empty” means.

    Perform a fetch to get the count of records. If it returns 0, the database is empty.

    Perform a fetch for a “special” entity that keeps meta data about the database. If it is not there, then the database is empty.

    EDIT

    To check and see if there are no entities, just ask for the count…

    NSFetchRequest fetchRequest = [NSFetchRequest fetchRequestWithEntityName:@"MyEntity"];
    NSError *error = nil;
    NSUInteger count = [managedObjectContext countForFetchRequest:fetchRequest error:&error];
    if (count == NSNotFound) {
        // Handle error
    } else if (count == 0) {
        // The document is empty, i.e., has no entities of this type
    } else {
        // The document contains <count> entities.
    }
    

    Or, you can just fetch one entity…

    NSFetchRequest fetchRequest = [NSFetchRequest fetchRequestWithEntityName:@"MyEntity"];
    fetchRequest.fetchLimit = 1;
    NSError *error = nil;
    NSArray *objects = [managedObjectContext executeFetchRequest:fetchRequest error:&error];
    if (objects == nil) {
        // Handle error
    } else if (objects.count == 0) {
        // The document is empty, i.e., has no entities of this type
    } else {
        // The document contains at least one entity.
    }
    

    EDIT

    Sorry, Philip, but that last comment does not make sense to me. How is asking the database to tell me how many items it has of a certain entity type assuming that it has any at all.

    It’s like the kid’s card game:

    Me: Yo, database, you got any fours?
    CoreData: Go fish.

    Now, my best guess at your comment is that you are confusing database model with database records.

    When you create the database, you gave it a model. Did you create the model with the GUI, or in code? Either way, you have already told the database the types of entities it will contain, and the relationships between them.

    When you initially create the database, it already knows about all the entities (that’s what the managed object model is all about). The core-data managed object context needs a persistent store, which needs a managed object model.

    Thus, when the core data stack is completely instantiated, it has all the knowledge it needs about what types of entities will be in the database.

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

Sidebar

Related Questions

I have a helper class that distributes a shared instance of UIManagedDocument. The idea
How to import data from existing non-document Core Data store to uimanageddocument? The question
in the Stack Overflow posting: How do I create a global UIManagedDocument instance per
Here is the call with the block: [VacationHelper openVacationWithName:vacationName usingBlock:^(UIManagedDocument *vacationDocument) { NSLog(@vacationDocument.description:%@, vacationDocument.description);
I'm using an UIManagedDocument in my project and have to change the relationship of
I am using a NSFetchedResultsController in combination with a UIManagedDocument which gets updated in
Can anyone show me how to make a UIManagedDocument globally accessible?
I'm using CoreData (NSManagedObject, UIManagedDocument) for an iOS app and I was wondering if
I'm having a lot of trouble deciphering Apple's documentation around UIManagedDocument , specifically the
I am using Core Data for the first time and was just curious if

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.