Skip to content

Instantly share code, notes, and snippets.

@bgrace
Last active August 29, 2015 14:09
Show Gist options
  • Save bgrace/021f55a9f160685e2433 to your computer and use it in GitHub Desktop.
Save bgrace/021f55a9f160685e2433 to your computer and use it in GitHub Desktop.
Printable on NSManaged Object doesn't get invoked.
// See notes in first comment
if let moc = AppDelegate.instance.managedObjectContext {
// fetching NSManagedObject with fancy generic typesafe helper method
if let allGrowers = moc.fetch(Grower.self, predicate: nil, sortDescriptors: nil, returnsObjectsAsFaults: true) {
switch allGrowers {
case .Value(let b):
let allGrowers = b.contents
for grower in allGrowers {
println("\(grower.name) -> \(grower)")
}
case .Error(let e):
println("Couldn't fetch all growers because \(e)")
}
}
// fetching the objects as simply as possible
let request = NSFetchRequest(entityName: "Grower")
let result = moc.executeFetchRequest(request, error: nil)
println("Raw result:")
println(result)
}
///// Extension for the Grower object:
extension Grower : NamedEntity {
class var entityName: String {
return "Grower"
}
override var description: String {
return self.name
}
override var debugDescription: String {
return "Debug: \(self.name)"
}
}
@bgrace
Copy link
Author

bgrace commented Nov 15, 2014

In response to the Stackoverflow question: http://stackoverflow.com/questions/26599493/printing-nsmanagedobject-subclassed-core-data-object-to-console-returns-empty-li and in particular the first comment.

I'm using Xcode 6.1 and developing against the iOS 8.1 SDK.

This code should fetch all objects of type Grower, and print them. Instead, the description method is never called. I demonstrate this fetching the objects in two different ways.

Note:

  • The objects at this point are already saved, so we're just loading them from storage.
  • It doesn't matter whether returnObjectsAsFaults is true or false
  • Setting a breakpoint in description method is never hit during string interpolation.
  • However I can call the description method and get the expected result back.
  • I can get any other variables from the object that should be available for its defined type (it behaves in every other respect like a Swift class).
  • It doesn't matter if I cast the object to Grower or leave it as NSManagedObject.

The output for println("\(grower.name) -> \(grower)") is always:
EXAMPLE VINEYARDS ->

The output for dumping the whole array is always:

Raw result:
Optional([, , , , , , , , , , , , , , , , , , , , ])

I'm pretty new to CoreData. Is it a bug or am I doing it wrong?

@tualatrix
Copy link

Me too. I tried to print single object, it just returned:

{
CoreData.NSManagedObject = {
ObjectiveC.NSObject = {}
}
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment