How can I check what is stored in my Core Data Database?

后端 未结 17 2169
北恋
北恋 2020-12-12 14:35

I am making an app that relies on Core Data. I am able to enter data into a text field and store it.

But I need to know if the data is being stored.

I am try

相关标签:
17条回答
  • 2020-12-12 15:03

    Just add in viewDidLoad:

    debugPrint(FileManager.default.urls(for: .documentDirectory, in: .userDomainMask))
    
    0 讨论(0)
  • 2020-12-12 15:07

    The folder where the db is stored has recently been changed and is not where you'd expect to find it. Here's what I used to solve this: First add this code to your viewDidLoad or applicationDidFinishLaunching:

        #if TARGET_IPHONE_SIMULATOR
        // where are you?
        NSLog(@"Documents Directory: %@", [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject]);
        #endif
    

    Got this from here: where is the documents directory for the ios 8 simulator

    This will reveal the actual location of your app in the console during runtime. Then use the SQLiteBrowser to check the contents of your SQLite DB.

    Worked like a charm for me ;)

    0 讨论(0)
  • 2020-12-12 15:09

    Download the SQLite Browser from here.

    Run your app in the Simulator. The app should be copied to a path on your Mac that looks like:

    /Users/$your username$/Library/Application Support/iPhone Simulator/$your iphone simulator version$/Applications/
    

    Once you locate your app, you have to dig deeper to find the sqlite db (It's usually under Documents).

    0 讨论(0)
  • 2020-12-12 15:13

    If you use sqlite as the storage media for Core Data, you can run your app in simulator and try to check the database file which is located in the sandbox's Library folder.

    The path shall be something like: ~/Library/Application Support/iPhone Simulator/5.1/Applications/3BF8A4B3-4959-4D8F-AC12-DB8EF4C3B6E1/Library/YourAppName.sqlite

    To open the sqlite file, you need a tool. I use a free tool called Liya (http://itunes.apple.com/us/app/liya/id455484422?mt=12).

    0 讨论(0)
  • 2020-12-12 15:14

    If you've already access to sqlite, shm and wal files then run the commands in the terminal to merge the WAL file into the sqlite file.

    $ sqlite3 persistentStore
    sqlite> PRAGMA wal_checkpoint;
    Press control + d
    

    After running the above commands you can see the data in your sqlite file.


    Utility to copy sqlite files to your desktops (do change the desktop path and give the absolute path, the ~ symbol won't work.

    For iOS 10.0+ you can use persistentContainer.persistentStoreCoordinator.persistentStores.first?.url

    I have created the Utility function that copies the sqlite file to your desired location (works only for simulator). You can use the utility it like

    import CoreData
    
    
    let appDelegate = UIApplication.shared.delegate as! AppDelegate
    UTility.getSqliteTo(destinationPath: "/Users/inderkumarrathore/Desktop", persistentContainer: appDelegate.persistentContainer)
    

    Here is definition of utility method for swift

    /**
     Copies the sqlite, wal and shm file to the destination folder. Don't forget to merge the wal file using the commands printed int the console.
     @param destinationPath Path where sqlite files has to be copied
     @param persistentContainer NSPersistentContainer
    */
    public static func getSqliteTo(destinationPath: String, persistentContainer: NSPersistentContainer) {
      let storeUrl = persistentContainer.persistentStoreCoordinator.persistentStores.first?.url
      
      let sqliteFileName = storeUrl!.lastPathComponent
      let walFileName = sqliteFileName + "-wal"
      let shmFileName = sqliteFileName + "-shm"
      //Add all file names in array
      let fileArray = [sqliteFileName, walFileName, shmFileName]
      
      let storeDir = storeUrl!.deletingLastPathComponent()
      
      // Destination dir url, make sure file don't exists in that folder
      let destDir = URL(fileURLWithPath: destinationPath, isDirectory: true)
    
      do {
        for fileName in fileArray {
          let sourceUrl = storeDir.appendingPathComponent(fileName, isDirectory: false)
          let destUrl = destDir.appendingPathComponent(fileName, isDirectory: false)
          try FileManager.default.copyItem(at: sourceUrl, to: destUrl)
          print("File: \(fileName) copied to path: \(destUrl.path)")
        }
      }
      catch {
        print("\(error)")
      }
      print("\n\n\n ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ NOTE ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~\n")
      print("In your terminal run the following commands to merge wal file. Otherwise you may see partial or no data in \(sqliteFileName) file")
      print("\n-------------------------------------------------")
      print("$ cd \(destDir.path)")
      print("$ sqlite3 \(sqliteFileName)")
      print("sqlite> PRAGMA wal_checkpoint;")
      print("-------------------------------------------------\n")
      print("Press control + d")      
    }
    

    For objective-c

    /**
     Copies the sqlite, wal and shm file to the destination folder. Don't forget to merge the wal file using the commands printed int the console.
     @param destinationPath Path where sqlite files has to be copied
     @param persistentContainer NSPersistentContainer
     */
    + (void)copySqliteFileToDestination:(NSString *)destinationPath persistentContainer:(NSPersistentContainer *)persistentContainer {
      NSError *error = nil;
      NSURL *storeUrl = persistentContainer.persistentStoreCoordinator.persistentStores.firstObject.URL;
      NSString * sqliteFileName = [storeUrl lastPathComponent];
      NSString *walFileName = [sqliteFileName stringByAppendingString:@"-wal"];
      NSString *shmFileName = [sqliteFileName stringByAppendingString:@"-shm"];
      //Add all file names in array
      NSArray *fileArray = @[sqliteFileName, walFileName, shmFileName];
      
      // Store Directory
      NSURL *storeDir = storeUrl.URLByDeletingLastPathComponent;
    
      // Destination dir url, make sure file don't exists in that folder
      NSURL *destDir = [NSURL fileURLWithPath:destinationPath isDirectory:YES];
      
      for (NSString *fileName in fileArray) {
        NSURL *sourceUrl = [storeDir URLByAppendingPathComponent:fileName isDirectory:NO];
        NSURL *destUrl = [destDir URLByAppendingPathComponent:fileName isDirectory:NO];
        [[NSFileManager defaultManager] copyItemAtURL:sourceUrl toURL:destUrl error:&error];
        if (!error) {
          RLog(@"File: %@ copied to path: %@", fileName, [destUrl path]);
        }
      }
      
      
      NSLog(@"\n\n\n ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ NOTE ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~\n");
      NSLog(@"In your terminal run the following commands to merge wal file. Otherwise you may see partial or no data in %@ file", sqliteFileName);
      NSLog(@"\n-------------------------------------------------");
      NSLog(@"$ cd %@", destDir.path);
      NSLog(@"$ sqlite3 %@", sqliteFileName);
      NSLog(@"sqlite> PRAGMA wal_checkpoint;");
      NSLog(@"-------------------------------------------------\n");
      NSLog(@"Press control + d");
    }
    
    0 讨论(0)
提交回复
热议问题