MattsFatCat
MattsFatCat

Reputation: 13

CoreData works on my simulator in but not on my iPhone in iOS 8 Swift using xCode 6

When going back through my code with breakpoints i was able to narrow down my error to one line specifically but there could be other problems later. This works just fine on my simulator but when i try to use my phone it throws an error on that line.

I have specified this line with the error by saying "<<<<< THIS IS MY ERROR"

So here is my code

import UIKit
import CoreData

class ViewController: UIViewController, UITextFieldDelegate {

@IBOutlet weak var startUpLabel: UILabel!
@IBOutlet weak var nameSubmitOutlet: UIButton!
@IBOutlet weak var characterName: UITextField!
@IBOutlet weak var enterGameOutlet: UIButton!
@IBOutlet weak var continueChangeLabelStartUpOutlet: UIButton!


var startUpScene = StartUpScene()
var index = 0
var name: String? = nil

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.

    startUpLabel.text = "[Heavy breathing]"

    self.characterName.delegate = self

    enterGameOutlet.hidden = true
    characterName.hidden = true
    nameSubmitOutlet.hidden = true

}

*code in here blah*

@IBAction func continueChangeLabelStartup() {


    if index < startUpScene.startUpLabelArray.count {

        startUpLabel.text = startUpScene.startUpLabelArray[index++]
        if index == 3 {
            characterName.hidden = false
            nameSubmitOutlet.hidden = false
            continueChangeLabelStartUpOutlet.hidden = true
        }

        if index == startUpScene.startUpLabelArray.count {//later on enter game will have its own method to hide everything except itself
            startUpLabel.hidden = true
            continueChangeLabelStartUpOutlet.hidden = true
            enterGameOutlet.hidden = false
        }
    }
}

@IBAction func nameSubmitButton() {
    name = characterName.text



    var appDel: AppDelegate = (UIApplication.sharedApplication().delegate as AppDelegate) //creates and instance of the appdelegate
    if let context: NSManagedObjectContext = appDel.managedObjectContext {//<<<<< THIS IS MY ERROR (i tried using an "if let" statement to unwrap the code that returned as an optional. Previously it didn't return as an optional so maybe that why I'm getting the error.The "bang"(!) was too risky
        var newUser = NSEntityDescription.insertNewObjectForEntityForName("UserInfo", inManagedObjectContext: context) as NSManagedObject // creates a "newUser" var and enters the data i want to save in the "userName" key under the UserInfo table i created in the .xcdatamold earlier

        newUser.setValue(name!, forKey: "userName") // sets the name in the .xcdatamold as if it were a dictionary

        context.save(nil)//saves the data
    }// creates an instance of the NSManagedObjectContext 



    characterName.hidden = true
    nameSubmitOutlet.hidden = true
    continueChangeLabelStartUpOutlet.hidden = false

    characterName.resignFirstResponder()

    startUpLabel.text = "Ah that's right. I'm \(name!)"
}
*Other code*

}

I load my data in this code. This is where the game will default to after they go through the intro the first time they use the app. So i need to reload it here. Again it works fine on my simulator but i can't get the code to go this far in my phone.

import UIKit
import CoreData

class GameMenuViewController: UIViewController {

@IBOutlet weak var loadGameOutlet: UIButton!
var name = ""

*code*

@IBAction func loadGameButton() {

    var appDel: AppDelegate = (UIApplication.sharedApplication().delegate as AppDelegate) //creates and instance of the appdelegate
    var context: NSManagedObjectContext = appDel.managedObjectContext! // creates an instance of the NSManagedObjectContext

    var request = NSFetchRequest(entityName: "UserInfo")//requests the userName entity which is the name
    request.returnsObjectsAsFaults = false //dont get the object back until i cast it as an object and use it

    var results: NSArray = context.executeFetchRequest(request, error: nil)!

    if results.count > 0 {

        var res = results[0] as NSManagedObject
        name = res.valueForKey("userName") as String
        println(name) *my name prints in the console here when i use the simulator*
    } else {
        println("Error")
    }
}

}

Here is my code error

2014-12-20 23:19:54.773 Forgotten[1316:313403] CoreData: error: -addPersistentStoreWithType:SQLite configuration:(null) URL:file:///var/mobile/Containers/Data/Application/7DCE915A-6A49-4B6D-9AF5-51D7CE5BEDDB/Documents/Forgotten.sqlite options:(null) ... returned error Error Domain=NSCocoaErrorDomain Code=134100 "The operation couldn’t be completed. (Cocoa error 134100.)" UserInfo=0x15d71df0 {metadata={
NSPersistenceFrameworkVersion = 519;
NSStoreModelVersionHashes =     {
};
NSStoreModelVersionHashesVersion = 3;
NSStoreModelVersionIdentifiers =     (
    ""
);
NSStoreType = SQLite;
NSStoreUUID = "B9C87F2A-E940-4569-B98C-396127B9339A";
"_NSAutoVacuumLevel" = 2;

}, reason=The model used to open the store is incompatible with the one used to create the store} with userInfo dictionary {
metadata =     {
    NSPersistenceFrameworkVersion = 519;
    NSStoreModelVersionHashes =         {
    };
    NSStoreModelVersionHashesVersion = 3;
    NSStoreModelVersionIdentifiers =         (
        ""
    );
    NSStoreType = SQLite;
    NSStoreUUID = "B9C87F2A-E940-4569-B98C-396127B9339A";
    "_NSAutoVacuumLevel" = 2;

};
reason = "The model used to open the store is incompatible with the one used to create the store";
}
2014-12-20 23:19:54.788 Forgotten[1316:313403] Unresolved error Optional(Error Domain=YOUR_ERROR_DOMAIN Code=9999 "Failed to initialize the application's saved data" UserInfo=0x15ef5990 {NSLocalizedDescription=Failed to initialize the application's saved data, NSUnderlyingError=0x15d71e10 "The operation couldn’t be completed. (Cocoa error 134100.)", NSLocalizedFailureReason=There was an error creating or loading the application's saved data.}), Optional([NSLocalizedDescription: Failed to initialize the application's saved data, NSUnderlyingError: Error Domain=NSCocoaErrorDomain Code=134100 "The operation couldn’t be completed. (Cocoa error 134100.)" UserInfo=0x15d71df0 {metadata={
NSPersistenceFrameworkVersion = 519;
NSStoreModelVersionHashes =     {
};
NSStoreModelVersionHashesVersion = 3;
NSStoreModelVersionIdentifiers =     (
    ""
);
NSStoreType = SQLite;
NSStoreUUID = "B9C87F2A-E940-4569-B98C-396127B9339A";
"_NSAutoVacuumLevel" = 2;
}, reason=The model used to open the store is incompatible with the one used to create the store}, NSLocalizedFailureReason: There was an error creating or loading the application's saved data.])
(lldb) 

I am not that good at debugging but this was all i could do. And here are my results

self    Forgotten.AppDelegate   0x15da1440  0x15da1440
coordinator NSPersistentStoreCoordinator?   nil None
error   NSError?    domain: nil - code: 9999    0x15ed82c0
failureReason   String  "There was an error creating or loading the application's saved data."  
url NSURL   "file:///var/mobile/Containers/Data/Application/7DCE915A-6A49-4B6D-9AF5-51D7CE5BEDDB/Documents/Forgotten.sqlite"    0x15ece010
dict    NSMutableDictionary 3 key/value pairs   0x15e89ac0

Upvotes: 1

Views: 777

Answers (1)

Ian
Ian

Reputation: 12768

The "The model used to open the store is incompatible with the one used to create the store" error indicates that the model used to store your data has changed and you have not written the appropriate code to handle the changes. If you are experiencing this error on your phone while testing your app, simply delete the app and reinstall it. If you are experiencing this in the simulator you can reset content and settings of the simulator. If these methods are not appropriate because your data is important, see Core Data Model Versioning and Data Migration.

Upvotes: 2

Related Questions