2

I have been working with CoreData recently for an app of mine. Oddly enough it is crashing only on the iPad Air with iOS 7.x. I have run in both on physical devices and the iOS simulator, it never fails to crash on the Air, and always runs elsewhere. (It does not crash on the iPad Air iOS 8.0)

The crash is happening in one of my CoreData calls, specifically

    var childEntity = NSEntityDescription.entityForName(ChildInSessionEntity, inManagedObjectContext: context)
    var parentEntity = NSEntityDescription.entityForName(ParentInSessionEntity, inManagedObjectContext: context)
    var newChild = ChildInSession(entity: childEntity, insertIntoManagedObjectContext: context)
    var newParent = ParentInSession(entity: parentEntity, insertIntoManagedObjectContext: context)

The first line does not return nil, but the 2nd line does, and on the 4th line my app crashes.

It is a very odd scenario as on other simulators it does not return a nil value.

Any help or suggestions are greatly appreciated

chaoscope
  • 33
  • 2
  • Which version of XCode are you using? It's very likely that this is a bug that 1) may have been fixed already or 2) you should report to Apple (there have been some other iOS 7.1 issues over the course of the beta period). – snickle Jul 23 '14 at 18:22
  • The newest as of yesterday, xCode-Beta4, it seemed like a bug to me as well, but my concern was that my childEntity did not get set to a nil value – chaoscope Jul 23 '14 at 18:23
  • What are `ChildInSessionEntity` and `ParentInSessionEntity`? Are they strings? – Tom Harrington Jul 23 '14 at 20:45
  • Yes, they are strings, I'm probably not following proper naming conventions, but I have saved all my entity strings in variables in a separate file – chaoscope Jul 23 '14 at 20:47

2 Answers2

1

I think I had the same issue as you. I found that it would crash when accessing an entity for the second time. I was able to fix the issue thanks to this article: http://stackanswers.com/questions/24440927/swift-core-data-on-ios7-device-entityforname-on-second-entity-is-nil

For some reason, you need pass the entity name as an instance of NSString, e.g.

// Rather than...
let entity = NSEntityDescription.entityForName("Entity", inManagedObjectContext: self.managedObjectContext!)

// Use this instead...
let entityName: NSString = "Entity"
let entity = NSEntityDescription.entityForName(entityName, inManagedObjectContext: self.managedObjectContext!)

If you're using a fetch request then you can use the shortcut:

let entityName: NSString = "Entity"
let fetchRequest = NSFetchRequest(entityName)
Ian
  • 7,480
  • 2
  • 47
  • 51
0

I think it is a Bug, i have the same issue only with iphone 5s (iOS 7.1). It accidentally worked by choosing the .xcdatamodeld and under the "File Inspector" under "Target Membership" removing the check from the project.

enter image description here

iFadi
  • 915
  • 2
  • 12
  • 20
  • I found that as a possible solution as well, I tried it, (Uncheck-ed, built, ran, crash) then checked it again (build, run, crash), I am also not sure how to log such a bug with Apple – chaoscope Jul 29 '14 at 17:55
  • In the mean time, I have found a workaround/fix, I explicitly made my constant Entity names of type NSString and it compiles and runs, I am new to stackoverflow, should I add this as an answer? Still feels like a bug to me – chaoscope Aug 05 '14 at 02:13