Para iOS 10.0+
você pode usar
persistentContainer.persistentStoreCoordinator.persistentStores.first?.url
Criei a função Utilitário que copia o arquivo sqlite para o local desejado (funciona apenas para simulador).
Você pode usar como
import CoreData
let appDelegate = UIApplication.shared.delegate as! AppDelegate
UTility.getSqliteTo(destinationPath: "/Users/inderkumarrathore/Desktop", persistentContainer: appDelegate.persistentContainer)
Se você já tiver acesso aos arquivos sqlite, shm e wal, execute os comandos no terminal para mesclar o arquivo WAL no arquivo sqlite.
$ sqlite3 persistentStore
sqlite> PRAGMA wal_checkpoint;
Press control + d
Depois de executar os comandos acima, você pode ver os dados em seu arquivo sqlite.
Aqui está a definição do método de utilidade para rápido
/**
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")
}
Para objetivo-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");
}