用swift加载sqlite商店

时间:2014-12-07 20:09:59

标签: sqlite core-data swift xcode6.1

我想加载一个sqlite商店,如果在Xcode中添加到我的Project中。我知道如何在Objective-C中做到这一点,但我还在学习Swift。我在Swift中尝试过但没有成功。 有人知道我怎么能在Swift中做到这一点吗?

我的删除代码:

   var daten = [Notizen]()
   let context = (UIApplication.sharedApplication().delegate as AppDelegate).managedObjectContext


func tableView(tableView: UITableView!, canEditRowAtIndexPath indexPath: NSIndexPath!) -> Bool {
    return true
}

func tableView(tableView: UITableView!, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath!) {
    if (editingStyle == UITableViewCellEditingStyle.Delete) {
        // handle delete (by removing the data from your array and updating the tableview)


        if let tv=tableView
        {

            context?.deleteObject(daten[indexPath.row])
            context?.save(nil)
            let fetchRequest = NSFetchRequest(entityName: "Notizen")
            daten = context?.executeFetchRequest(fetchRequest, error: nil) as [Notizen]
            tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Right)

        }
    }
}

崩溃日志:

2014-12-16 17:48:17.197 NoteApp[3161:45746] CoreData: error: failed to resolve optimistic locking failure: optimistic locking failure with (null) 2014-12-16 17:48:17.197 NoteApp[3161:45746] CoreData: error: failed to resolve optimistic locking failure. Old save request was: <NSSaveChangesRequest: 0x7a6c9270> { inserts (), updates (), deletes (( "0x7b862ec0 <x-coredata://21112FCF-88DE-4D7F-A5C5-1A97731D180E/Notizen/p2>" )) locks () } 2014-12-16 17:48:17.198 NoteApp[3161:45746] CoreData: error: failed to resolve optimistic locking failure. Next attempt will be: <NSSaveChangesRequest: 0x7a6c9270> { inserts (), updates (), deletes (( "0x7b862ec0 <x-coredata://21112FCF-88DE-4D7F-A5C5-1A97731D180E/Notizen/p2>" )) locks () } 2014-12-16 17:48:17.198 NoteApp[3161:45746] CoreData: sql: BEGIN EXCLUSIVE 2014-12-16 17:48:17.198 NoteApp[3161:45746] CoreData: sql: DELETE FROM ZNOTIZEN WHERE Z_PK = ? AND Z_OPT = ? 2014-12-16 17:48:17.198 NoteApp[3161:45746] CoreData: details: SQLite bind[0] = (int64)2 2014-12-16 17:48:17.198 NoteApp[3161:45746] CoreData: details: SQLite bind[1] = nil 2014-12-16 17:48:17.199 NoteApp[3161:45746] CoreData: sql: ROLLBACK 2014-12-16 17:48:17.199 NoteApp[3161:45746] CoreData: sql: SELECT Z_PK,Z_OPT FROM ZNOTIZEN WHERE Z_PK IN (2) ORDER BY Z_PK 2014-12-16 17:48:17.199 NoteApp[3161:45746] CoreData: annotation: sql execution time: 0.0002s 2014-12-16 17:48:17.199 NoteApp[3161:45746] CoreData: error: failed to resolve optimistic locking failure: optimistic locking failure with (null) 2014-12-16 17:48:17.211 NoteApp[3161:45746] CoreData: error: failed to resolve optimistic locking failure. Old save request was: <NSSaveChangesRequest: 0x7a6c9270> { inserts (), updates (), deletes (( "0x7b862ec0 <x-coredata://21112FCF-88DE-4D7F-A5C5-1A97731D180E/Notizen/p2>" )) locks () } 2014-12-16 17:48:17.211 NoteApp[3161:45746] CoreData: error: failed to resolve optimistic locking failure. Next attempt will be: <NSSaveChangesRequest: 0x7a6c9270> { inserts (), updates (), deletes (( "0x7b862ec0 <x-coredata://21112FCF-88DE-4D7F-A5C5-1A97731D180E/Notizen/p2>" )) locks () } 2014-12-16 17:48:17.212 NoteApp[3161:45746] CoreData: sql: BEGIN EXCLUSIVE 2014-12-16 17:48:17.212 NoteApp[3161:45746] CoreData: sql: DELETE FROM ZNOTIZEN WHERE Z_PK = ? AND Z_OPT = ? 2014-12-16 17:48:17.212 NoteApp[3161:45746] CoreData: details: SQLite bind[0] = (int64)2 2014-12-16 17:48:17.212 NoteApp[3161:45746] CoreData: details: SQLite bind[1] = nil 2014-12-16 17:48:17.213 NoteApp[3161:45746] CoreData: sql: ROLLBACK 2014-12-16 17:48:17.213 NoteApp[3161:45746] CoreData: sql: SELECT Z_PK,Z_OPT FROM ZNOTIZEN WHERE Z_PK IN (2) ORDER BY Z_PK 2014-12-16 17:48:17.213 NoteApp[3161:45746] CoreData: annotation: sql execution time: 0.0003s 2014-12-16 17:48:17.214 NoteApp[3161:45746] CoreData: error: failed to resolve optimistic locking failure: optimistic locking failure with (null) 2014-12-16 17:48:17.214 NoteApp[3161:45746] CoreData: error: failed to resolve optimistic locking failure. Old save request was: <NSSaveChangesRequest: 0x7a6c9270> { inserts (), updates (), deletes (( "0x7b862ec0 <x-coredata://21112FCF-88DE-4D7F-A5C5-1A97731D180E/Notizen/p2>" )) locks () } 2014-12-16 17:48:17.214 NoteApp[3161:45746] CoreData: error: failed to resolve optimistic locking failure. Next attempt will be: <NSSaveChangesRequest: 0x7a6c9270> { inserts (), updates (), deletes (( "0x7b862ec0 <x-coredata://21112FCF-88DE-4D7F-A5C5-1A97731D180E/Notizen/p2>" )) locks () } 2014-12-16 17:48:17.214 NoteApp[3161:45746] CoreData: sql: BEGIN EXCLUSIVE 2014-12-16 17:48:17.214 NoteApp[3161:45746] CoreData: sql: DELETE FROM ZNOTIZEN WHERE Z_PK = ? AND Z_OPT = ? 2014-12-16 17:48:17.215 NoteApp[3161:45746] CoreData: details: SQLite bind[0] = (int64)2 2014-12-16 17:48:17.215 NoteApp[3161:45746] CoreData: details: SQLite bind[1] = nil 2014-12-16 17:48:17.215 NoteApp[3161:45746] CoreData: sql: ROLLBACK 2014-12-16 17:48:17.215 NoteApp[3161:45746] CoreData: sql: SELECT Z_PK,Z_OPT FROM ZNOTIZEN WHERE Z_PK IN (2) ORDER BY Z_PK 2014-12-16 17:48:17.215 NoteApp[3161:45746] CoreData: annotation: sql execution time: 0.0003s 2014-12-16 17:48:17.216 NoteApp[3161:45746] CoreData: error: failed to resolve optimistic locking failure: optimistic locking failure with (null) 2014-12-16 17:48:17.216 NoteApp[3161:45746] CoreData: error: failed to resolve optimistic locking failure. Old save request was: <NSSaveChangesRequest: 0x7a6c9270> { inserts (), updates (), deletes (( "0x7b862ec0 <x-coredata://21112FCF-88DE-4D7F-A5C5-1A97731D180E/Notizen/p2>" )) locks () } 2014-12-16 17:48:17.216 NoteApp[3161:45746] CoreData: error: failed to resolve optimistic locking failure. Next attempt will be: <NSSaveChangesRequest: 0x7a6c9270> { inserts (), updates (), deletes (( "0x7b862ec0 <x-coredata://21112FCF-88DE-4D7F-A5C5-1A97731D180E/Notizen/p2>" )) locks () } 2014-12-16 17:48:17.216 NoteApp[3161:45746] CoreData: sql: BEGIN EXCLUSIVE 2014-12-16 17:48:17.217 NoteApp[3161:45746] CoreData: sql: DELETE FROM ZNOTIZEN WHERE Z_PK = ? AND Z_OPT = ? 2014-12-16 17:48:17.217 NoteApp[3161:45746] CoreData: details: SQLite bind[0] = (int64)2 2014-12-16 17:48:17.217 NoteApp[3161:45746] CoreData: details: SQLite bind[1] = nil 2014-12-16 17:48:17.217 NoteApp[3161:45746] CoreData: sql: ROLLBACK 2014-12-16 17:48:17.217 NoteApp[3161:45746] CoreData: sql: SELECT Z_PK,Z_OPT FROM ZNOTIZEN WHERE Z_PK IN (2) ORDER BY Z_PK 2014-12-16 17:48:17.218 NoteApp[3161:45746] CoreData: annotation: sql execution time: 0.0003s 2014-12-16 17:48:17.218 NoteApp[3161:45746] CoreData: error: failed to resolve optimistic locking failure: optimistic locking failure with (null) 2014-12-16 17:48:17.218 NoteApp[3161:45746] CoreData: error: failed to resolve optimistic locking failure. Old save request was: <NSSaveChangesRequest: 0x7a6c9270> { inserts (), updates (), deletes (( "0x7b862ec0 <x-coredata://21112FCF-88DE-4D7F-A5C5-1A97731D180E/Notizen/p2>" )) locks () } 2014-12-16 17:48:17.218 NoteApp[3161:45746] CoreData: error: failed to resolve optimistic locking failure. Next attempt will be: <NSSaveChangesRequest: 0x7b89bda0> { inserts (), updates (), deletes (( "0x7b862ec0 <x-coredata://21112FCF-88DE-4D7F-A5C5-1A97731D180E/Notizen/p2>" )) locks () } 2014-12-16 17:48:17.219 NoteApp[3161:45746] CoreData: sql: BEGIN EXCLUSIVE 2014-12-16 17:48:17.219 NoteApp[3161:45746] CoreData: sql: DELETE FROM ZNOTIZEN WHERE Z_PK = ? AND Z_OPT = ? 2014-12-16 17:48:17.219 NoteApp[3161:45746] CoreData: details: SQLite bind[0] = (int64)2 2014-12-16 17:48:17.219 NoteApp[3161:45746] CoreData: details: SQLite bind[1] = nil 2014-12-16 17:48:17.219 NoteApp[3161:45746] CoreData: sql: ROLLBACK 2014-12-16 17:48:17.220 NoteApp[3161:45746] CoreData: sql: SELECT Z_PK,Z_OPT FROM ZNOTIZEN WHERE Z_PK IN (2) ORDER BY Z_PK 2014-12-16 17:48:17.220 NoteApp[3161:45746] CoreData: annotation: sql execution time: 0.0003s 2014-12-16 17:48:17.220 NoteApp[3161:45746] CoreData: error: failed to resolve optimistic locking failure: optimistic locking failure with (null) 2014-12-16 17:48:17.220 NoteApp[3161:45746] CoreData: error: failed to resolve optimistic locking failure. Old save request was: <NSSaveChangesRequest: 0x7b89bda0> { inserts (), updates (), deletes (( "0x7b862ec0 <x-coredata://21112FCF-88DE-4D7F-A5C5-1A97731D180E/Notizen/p2>" )) locks () } 2014-12-16 17:48:17.221 NoteApp[3161:45746] CoreData: error: failed to resolve optimistic locking failure. Next attempt will be: <NSSaveChangesRequest: 0x7b89bda0> { inserts (), updates (), deletes (( "0x7b862ec0 <x-coredata://21112FCF-88DE-4D7F-A5C5-1A97731D180E/Notizen/p2>" )) locks () } 2014-12-16 17:48:17.221 NoteApp[3161:45746] CoreData: error: fatal: Unable to recover from optimistic locking failure. (lldb)

1 个答案:

答案 0 :(得分:0)

将其放在AppDelegate中。并确保NameOfFile.sqlite在项目中并被复制。

lazy var persistentStoreCoordinator: NSPersistentStoreCoordinator? = {
    // The persistent store coordinator for the application. This implementation creates and return a coordinator, having added the store for the application to it. This property is optional since there are legitimate error conditions that could cause the creation of the store to fail.
    // Create the coordinator and store
    var coordinator: NSPersistentStoreCoordinator? = NSPersistentStoreCoordinator(managedObjectModel: self.managedObjectModel)
    let url = self.applicationDocumentsDirectory.URLByAppendingPathComponent("NameOfFile.sqlite")
    var error: NSError? = nil
    var failureReason = "There was an error creating or loading the application's saved data."
    // Copying
    let path = NSBundle.mainBundle().pathForResource("NameOfFile", ofType:"sqlite")!

    NSFileManager.defaultManager().copyItemAtPath(path, toPath: url.path!, error:nil)
    if coordinator!.addPersistentStoreWithType(NSSQLiteStoreType, configuration: nil, URL: NSURL.fileURLWithPath(url.path!), options: nil, error: &error) == nil {
        coordinator = nil
        // Report any error we got.
        let dict = NSMutableDictionary()
        dict[NSLocalizedDescriptionKey] = "Failed to initialize the application's saved data"
        dict[NSLocalizedFailureReasonErrorKey] = failureReason
        dict[NSUnderlyingErrorKey] = error
        //error = NSError.errorWithDomain("YOUR_ERROR_DOMAIN", code: 9999, userInfo: dict)
        // Replace this with code to handle the error appropriately.
        // abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.
        NSLog("Unresolved error \(error), \(error!.userInfo)")
        abort()
    }

    return coordinator
}()

看起来你可能会发生故障。我也有不同的方法,所以希望这会有所帮助。

    // called when a row deletion action is confirmed
override func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
        switch editingStyle {
        case .Delete:
            // remove the deleted item from the model
            let appDel:AppDelegate = UIApplication.sharedApplication().delegate as AppDelegate
            let context:NSManagedObjectContext = appDel.managedObjectContext!
            context.deleteObject(myData[indexPath.row] as NSManagedObject)
            myData.removeAtIndex(indexPath.row)
            context.save(nil)

           //tableView.reloadData()
            // remove the deleted item from the `UITableView`
            self.tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Fade)
        default:
            return

        }
}