Skip to content

Instantly share code, notes, and snippets.

@cyberfox
Last active August 29, 2015 13:59
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cyberfox/10686438 to your computer and use it in GitHub Desktop.
Save cyberfox/10686438 to your computer and use it in GitHub Desktop.
RubyMotion entity name weirdness
# This shows the list of entities, and that they look like String objects to RubyMotion.
(main)> mapping = App.delegate.managedObjectContext.persistentStoreCoordinator.managedObjectModel.entitiesByName.keys.each_with_object({}) { |key, map| map[key.to_s] = key}
=> {"Auction"=>"Auction", "Category"=>"Category", "Entry"=>"Entry", "Host"=>"Host"}
# This is an example lookup for the Category entity, by name. It fails.
(main)> App.delegate.managedObjectContext.persistentStoreCoordinator.managedObjectModel.entitiesByName['Category']
=> nil
# Here we try to insert a new object for an entity by name, using 'Category' as a raw string. It fails, unable to locate it.
(main)> begin
entity = NSEntityDescription.insertNewObjectForEntityForName('Category', inManagedObjectContext: App.delegate.managedObjectContext)
rescue => e
puts e.reason
end
+entityForName: could not locate an entity named 'Category' in this model.
=> nil
# Here we try to insert a new object for an entity by name, using 'Category' as a lookup to the mapping table above. It succeeds.
(main)> begin
entity = NSEntityDescription.insertNewObjectForEntityForName(mapping['Category'], inManagedObjectContext: App.delegate.managedObjectContext)
rescue => e
puts e.reason
end
=> #<Category_Category_:0x7f8c90d393b0>
# The only difference I can find is that the strings I pass in are NSMutableString objects, but the 'values' in the dictionary
# above are some kind of private subclass of NSString used by the CoreData code. This apparently is enough to make them not
# findable in a dictionary and via #iNOFEFN as demonstrated above. I don't understand why, though.
(main)> mapping.keys.map &:superclass
=> [NSMutableString, NSMutableString, NSMutableString, NSMutableString]
(main)> mapping.values.map &:superclass
=> [_PFEncodedString, _PFString, _PFString, _PFString]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment