简体   繁体   English

在Simulator App启动之间丢失的SQLite数据

[英]SQLite data lost between Simulator App launches

This feels strange and I somehow could not find an answer through search. 这感觉很奇怪,我以某种方式无法通过搜索找到答案。 This is an issue on Xcode iOS Simulator. 这是Xcode iOS模拟器上的问题。

I am write an data persistence code. 我正在写一个数据持久性代码。 When instantiate the data store, I pretty much copied the following code fragment from Apple ( https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/CoreData/InitializingtheCoreDataStack.html#//apple_ref/doc/uid/TP40001075-CH4-SW1 ). 实例化数据存储时,我几乎从Apple复制了以下代码片段( https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/CoreData/InitializingtheCoreDataStack.html#//apple_ref/doc/uid / TP40001075-CH4-SW1 )。

Now, the issue seems - storeURL (when in debugging) is a URL containing a long numeric string and when relaunching the simulator, that long numeric string was changed. 现在,问题似乎出现了-storeURL(在调试时)是一个包含长数字字符串的URL,并且在重新启动模拟器时,该长数字字符串已更改。 So the sqlite file is not reachable any more. 因此,sqlite文件不再可用。 I do believe I saved the data correctly because I debugged through the code (and context save api called with no error), I retrieved data after saving (without relaunching) and I see the data using command line sqlite tool against the sqlite file. 我确实相信我正确地保存了数据,因为我通过代码进行了调试(并且没有错误地调用了上下文保存api),保存后检索了数据(没有重新启动),并且使用sqlite工具的命令行sqlite工具查看了数据。

import UIKit
import CoreData
class DataController: NSObject {
    var managedObjectContext: NSManagedObjectContext
    init() {
        // This resource is the same name as your xcdatamodeld contained in your project.
        guard let modelURL = NSBundle.mainBundle().URLForResource("DataModel", withExtension:"momd") else {
            fatalError("Error loading model from bundle")
        }
        // The managed object model for the application. It is a fatal error for the application not to be able to find and load its model.
        guard let mom = NSManagedObjectModel(contentsOfURL: modelURL) else {
            fatalError("Error initializing mom from: \(modelURL)")
        }
        let psc = NSPersistentStoreCoordinator(managedObjectModel: mom)
        self.managedObjectContext = NSManagedObjectContext(concurrencyType: .MainQueueConcurrencyType)
        self.managedObjectContext.persistentStoreCoordinator = psc
        dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0)) {
            let urls = NSFileManager.defaultManager().URLsForDirectory(.DocumentDirectory, inDomains: .UserDomainMask)
            let docURL = urls[urls.endIndex-1]
            /* The directory the application uses to store the Core Data store file.
            This code uses a file named "DataModel.sqlite" in the application's documents directory.
            */
            let storeURL = docURL.URLByAppendingPathComponent("DataModel.sqlite")
            do {
                try psc.addPersistentStoreWithType(NSSQLiteStoreType, configuration: nil, URL: storeURL, options: nil)
            } catch {
                fatalError("Error migrating store: \(error)")
            }
        }
    }
}

This is what basically Matt said in the comment section. 这基本上是Matt在评论部分所说的。 This issue is related to where to place the .sqlite file. 此问题与.sqlite文件的放置位置有关。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM