iOS 10.0+
आप के लिए उपयोग कर सकते हैं
persistentContainer.persistentStoreCoordinator.persistentStores.first?.url
मैंने उपयोगिता फ़ंक्शन बनाया है जो आपके इच्छित स्थान (केवल सिम्युलेटर के लिए काम करता है) के लिए स्क्लाइट फाइल की प्रतिलिपि बनाता है।
आप इसका उपयोग कर सकते हैं
import CoreData
let appDelegate = UIApplication.shared.delegate as! AppDelegate
UTility.getSqliteTo(destinationPath: "/Users/inderkumarrathore/Desktop", persistentContainer: appDelegate.persistentContainer)
यदि आप पहले से ही sqlite, shm और wal files तक पहुँच चुके हैं, तो टर्मिनल में वाक फ़ाइल को sqiteite फ़ाइल में मर्ज करने के लिए टर्मिनल में कमांड चलाएँ।
$ sqlite3 persistentStore
sqlite> PRAGMA wal_checkpoint;
Press control + d
उपरोक्त आदेशों को चलाने के बाद आप डेटा को अपनी sqlite फ़ाइल में देख सकते हैं।
यहाँ के लिए उपयोगिता विधि की परिभाषा है तीव्र
/**
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")
}
के लिये उद्देश्य सी
/**
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");
}