Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/107.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 在CoreData后面复制Sqlite数据库_Ios_Swift_Sqlite_Core Data - Fatal编程技术网

Ios 在CoreData后面复制Sqlite数据库

Ios 在CoreData后面复制Sqlite数据库,ios,swift,sqlite,core-data,Ios,Swift,Sqlite,Core Data,我想访问支持CoreData的Sqlite数据库,制作一份副本,并将其发送到端点进行远程故障排除。这可能吗 我的解决方案 let fileManager = FileManager.default let libraryURL = NSPersistentContainer.defaultDirectoryURL() guard let documentsURL = fileManager.urls(for: .documentDirectory, in: .userDomainMask).

我想访问支持CoreData的Sqlite数据库,制作一份副本,并将其发送到端点进行远程故障排除。这可能吗

我的解决方案

let fileManager = FileManager.default

let libraryURL = NSPersistentContainer.defaultDirectoryURL()

guard let documentsURL = fileManager.urls(for: .documentDirectory, in: .userDomainMask).first else {
    // handle error...
    return
}

// creating a folder in documents for ease of zipping and later removal
let dbURL = documentsURL.appendingPathComponent("db")
do {
    try fileManager.createDirectory(at: dbURL, withIntermediateDirectories: true, attributes: nil)
} catch {
    // handle error...
    return
}

do {
    let fileURLs = try fileManager.contentsOfDirectory(at: libraryURL, includingPropertiesForKeys: nil, options: [])
    for fileURL in fileURLs.filter({ $0.absoluteString.contains("<NAME_OF_CORE_DATA_STORE>") }) {
        let fileName = fileURL.lastPathComponent
        do {
            try fileManager.copyItem(at: fileURL, to: URL(string: "\(documentsURL.absoluteString)db/\(fileName)")!)
        } catch  {
            // handle error...
        }
    }
} catch {
    // handle error...
}

// finally, do something with the file (I zip and send to an endpoint)
让fileManager=fileManager.default
让libraryURL=NSPersistentContainer.defaultDirectoryURL()
guard let documentsURL=fileManager.URL(对于:.documentDirectory,在:.userDomainMask中)。首先{
//处理错误。。。
返回
}
//在文档中创建文件夹以便于压缩和以后删除
让dbURL=documentsURL.appendingPathComponent(“db”)
做{
请尝试fileManager.createDirectory(位于:dbURL,带中间目录:true,属性:nil)
}抓住{
//处理错误。。。
返回
}
做{
让fileURLs=尝试fileManager.contentsOfDirectory(位于:libraryURL,包括属性Forkeys:nil,选项:[])
用于fileURLs.filter({$0.absoluteString.contains(“”})中的fileURL{
让fileName=fileURL.lastPathComponent
做{
试试fileManager.copyItem(在:fileURL,到:URL(字符串:“\(documentsURL.absoluteString)db/\(fileName)”!)
}抓住{
//处理错误。。。
}
}
}抓住{
//处理错误。。。
}
//最后,对该文件执行一些操作(我压缩并发送到端点)

是,可能。只是不要试图在核心数据生态系统之外将其视为可写。好的,不用担心

从写入的位置复制所有部分。默认的SQLite模式是日志模式,因此您将得到一个
wal
文件作为对应文件写入


您可以查询
NSPersistentStore.url
以查找数据库的写入位置

是的,可能。只是不要试图在核心数据生态系统之外将其视为可写。好的,不用担心

从写入的位置复制所有部分。默认的SQLite模式是日志模式,因此您将得到一个
wal
文件作为对应文件写入


您可以查询
NSPersistentStore.url
以了解数据库的写入位置

谢谢Warren,您为我提供了所需的几条信息-我更新了我的问题以包含解决方案。非常感谢!谢谢沃伦,你给了我一些我需要的信息-我更新了我的问题,包括解决方案。非常感谢!