Porque iOS 10.0+
puedes usar
persistentContainer.persistentStoreCoordinator.persistentStores.first?.url
He creado la función de utilidad que copia el archivo sqlite a la ubicación deseada (funciona solo para el simulador).
Puedes usarlo como
import CoreData
let appDelegate = UIApplication.shared.delegate as! AppDelegate
UTility.getSqliteTo(destinationPath: "/Users/inderkumarrathore/Desktop", persistentContainer: appDelegate.persistentContainer)
Si ya tiene acceso a los archivos sqlite, shm y wal, ejecute los comandos en la terminal para fusionar el archivo WAL en el archivo sqlite.
$ sqlite3 persistentStore
sqlite> PRAGMA wal_checkpoint;
Press control + d
Después de ejecutar los comandos anteriores, puede ver los datos en su archivo sqlite.
Aquí está la definición de método de utilidad 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")
}
por C objetivo
/**
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");
}