4
votes


i've tested swift on a little iPhone project using Xcode 6 beta 2. The project uses core data to access 2 entities (User/Contract) in a data model. Inside the IOS Simulator everything works fine.

Problem: When i build the app for IOS7 and test the app on my iPhone5s (running IOS 7.1.1) the program can only save data into the first entity (as defined in core data model = User). For all other entities the NSEntityDescription.entityForName(...) is "nil". In managedObjectModel (println(managedObjectModel)) all entities are included. Seems like the data model is not correct included into the SQL-Database on IOS7?! Does anyone have a solution/idea? Thanks :-)

    var myAppDel:AppDelegate = UIApplication.sharedApplication().delegate as AppDelegate
    var myContext:NSManagedObjectContext = myAppDel.managedObjectContext

    println(NSEntityDescription.entityForName("User", inManagedObjectContext: myContext))
    println(NSEntityDescription.entityForName("Contract", inManagedObjectContext: myContext)) 

    var newContract:AnyObject = NSEntityDescription.insertNewObjectForEntityForName("Contract", inManagedObjectContext: myContext)

    newContract.setValue("" + txtContract.text, forKey: "contractName")
    myContext.save(nil)

    var newUser = NSEntityDescription.insertNewObjectForEntityForName("User", inManagedObjectContext: myContext)

    newUser.setValue("" + txtUsername.text, forKey: "userName")
    newUser.setValue("" + txtPassword.text, forKey: "userPass")
    newUser.setValue(newContract, forKey: "contracts") // Save Relationship

    myContext.save(nil)

Error: Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: '+entityForName: could not locate an entity named 'Contract' in this model.'

1
I was running into the same problem and finally gave up on it. If I rebuilt enough times it would eventually work for a run or two and then stop running again. If you have a small isolated example I'd suggest file a bug report at bugreport.apple.com. Unfortunately, the example I was working on was pretty convoluted in a lot of ways so I gave up on it, as just yet another way swift won't be ready for prime time for my current project anyway.David Berry
Hi Dave, thanks for your quick reply! I'm waiting on the error correction. Maybe I send apple a bug report earlier in the week.user3781019
I think I am seeing this issue as well. Everything works fine on the iOS 8 simulator, but running on my iOS 7.1.2 iPhone, I get this error: "NSFetchRequest could not locate an NSEntityDescription for entity name...". I am using Xcode 6 Beta 4. My context, model and storeCoorindator objects all look fine -- I can see that my model object contains all the correct entities. I've given up for now; I'll try again with the next beta.DarrylLawson

1 Answers

13
votes

It seems to be a bug for now. As explained on this post: https://devforums.apple.com/message/996259#996259.

The workaround for this bug is to use a NSString instead of passing a String as the entity name:

    let myEntity: NSString = "EntityName"
    var fetchRequest = NSFetchRequest(entityName: myEntity)