core data database is empty test

前端 未结 3 1040
失恋的感觉
失恋的感觉 2021-01-13 09:28

How can I test if the core data database is empty? I tried:

NSIndexPath *path1 = [NSIndexPath indexPathForRow:0 inSection:0];
NSManagedObject *managedObject          


        
3条回答
  •  -上瘾入骨i
    2021-01-13 09:48

    I have these two methods implemented in my appDelegate:

    - (NSString *)applicationDocumentsDirectory 
    
    {
        
        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        
        NSString *basePath = ([paths count] > 0) ? [paths objectAtIndex:0] : nil;
        
        return basePath;
        
    }
    
        - (NSPersistentStoreCoordinator *)persistentStoreCoordinator 
        
        {
            
            if (persistentStoreCoordinator != nil) 
        
                return persistentStoreCoordinator;
            
            NSURL *storeUrl = [NSURL fileURLWithPath: [[self applicationDocumentsDirectory] stringByAppendingPathComponent: @"YourApp.sqlite"]];
    
            NSLog(@"storeURL: %@", storeUrl);
                    
            NSError *error;
            
            persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel: [self managedObjectModel]];
            
            NSDictionary * options = [NSDictionary dictionaryWithObjectsAndKeys:  
            
                                     [NSNumber numberWithBool:YES], NSMigratePersistentStoresAutomaticallyOption,  
                                     
                                     [NSNumber numberWithBool:YES], NSInferMappingModelAutomaticallyOption, nil];  
            
            if (![persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeUrl options:options error:&error]) 
            
            {
                
                /* 
                Replace this implementation with code to handle the error appropriately. 
          
                abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be 
                        useful during development. If it is not possible to recover from the error, display an alert panel that instructs the user to quit the application by pressing the Home button. 
          
                Typical reasons for an error here include: 
                * The persistent store is not accessible 
                * The schema for the persistent store is incompatible with current managed object model 
                Check the error message to determine what the actual problem was. 
                */
                
            
            }// if    
            
            return persistentStoreCoordinator;
        }
    

    The storeUrl prints the path to the sqlite database.

    If you open this path with a sqlite manager, you're able to see the content of your sql database. I use this SQLite Manager to analyze sqlite databases: SQLite Manager

    (You can only use this method on the simulator)

提交回复
热议问题